1. 程式人生 > >mybatis和spring的整合

mybatis和spring的整合

現有需求:要求查詢user表的所有使用者資訊。

jar包準備:

依照資料庫的表寫好user實體類,略。

 dao層書寫:

準備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.bdqn.dao.UserMapper">
	<select id="queryUserList" resultType="user">
		select * from easybuy_user
	</select>
</mapper>

書寫介面:

public interface UserMapper {
	public List<User>queryUserList();
}

 service層:

首先是UserService介面:

public interface UserService {
	public List<User>getUserList();
}

它的實現類,UserServiceImpl:

public class UserServiceImpl implements UserService {
	private UserMapper userDao;

	public UserMapper getUserDao() {
		return userDao;
	}

	public void setUserDao(UserMapper userDao) {
		this.userDao = userDao;
	}

	public List<User> getUserList() {
		// TODO Auto-generated method stub		
		return userDao.queryUserList();
	}
	
}

它有一個屬性,UserMapper。

建立resource資料夾,準備配置檔案,資料庫配置檔案,database.properties。mybatis的配置檔案Mybatis-config.xml,spring的配置檔案ApplicationContext.xml。

Mybatis-config.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> 
<typeAliases>
	<package name ="com.bdqn.entity" />
</typeAliases>
	<mappers>
		<mapper resource="com/bdqn/dao/UserMapper.xml"/>
	</mappers>
</configuration>

方式一:在dao層增加UserMapperImpl實現類。這種方式符合我們以前的思維方式,通過建立實現類來建立物件,因為介面是不能例項化的,而我們又需要在service層引用該例項物件。

UserMapperImpl:

public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSession;
	public SqlSessionTemplate getSqlSession() {
	return sqlSession;
}
public void setSqlSession(SqlSessionTemplate sqlSession) {
	this.sqlSession = sqlSession;
}
	public List<User> queryUserList() {
		// TODO Auto-generated method stub
		List<User> queryUserList = sqlSession.getMapper(UserMapper.class).queryUserList();
		return queryUserList;
	}
}

 sqlSessionTemplate負責管理MyBatis的SqlSession,呼叫SQL對映語句,實現對資料庫的訪問。由mybatis-spring.1.2.0.jar提供。可以理解是sqlSession。

Spring的ApplicationContext.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"    
       xmlns:p="http://www.springframework.org/schema/p" 
       xmlns:aop="http://www.springframework.org/schema/aop"  
       xmlns:context="http://www.springframework.org/schema/context" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.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.xsd">
    	   <!-- 匯入資料庫配置檔案 -->
    	<context:property-placeholder location="classpath:database.properties"/>
    	<!-- dbcp資料庫連線池 -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<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> 
    	<!-- sqlSessionFactory -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"></property>
    		<property name="configLocation" value="classpath:Mybatis-config.xml"></property> 
    	</bean> 
    	<!-- sqlSession -->
    	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    	</bean> 
    	<bean id="userDao" class="com.bdqn.dao.UserMapperImpl">
    		<property name="sqlSession" ref="sqlSession"></property>
    	</bean> 	
    	<bean id="userService" class="com.bdqn.service.UserServiceImpl">
    		<property name="userDao" ref="userDao"></property>
    	</bean>  
</beans>

 sqlSessionFactoryBean由mybatis-spring.1.2.0.jar提供。可以理解是sqlSessionFactory。

這種方式不會採用,通過建立實現類多次一舉。

方式二:

採用資料對映器(MapperFactoryBean)的方式完成對資料庫操作 根據Mapper介面獲取Mapper物件,它封裝了原有的SqlSession.getMapper()功能的實現

Spring的ApplicationContext.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"    
       xmlns:p="http://www.springframework.org/schema/p" 
       xmlns:aop="http://www.springframework.org/schema/aop"  
       xmlns:context="http://www.springframework.org/schema/context" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.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.xsd">
    	   <!-- 匯入資料庫配置檔案 -->
    	<context:property-placeholder location="classpath:database.properties"/>
    	<!-- dbcp資料庫連線池 -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<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> 
    	<!-- sqlSessionFactory -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"></property>
    		<property name="configLocation" value="classpath:Mybatis-config.xml"></property> 
    	</bean> 
    	<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.bdqn.dao.UserMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
		</bean>
    	<bean id="userService" class="com.bdqn.service.UserServiceImpl">
    		<property name="userDao" ref="userDao"></property>
    	</bean>  
</beans>

不用建立介面的實現類。因為使用MapperFactoryBean建立的代理類實現了 UserMapper 介面,並且注入到應用程式中。它有兩個屬性需要注入:1.mapperInterface,指定對映器,只能是介面型別。2.sqlSessionFactory,注入SqlSessionFactory以提供SqlSessionTemplate例項。因此原先的如下程式碼:

<!-- sqlSession -->
    	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    	</bean> 
    	<bean id="userDao" class="com.bdqn.dao.UserMapperImpl">
    		<property name="sqlSession" ref="sqlSession"></property>
    	</bean> 	

就可以刪除了。

方式三: MapperScannerConfigurer

 採用方式二有一個問題:若對映器很多的話,相應的配置項也會很多。

Spring的ApplicationContext.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"    
       xmlns:p="http://www.springframework.org/schema/p" 
       xmlns:aop="http://www.springframework.org/schema/aop"  
       xmlns:context="http://www.springframework.org/schema/context" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.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.xsd">
    	   <!-- 匯入資料庫配置檔案 -->
    	<context:property-placeholder location="classpath:database.properties"/>
    	<!-- dbcp資料庫連線池 -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<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> 
    	<!-- sqlSessionFactory -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"></property>
    		<property name="configLocation" value="classpath:Mybatis-config.xml"></property> 
    	</bean> 
		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
			<property name="basePackage" value="com.bdqn.dao"></property>
		</bean>
    	<bean id="userService" class="com.bdqn.service.UserServiceImpl">
    		<property name="userDao" ref="userMapper"></property>
    	</bean>  
</beans>

MapperScannerConfigurer自動掃描指定包下的Mapper介面,並將它們直接註冊為MapperFactoryBean

MapperScannerConfigurer遞迴掃描基準包下所有介面,若它們在SQL對映檔案中定義過,則動態註冊為MapperFactoryBean,如此即可批量產生對映器實現類。

basePackage屬性:指定掃描的基準包

測試類:

public class Test1 {
	@Test
	public void test() {
	ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
	UserServiceImpl us = (UserServiceImpl) ctx.getBean("userService");
	List<User> userList = us.getUserList();
	for (User user : userList) {
		System.out.println("使用者名稱:"+user.getLoginName());
	}
	}
}

測試結果: