1. Integration Ideas
Spring is required to manage sqlsessionfactory in a single case.
Spring and MyBatis Consolidate build proxy objects and use Sqlsessionfactory to create sqlsession. (Spring and MyBatis Integration Auto-complete)
The mapper of the persistence layer needs to be managed by spring.
2. Building the integrated environment
A. Adding a jar package to MyBatis
B. Adding a spring jar package
C.mybatis and spring's integration pack mybatis-spring-1.2.3
http://mvnrepository.com/artifact/org.mybatis/mybatis-spring/1.2.3
D.mysql Driver Package
E.junit Bag
F.DBCP Connection Pool Package:
http://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi
Commons pool:
http://commons.apache.org/proper/commons-pool/download_pool.cgi
3. Writing the configuration file
4.sqlSessionFactory
Configuring Sqlsessionfactory and data sources in Applicationcontext.xml
Sqlsessionfactory under the integration package for MyBatis and spring:
<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= "Maxidle" value= "5"/>
</bean>
5. Development of the original DAO (and spring Integration)
Mapper.xml
DAO interface
The Applicationcontext.xml configuration is as follows:
<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-4.1.xsd
http://www.springframework.org/ SCHEMA/MVC
Http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/ Schema/context
Http://www.springframework.org/schema/context/spring-context-4.1.xsd
/http// WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP
Http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
/http// WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX
Http://www.springframework.org/schema/tx/spring-tx-4.1.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= "Maxidle" value= "5"/>
</bean>
<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<!--load MyBatis configuration file--
<property name= "configlocation" value= "Sqlmapconfig.xml"/>
<!--data Source-
<property name= "DataSource" ref= "DataSource"/>
</bean>
<bean id= "Userdao" class= "DAO. Userdaoimpl ">
<property name= "Sqlsessionfactory" ref= "Sqlsessionfactory"/>
</bean>
</beans>
Test the program and test it successfully.
Package DAO;
Import Org.junit.Test;
public class Userdaoimpltest {
Private ApplicationContext ApplicationContext;
@Before
public void SetUp () throws Exception {
ApplicationContext = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
}
@Test
public void Testfinduserbyid () throws Exception {
Userdao Userdao = (Userdao) applicationcontext.getbean ("Userdao");
User user = Userdao.finduserbyid (1);
SYSTEM.OUT.PRINTLN (user);
}
}
MyBatis Learning 24-mybatis and Spring integration