javascript
Spring与mybatis整合---Mybatis学习笔记(十一)
實現mybatis與spring進行整合,通過spring管理SqlSessionFactory、mapper接口。
mybatis與spring整合jar
mybatis官方提供與mybatis與spring整合jar包:
還包括其它jar:
spring3.2.0
mybatis3.2.7
dbcp連接池
數據庫驅動
參考:
mybatis與spring整合全部jar包
Mybatis配置文件
在classpath下創建mybatis/SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><mappers><mapper resource="sqlmap/User.xml" /><mapper resource="mapper/UserMapper.xml" /></mappers> </configuration>Spring配置文件
在classpath下創建applicationContext.xml,定義數據庫鏈接池、SqlSessionFactory。
<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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "><!-- 加載配置文件 --><context:property-placeholder location="classpath:db.properties" /><!-- 數據庫連接池 --><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><!-- mapper配置 --><!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 數據庫連接池 --><property name="dataSource" ref="dataSource" /><!-- 加載mybatis的全局配置文件 --><property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /></bean></beans>注意:在定義sqlSessionFactory時指定數據源dataSource和mybatis的配置文件。
1.原始dao開發(和spring整合后)
1.User.xml映射文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace :命名空間,用于隔離sql語句,后面會講另一層非常重要的作用。 --> <mapper namespace="test"><select id="findUserById" parameterType="int" resultType="com.huihui.pojo.User">select * from user where id=#{id}</select></mapper>在SqlMapconfig.xml中加載User.xml:
<mappers><mapper resource="sqlmap/User.xml" /></mappers>2.UserDao接口:
public interface UserDao {//根據id查詢用戶信息public User findUserById(int id) throws Exception; }3.UserDao接口的實現類:(需要繼承SqlSessionDaoSupport)
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{@Overridepublic User findUserById(int id) throws Exception {//繼承sqlSessionDaoSupport,通過this.getSqlSession()得到sqlSessionSqlSession sqlSession = this.getSqlSession();User user = sqlSession.selectOne("test.findUserById", id);return user;} }4.在applicationContext.xml配置文件中增加如下內容:
<bean id="userDao" class="com.huihui.dao.UserDaoImpl"><property name="sqlSessionFactory" ref="sqlSessionFactory"></property></bean>5.測試:
public class UserDaoImplTest {private ApplicationContext applicationContext;@Before//在setUp中得到spring的容器public void setUp() throws Exception {applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");}@Testpublic void testFindUserById() throws Exception{UserDao userdao = (UserDao)applicationContext.getBean("userDao");User user = userdao.findUserById(1);System.out.println(user);}}2.mapper代理的開放(與Spring整合后)
1.UserMapper.xml映射文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.huihui.mapper.UserMapper"><select id="findUserById" parameterType="int" resultType="com.huihui.pojo.User">select * from user where id=#{id}</select> </mapper>在SqlMapconfig.xml中加載UserMapper.xml:
<mappers><mapper resource="sqlmap/User.xml" /><mapper resource="mapper/UserMapper.xml"/></mappers>2.mapper接口:
public interface UserMapper {//根據id查詢用戶信息public User findUserById(int id) throws Exception;}3.在applicationContext.xml中通過MapperFactoryBean創建實體對象:
<!-- MapperFactoryBean可以根據mapper接口生成代理對象 --><bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><!-- mapperInterface指定mapper接口 --><property name="mapperInterface" value="com.huihui.mapper.UserMapper"/><property name="sqlSessionFactory" ref="sqlSessionFactory"></property></bean>4.測試:
@Testpublic void testFindUserByIdMapper() throws Exception{UserMapper userMapper = (UserMapper)applicationContext.getBean("userMapper");User user = userMapper.findUserById(1);System.out.println(user);}上面的方法存在的問題:
需要針對每個mapper進行配置,麻煩。
解決方案:
通過MapperScannerConfigurer進行mapper掃描。
此方法即mapper接口開發方法,只需定義mapper接口,不用編寫mapper接口實現類。只需要在spring配置文件中定義一個mapper掃描器,自動掃描包中的mapper接口生成代代理對象。
1、 mapper.xml文件編寫
2、 定義mapper接口
注意mapper.xml的文件名和mapper的接口名稱保持一致,且放在同一個目錄
3、 配置mapper掃描器
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="mapper接口包地址"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/></bean>basePackage:掃描包路徑,中間可以用逗號或分號分隔定義多個包
4、 使用掃描器后從spring容器中獲取mapper的實現對象
如果將mapper.xml和mapper接口的名稱保持一致且放在一個目錄 則不用在sqlMapConfig.xml中進行配置
總結
以上是生活随笔為你收集整理的Spring与mybatis整合---Mybatis学习笔记(十一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查询缓存---Mybatis学习笔记(十
- 下一篇: SpringMVC架构---Spring