This article mainly introduces how to integrate MyBatis and spring together, I use mybatis3.05 + spring3.1.0m2, using DBCP as the database connection pool.
1. Writing The data Access Interface (Userdao.java)
Package com.mybatis;
Public interface Userdao {
public int countall ();
}
2. Writing the Data Provider mapping file (Userdaomapper.xml)
<?xml version= "1.0" encoding= "UTF-8"?>
<mapper namespace= "Com.mybatis.UserDao" >
<select id= "Countall" resulttype= "int" >
Select COUNT (*) c from user;
</select>
</mapper>
3. Writing the MyBatis configuration file (mybatis-configuration.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= "Com/mybatis/userdaomapper.xml"/>
</mappers>
</configuration>
4. Writing the service Layer Interface (Userservice.java)
Package com.mybatis;
Public interface UserService {
public int countall ();
}
5. Writing the service Layer Implementation Code (USERSERVICEIMPL.JAVA)
Package com.mybatis;
public class Userserviceimpl implements UserService {
Private Userdao Userdao;
Public Userdao Getuserdao () {
return Userdao;
}
public void Setuserdao (Userdao Userdao) {
This.userdao = Userdao;
}
public int Countall () {
return This.userDao.countAll ();
}
}
6. Writing the spring configuration file (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"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd ">
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "Driverclassname" value= "Com.mysql.jdbc.Driver" ></property>
<property name= "url" value= "Jdbc:mysql://localhost:3306/hlp?useunicode=true&characterencoding=utf-8 &zerodatetimebehavior=converttonull "></property>
<property name= "username" value= "root" ></property>
<property name= "password" value= "1234" ></property>
<property name= "maxactive" value= "></property>"
<property name= "Maxidle" value= "></property>"
<property name= "maxwait" value= "></property>"
<property name= "Defaultautocommit" value= "true" ></property>
</bean>
<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<property name= "configlocation" value= "Classpath:mybatis-configuration.xml" ></property>
<property name= "DataSource" ref= "DataSource"/>
</bean>
<bean id= "Userdao" class= "Org.mybatis.spring.mapper.MapperFactoryBean" >
<property name= "Mapperinterface" value= "Com.mybatis.UserDao" ></property>
<property name= "Sqlsessionfactory" ref= "Sqlsessionfactory" ></property>
</bean>
<bean id= "UserService" class= "Com.mybatis.UserServiceImpl" >
<property name= "Userdao" ref= "Userdao" ></property>
</bean>
</beans>
7. Test code (USERSERVICETEST.JAVA)
Package com.mybatis;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Userservicetest {
@Test
public void Userservicetest () {
ApplicationContext context = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
UserService UserService = (userservice) context.getbean ("UserService");
System.out.println (Userservice.countall ());
}
}
Appendix: Libraries that need to be imported
MyBatis and Spring Integration (profile-based)