Spring-Mybatis --- 配置SqlSessionFactoryBean,整合Spring-Mybatis

 要利用Mybatis首先是需要导入mybatis-x.x.x.jar,其次,要整合Spring和Mybatis需要导入mybatis-spring-x.x.x.jar。

  JAR : mybatis-x.x.x

        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.6version>
        dependency>

  JAR : mybatis-spring-x.x.x

        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.2.2version>
        dependency>

1、Spring整合Mybatis的xml配置

  xml : POM

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.1.41version>
        dependency>

        <dependency>
            <groupId>org.apache.tomcatgroupId>
            <artifactId>tomcat-servlet-apiartifactId>
            <version>7.0.54version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.apache.tomcatgroupId>
            <artifactId>tomcat-jdbcartifactId>
            <version>7.0.23version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.18version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.2.2version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.6version>
        dependency>

  xml : DataSource

    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
        <property name="poolProperties">
            <bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
                <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.user}" />
                <property name="password" value="${jdbc.password}" />
                
                <property name="jmxEnabled" value="true" />
                
                <property name="testWhileIdle" value="true" />
                
                <property name="testOnBorrow" value="false" />
                <property name="testOnReturn" value="false" />
                <property name="initialSize" value="${jdbc.initialPoolSize}" />
                <property name="maxActive" value="${jdbc.maxActive}" />
                <property name="maxWait" value="${jdbc.maxWait}" />
                <property name="minIdle" value="${jdbc.minIdle}" />
                <property name="maxIdle" value="${jdbc.maxIdle}" />
                <property name="maxAge" value="60000" />
                
                <property name="timeBetweenEvictionRunsMillis" value="15000" />
                
                <property name="minEvictableIdleTimeMillis" value="60000" />
                <property name="removeAbandoned" value="true" />
                <property name="removeAbandonedTimeout" value="30" />
                <property name="validationQuery" value="SELECT 1" />
                <property name="validationInterval" value="30000" />
            bean>
        property>
    bean>

 

常用配置:

(如果在mybatis-config.xml利用进行xml映射文件的配置,就可以不用配置下面的mapperLocation属性了)


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
      p:dataSource-ref="dataSource"
      p:configLocation="classpath:mybatis-config.xml"
      p:mapperLocations="classpath:com/eliteams/quick4j/web/dao/*.xml"/>


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
      p:basePackage="com.eliteams.quick4j.web.dao"
      p:sqlSessionFactoryBeanName="sqlSessionFactory"/>

-------------------------阿弥陀佛----佛祖保佑----永无BUG--------------------------

2、Spring和Mybatis整合的三种方式

  • SqlSessionFactoryBean来替代SqlSessionFactoryBuilder来创建SqlSession

  • 利用mybatis映射文件**.xml来配置

    SqlSessionFactoryBean有一个必须属性dataSource,另外其还有一个通用属性configLocation(用来指定mybatis的xml配置文件路径)。

Spring的xml配置:

      
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <property name="dataSource" ref="dataSource" /> 
          
          <property  name="configLocation"  value="classpath:sqlMapConfig.xml"/>
      bean>

mybatis总配置文件sqlMapConfig.xml:

<configuration>
  <typeAliases>
     <typeAlias type="com.xxt.ibatis.dbcp.domain.User" alias="User" />
  typeAliases>
  <mappers>
     <mapper resource="com/xxt/ibatis/dbcp/domain/userMapper.xml" />
  mappers>
configuration>

userMapper.xml:

<mapper namespace="com.xxt.ibatis.dbcp.dao.UserDao">
     <resultMap type="User" id="userMap">
        <id property="id" column="id" />
        <result property="name" column="name" />
        <result property="password" column="password" />
        <result property="createTime" column="createtime" />
     resultMap>
     <select id="getUserById" parameterType="int" resultMap="userMap">
       select * from user where id = #{id}
     select>
<mapper/>

DAO层接口类UserDao.java:注意此处定义的接口方法需要和UserMapper.xml映射的