Mybatis maven项目配置以及案例

来源:互联网 发布:mac u盘装win8单系统 编辑:程序博客网 时间:2024/05/17 03:27
  1. mybatis的配置文件要放在resource目录下(maven的要求),放和mapper类同级目录中不会被识别(target类目录中不会生成该xml)
    如果没按上面做,会出现类似于下面的错误:

    Invalid bound statement (not found): com.xx.mapper.xx.getPageNum
  2. 上面的问题会导致,在配置mappers时,不推荐使用包扫描配置:

    <mappers>    <package name="com.xx.mapper"/></mappers>

    应改成:

    <mappers>    <mapper resource="xx.xml"/></mappers>

一个简单的mybatis和spring整合案例

  1. Sping的配置文件:

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd       ">    <context:component-scan base-package="com.batis"/>    <!-- 加载配置文件 -->    <context:property-placeholder location="classpath:db.properties" />    <!-- 数据库连接池 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"          destroy-method="close">        <property name="driverClass" value="${jdbc.driver}" />        <property name="jdbcUrl" value="${jdbc.url}" />        <property name="user" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />    </bean>    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 指定mybatis核心配置文件 -->        <property name="configLocation" value="classpath:SqlMapperConfig.xml"></property>        <!-- 指定会话工厂使用的数据源 -->        <property name="dataSource" ref="dataSource"></property>    </bean>    <bean id="userDao" class="com.batis.dao.TestBatisDaoImpl">        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>    </bean>    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <!-- 指定要扫描的包的全路径名称,如果有多个包用英文状态下的逗号分隔 -->        <property name="basePackage" value="cn.xx.mapper"></property>    </bean></beans>
  2. 数据库连接配置文件:

    jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8jdbc.username=rootjdbc.password=root
  3. mybatis的配置文件:

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <typeAliases>        <package name="com.batis"/>    </typeAliases>    <mappers>        <!--<mapper resource="TestBatisMapper.xml"/>-->        <package name="com.batis.mapper"/>    </mappers></configuration>
  4. 新建Mapper类

    public interface TestBatisMapper {    TestBatis getPageNum(String uuid);}
  5. 新建Dao(省略)以及实现类:

    public class TestBatisDaoImpl extends SqlSessionDaoSupport implements TestBatisDao{    public TestBatis getTestBatis(String uuid) {        SqlSession sqlSession = this.getSqlSession();        TestBatisMapper mapper = sqlSession.getMapper(TestBatisMapper.class);        TestBatis test= mapper.getPageNum(uuid);        System.out.println(num);        return test;    }}
  6. Test类

    @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:ApplicationContext.xml")public class Testbatis {    @Autowired    TestBatisDaoImpl testBatisDao ;    @Test    public void test() {        testBatisDao.getTestBatiss("1");    }}
阅读全文
0 0
原创粉丝点击