mybatis和spring整合

整合思路

  • 1、讓spring管理SqlSessionFactory
  • 2、讓spring管理mapper對(duì)象和dao。
    使用spring和mybatis整合開發(fā)mapper代理及原始dao接口。
    自動(dòng)開啟事務(wù),自動(dòng)關(guān)閉 sqlsession.
  • 3、讓spring管理數(shù)據(jù)源( 數(shù)據(jù)庫連接池)

整合結(jié)構(gòu)

框架

導(dǎo)jar包

  • mybatis框架的包
  • spring框架的包
  • 數(shù)據(jù)庫驅(qū)動(dòng)包
  • mybatis和spring的整合包

配置文件

Mybatis的配置文件SqlMapConfig.xml

  • 只配置別名、settings、mappers,數(shù)據(jù)源不在這里配置

    <!DOCTYPE configuration
      PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    <!-- 定義 別名 -->
      <typeAliases>
      <!-- 批量別名定義 指定包路徑,自動(dòng)掃描包下邊的pojo,定義別名,別名默認(rèn)為類名(首字母小寫或大寫) -->
          <package name="po" />
      </typeAliases>
    
      <mappers>
      <!-- 加載原始dao使用的user.xml -->
          <mapper resource="config/sqlmap/User.xml"/>
      <!--讀取mapper下的xml文件-->
          <package name="mapper" />
      </mappers>
    
    </configuration>
    

applicationContext.xml

1、數(shù)據(jù)源(dpcp連接池)
2、SqlSessionFactory
3、mapper或dao

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ..........等約束
">

<!-- 加載配置文件 -->
<context:property-placeholder location="classpath:config/db.properties"  />
<!-- 數(shù)據(jù)源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxActive" value="10" />
    <property name="maxIdle" value="5" />
</bean>


<!-- SqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 數(shù)據(jù)源 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- mybatis配置文件 -->
    <property name="configLocation" value="classpath:config/mybatis/SqlMapConfig.xml"/>
</bean>

<!-- 配置dao -->
<bean id="userDao" class="dao.UserDaoImpl">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

<!-- 配置mapper,逐個(gè)表編寫
    MapperFactoryBean:用于生成mapper代理對(duì)象
 -->
 <!--<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
    <!--<property name="mapperInterface" value="mapper.UserMapper"/>-->
    <!--<property name="sqlSessionFactory" ref="sqlSessionFactory"/>-->
<!--</bean>-->

<!--
MapperScannerConfigurer:mapper的掃描器,將包下邊的mapper接口自動(dòng)創(chuàng)建代理對(duì)象,自動(dòng)創(chuàng)建到spring容器中,bean的id是mapper的類名(首字母小寫)
 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 配置掃描包的路徑
    如果要掃描多個(gè)包,中間使用半角逗號(hào)分隔
     -->
    <property name="basePackage" value="mapper"/>
    <!-- 使用sqlSessionFactoryBeanName -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    
</bean>
</beans>

測(cè)試(以測(cè)試dao為例)

package test.dao;

import dao.UserDao;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
mport org.springframework.context.support.ClassPathXmlApplicationContext;
import po.User;

public class UserDaoImplTest {

private ApplicationContext applicationContext;

@Before
public void setUp(){
    //創(chuàng)建spring 容器
    applicationContext = new ClassPathXmlApplicationContext("config/spring/applicationContext.xml");
}
@Test
public void testFindUserById(){
    //從spring容器中獲取UserDao這個(gè)bean
    UserDao userDao = (UserDao) applicationContext.getBean("userDao");
    User userById = userDao.findUserById(1);
   System.out.println(userById);applicationContext.getBean("userById");

 //測(cè)試mapper
   UserMapper userMapper = (UserMapper) 
    User userById = userMapper.findUserById(1);
  }
}

原始dao整合

  • 使用SqlsessionTemplate或SqlSessionDaoSupport類在DAO實(shí)現(xiàn)類中注入SqlSessionFactory來創(chuàng)建SqlSession。
  • 開發(fā)dao(編寫UserDao 、UserDaoImpl)
  • 配置dao (在applicationContext.xml配置)
  • 接口測(cè)試


    UserDao
UserDaoImpl
dao配置

dao整合的目錄結(jié)構(gòu)圖

整合mapper

  • 基于MapperFactoryBean整合
  • 基于MapperScannerConfigurer整合
  • 編寫配置文件
  • 編寫Usermapper.java Usermapper.xml


    mapper整合的目錄結(jié)構(gòu)圖
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容