The eclipse used here first creates a dynamic Web project.
1. Import the jar packages and versions of the spring IOC, AOP, DAO, DBCP, Dbdrive, Mybatis.jar, Mybatis-spring.jar I use as follows:
Aopalliance.jar
Aspectjweaver.jar
Commons-dbcp-1.4.jar
Commons-logging.jar
Commons-pool-1.5.6.jar
Mybatis-3.2.5.jar
Mybatis-spring-1.2.2.jar
Mysql-connector-java-5.1.7-bin.jar
Spring-aop-4.1.6.release.jar
Spring-aspects-4.1.6.release.jar
Spring-beans-4.1.6.release.jar
Spring-context-4.1.6.release.jar
Spring-core-4.1.6.release.jar
Spring-expression-4.1.6.release.jar
Spring-jdbc-4.1.6.release.jar
Spring-tx-4.1.6.release.jar
Import two jar packages of SPRINGMVC if you need to
Spring-web-4.1.6.release.jar
Spring-webmvc-4.1.6.release.jar
2. Migrating the MyBatis Master profile to spring Application.xml
My application template is as follows:
<?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:jdbc= "Http://www.springframework.org/schema/jdbc"
Xmlns:jee= "Http://www.springframework.org/schema/jee"
xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
Xmlns:mvc= "Http://www.springframework.org/schema/mvc"
Xmlns:util= "Http://www.springframework.org/schema/util"
Xmlns:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-4.1.xsd ">
</beans>
On the inside first configure the DataSource and Sqlsessionfactorybean configuration as follows:
<!--inject Connection pool--
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "username" value= "root" ></property>//must write value
<property name= "Password" value= "root" ></property>
<property name= "url" value= "JDBC:MYSQL://127.0.0.1:3306/MYDB1" ></property>
<property name= "Driverclassname" value= "Com.mysql.jdbc.Driver" ></property>//cannot be written Driver
</bean>
<!--injected Sqlsessionfactorybean class can be entered in the test class, alt+/can be prompted to easily find the path--
<bean id= "SSF" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<!--Specify a connection resource--
<property name= "DataSource" ref= "DataSource" ></property>
<!--Specify a mapping file--
<property name= "mapperlocations" value= "classpath:com/***/mapper/*.xml" ></property>//file path
</bean>
Then write a test class to test whether the sqlsessionfactory can be loaded
The main code is as follows:
public class Testsqlsessionfactory {
@Test
public void Test1 () {
ApplicationContext AC =
New Classpathxmlapplicationcontext ("Applicationcontext.xml");
System.out.println (Ac.getbean ("SSF"));
}
}
Error proof configuration information is correct, can be followed by operation.
Then configure Mapperfactorybean
The configuration is as follows:
<bean id= "Mapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" >
<property name= "Mapperinterface" value= "Com.xdl.mapper.EmpMapper" ></property>
<property name= "Sqlsessionfactory" ref= "SSF" ></property>
</bean>
Configuration complete
3, MyBatis in the Mapper mapping file and mapper interface and the same as the original writing
The mapping file template is as follows
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Mapper Public "-//ibatis.apache.org//dtd mapper 3.0//en"
"Http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd" >
<mapper namespace= "Com.xdl.mapper.EmpMapper" >
<!--Find all employees-
<select id= "FindAll" resulttype= "COM.XDL.BEAN.EMP" >
SELECT * FROM emp
</select>
</mapper>
The interface is as follows
Package com.***l.mapper;
Import java.util.List;
Import COM.XDL.BEAN.EMP;
Public interface Empmapper {
List<emp> FindAll ();
}
4. Get the implementation class of the Mapper mapper from the spring container, get it through the interface name
The simple code is as follows;
ApplicationContext AC =
New Classpathxmlapplicationcontext ("Applicationcontext.xml");
Empmapper em = Ac.getbean ("Mapper", Empmapper.class);
System.out.println (Em.findall ());
At this point, a simple spring+mybatis is integrated
Simple construction of Spring+mybatis framework in Java Web development