Spring:(八) mybatis-spring整合

一、导入jar包

所需jar包:junit,mybatis,mysql,spring-webmvc,spring-jdbc,aspectjweaver,mybatis-spring

二、条件

image-20200226212202035

  1. Spring - MyBatis,需要一个 SqlSessionFactory 和至少一个数据映射器类。

  2. 在 MyBatis-Spring 中,可使用 SqlSessionFactoryBean来创建 SqlSessionFactory

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
    </bean>
  3. configLocation,指定 MyBatis 的 XML 配置文件路径。

  4. SqlSessionTemplate 是 MyBatis-Spring 的核心,作为 SqlSession 的一个实现,这意味着可以使用它无缝代替你代码中已经在使用的 SqlSession

  5. 模板可以参与到 Spring 的事务管理中,并且由于其是线程安全的,可以供多个映射器类使用

  6. 可以使用 SqlSessionFactory 作为构造方法的参数来创建 SqlSessionTemplate 对象。

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
      <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
  7. 需要在你的 bean 中添加一个 SqlSession 属性

    public class UserDaoImpl implements UserDao {
    
      private SqlSession sqlSession;
    
      public void setSqlSession(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
      }
    
      public User getUser(String userId) {
        return sqlSession.getMapper...;
      }
    }
  8. 注入SqlSessionTemplate

    <bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl">
      <property name="sqlSession" ref="sqlSession" />
    </bean>

三、实现一

  1. 引入Spring配置文件beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
  2. 配置数据源替换mybaits的数据源

    <!--配置数据源:数据源有非常多,可以使用第三方的,也可使使用Spring的-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
  3. 配置SqlSessionFactory,关联MyBatis

    <!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--关联Mybatis-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/kuang/dao/*.xml"/>
    </bean>
  4. 注册sqlSessionTemplate,关联sqlSessionFactory;

    <!--注册sqlSessionTemplate , 关联sqlSessionFactory-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--利用构造器注入-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
  5. 增加Dao接口的实现类;私有化sqlSessionTemplate

    public class UserDaoImpl implements UserMapper {
    
        //sqlSession不用我们自己创建了,Spring来管理
        private SqlSessionTemplate sqlSession;
    
        public void setSqlSession(SqlSessionTemplate sqlSession) {
            this.sqlSession = sqlSession;
        }
    
        public List<User> selectUser() {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            return mapper.selectUser();
        }
    
    }
  6. 注册bean实现

    <bean id="userDao" class="com.kuang.dao.UserDaoImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>
  7. 测试

        @Test
        public void test2(){
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            UserMapper mapper = (UserMapper) context.getBean("userDao");
            List<User> user = mapper.selectUser();
            System.out.println(user);
        }

四、实现二(继承Support类 , 直接getSqlSession() 获得 , 直接注入SqlSessionFactory)

  1. 实现类

    public class UserDaoImpl extends SqlSessionDaoSupport implements UserMapper {
        public List<User> selectUser() {
            UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
            return mapper.selectUser();
        }
    }
  2. bean的配置

    bean id="userDao" class="com.ry.dao.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
  3. 测试

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserMapper mapper = (UserMapper) context.getBean("userDao");
        List<User> user = mapper.selectUser();
        System.out.println(user);
    }

猜你喜欢

转载自www.cnblogs.com/dreamzone/p/12402847.html