Spring整合Mybatis方式二 - SqlSessionTemplate

前置工作

  • 导包:mybatis-spring、mysql-connector-java、mybatis、spring-webmvc、spring-jdbc

  • 实体类

  • DAO层两个文件(接口、xml文件);Service层的接口

spring-dao.xml

核心:sqlSessionTemplate配置

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

完整代码:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <!--    1.使用Spring配置dataSource,相当于myBatis配置文件的<environments>。需要spring-jdbc包-->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--    2.配置SqlSessionFactoryBean 等同于SqlSessionFactory
            做读取数据源以及注册mapper.xml的工作-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/ylzl/mapper/*.xml"/>
        <!--  配置MyBatis的全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="bookMapper" class="com.ylzl.mapper.BookMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>
    <bean id="bookServiceImpl" class="com.ylzl.service.BookServiceImpl"/>
</beans>

DAO层

新加Dao接口的实现类:私有化sqlSessionTemplate

public class BookMapperImpl implements BookMapper {
    //sqlSession不用我们自己创建了,Spring来管理
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public Book getBookById(Integer bookID) {
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        return mapper.getBookById(bookID);
    }
}

注:注册bean实现(dao接口实现类的)

Service层-实现类

public class BookServiceImpl implements BookService{
    @Autowired
    private BookMapperImpl bookMapperImpl;

    @Override
    public Book getBookById(Integer bookID) {
        return bookMapperImpl.getBookById(bookID);
    }
}

测试

ApplicationContext context=new ClassPathXmlApplicationContext("spring-dao.xml");
        BookService bookServiceImpl = context.getBean("bookServiceImpl", BookService.class);
        Book book = bookServiceImpl.getBookById(2);
        System.out.println(book);

优化:SqlSessionDaoSupport

mybatis-spring1.2.3版以上的才有这个

1.修改dao层实现类

public class BookMapperImpl extends SqlSessionDaoSupport implements BookMapper {
    @Override
    public Book getBookById(Integer bookID) {
        SqlSession sqlSession = getSqlSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        return mapper.getBookById(bookID);
    }
}

2.注册bean实现(dao接口实现类:BookMapperImpl)

<bean id="bookMapper" class="com.ylzl.mapper.BookMapperImpl">
  <!-- 注意这行代码 -->
  <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

3.删除

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
posted @ 2024-03-27 22:11  御灵之灵  阅读(100)  评论(0编辑  收藏  举报