Spring整合Mybatis方式二 - SqlSessionTemplate

御灵之灵發表於2024-03-27

前置工作

  • 導包: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);

相關文章