码迷,mamicode.com
首页 > 编程语言 > 详细

spring+mybatis

时间:2017-09-30 17:56:55      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:doc   别名   不用   www.   name   private   employee   man   let   

 

applicationContext-mybatis.xml 配置文件(spring,mybatis集成的配置文件)

<?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"
       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.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">


	<!-- 读取配置文件  两种方法 
	<context:property-placeholder location="classpath:jdbc.properties" />-->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
	    <property name="locations">    
	        <list>    
	            <value>classpath*:jdbc.properties</value>    
	        </list>    
	    </property>    
	</bean> 
	
	<!--  配置数据源的两种方法 JNDI获取数据源(DBCP连接池) 
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
		
	</bean>-->
	
	<bean id="dataSource" destroy-method="close"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${driver}"/>
		<property name="jdbcUrl" value="${url}?useUnicode=true&characterEncoding=utf8"/>
		<property name="user" value="${username}"/>
		<property name="password" value="${password}"/>
		
	</bean>
	
	  
	<!-- 事务管理 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- 配置 MyBatis sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
		<property name="mapperLocations" value="classpath:dao/*.xml"></property>
	</bean>
	
	<!-- 配置sqlSessionTemplate模板-->
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<!-- 构造器进行注入 -->
		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
		
	</bean>
	
	<!-- 把sqlSessionTemplate模板 注入到测试类 -->
	<bean id="empDaoTest" class="test.empTest">
		<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
	</bean>
	




           
</beans>

  

jdbc.propertis配置文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/testdb
username=root
password=123

  

mybatis-config.xml配置文件(mybatis配置文件)

<?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>
      <!-- 别名 以后pojo包下的实体类 可以用类名 不用完全限定名了 --> <typeAliases> <package name="pojo"/> </typeAliases> </configuration>

  

 

junit测试类

public class empTest {
	private Logger logger=Logger.getLogger(this.getClass());
	private SqlSessionTemplate sqlSessionTemplate;
	protected ApplicationContext applicationContext=null;
	private empTest test;
	
	@Before
	public void setuo(){
		applicationContext=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
		test=(empTest) applicationContext.getBean("empDaoTest");
	}
	
	@Test
	public void tests(){
		
		
		List<Employee> emps=test.sqlSessionTemplate.selectList("dao.EmployeeMapper.getEmployeeList");
		for(Employee emp:emps){
			logger.debug(emp.getName());
		}
		logger.debug(emps.size());
		
	}

  

 

 

 

第一种配置用的是 SqlSessionTemplate模版 

第二种配置用的是 MapperFactoryBean

======================第二种配置方法========================

 

applicationContext-mybatis.xml 配置文件(spring,mybatis集成的配置文件)

<?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"
       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.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">


	<!-- 读取配置文件  两种方法 
	<context:property-placeholder location="classpath:jdbc.properties" />-->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
	    <property name="locations">    
	        <list>    
	            <value>classpath*:jdbc.properties</value>    
	        </list>    
	    </property>    
	</bean> 
	
	<!--  配置数据源的两种方法 JNDI获取数据源(DBCP连接池) 
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
		
	</bean>-->
	
	<bean id="dataSource" destroy-method="close"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${driver}"/>
		<property name="jdbcUrl" value="${url}?useUnicode=true&characterEncoding=utf8"/>
		<property name="user" value="${username}"/>
		<property name="password" value="${password}"/>
		
	</bean>
	
	  
	<!-- 事务管理 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>


	<!-- 配置 MyBatis 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="employeeMapperFactory"  class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
		<property name="mapperInterface" value="dao.EmployeeMapper"></property>
	</bean> 
	
	<bean id="employeeServiceImpl" class="service.EmployeeServiceImpl">
		<property name="employeeMapper" ref="employeeMapperFactory"></property>
	</bean>
	

           
</beans>

  

其他配置文件不变

junit测试类

 

public class empTest {
	private Logger logger=Logger.getLogger(this.getClass());
	protected ApplicationContext applicationContext=null;
	private EmployeeServiceImpl employeeServiceImpl;
	@Before
	public void setuo(){
		applicationContext=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
		 employeeServiceImpl=(EmployeeServiceImpl) applicationContext.getBean("employeeServiceImpl");
	}
	
	@Test
	public void tests(){
		
		Department department=new Department();
		department.setId(2);
		List<Employee> emplist=employeeServiceImpl.getEmployeelistByDeptId(department);
		for(Employee emp:emplist){
			logger.debug(emp.getName());
			
		}
		
	}

  

 

spring+mybatis

标签:doc   别名   不用   www.   name   private   employee   man   let   

原文地址:http://www.cnblogs.com/m97i/p/7615409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!