This article explains the integration of spring+mybatis.
Directory structure:
First method of integrating spring
1. New java project: spring_mybatis
2. Import the jar package -- the spring and mybatis package, and then build
aopalliance.jar aspectjweaver.jar commons-logging.jar mybatis-3.2.7.jar mybatis-spring-1.2.3.jar mysql-connector-java-5.1.20-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-orm-4.1.6.RELEASE.jar spring-tx-4.1.6.RELEASE.jar spring-web-4.1.6.RELEASE.jar spring-webmvc-4.1.6.RELEASE.jar
3. Write vo class
User.java under cn.vincent.vo
1 package cn.vincent.vo; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 7 private int id; 8 private String name; 9 private int age; 10 private int rileId; 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public int getAge() { 24 return age; 25 } 26 public void setAge(int age) { 27 this.age = age; 28 } 29 public int getRileId() { 30 return rileId; 31 } 32 public void setRileId(int rileId) { 33 this.rileId = rileId; 34 } 35 @Override 36 public String toString() { 37 return "User [id=" + id + ", name=" + name + ", age=" + age 38 + ", rileId=" + rileId + "]"; 39 } 40 41 42 }
4. Write mapping files
UserMapper.xml under cn.vincent.mapper
<?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="cn.vincent.mapper.UserMapper"> <select id="findAll" resultType="User"> select * from t_user </select> </mapper>
5. writing dao
UserMapper.java under cn.vincent.mapper
package cn.vincent.mapper; import java.util.List; import cn.vincent.vo.User; public interface UserMapper { public List<User> findAll(); }
UserMapperImpl.java under cn.vincent.mapper.impl
package cn.vincent.mapper.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import cn.vincent.mapper.UserMapper; import cn.vincent.vo.User; @Repository("userMapper") public class UserMapperImpl implements UserMapper { @Autowired private SqlSession sqlSession; public void setSqlSession(SqlSession sqlSession){ this.sqlSession=sqlSession; } @Override public List<User> findAll() { return sqlSession.selectList("cn.vincent.mapper.UserMapper.findAll"); } }
6. Write service
UserService.java under cn.vincent.service
package cn.vincent.service; import java.util.List; import cn.vincent.vo.User; public interface UserService { public List<User> findAll(); }
UserService Impl. Java under cn.vincent.service
package cn.vincent.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.vincent.mapper.UserMapper; import cn.vincent.service.UserService; import cn.vincent.vo.User; @Service("userService") public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } @Override public List<User> findAll() { return userMapper.findAll(); } }
7. Write mybatis configuration file
mybatis.cfg.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="cn.vincent.vo"/> </typeAliases> <mappers> <mapper resource="cn/vincent/mapper/UserMapper.xml"/> </mappers> </configuration>
8. Write configuration files for spring
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--Read external configuration--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </bean> <!-- according to mybatis Configuration files to create sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis.cfg.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean> <!-- Transaction Manager --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- Transaction notification --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- Represented by save The way to start requires transactions propagation Represents the propagation characteristics of transactions REQUIRED Check to see if there are currently transactions, if so, use the current transaction, if no new transaction is opened --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <!--expression Identify where transactions work //First* Represents all return values //The second* Represents all classes //Third* Represents all methods in a class .. Represents all parameters --> <aop:pointcut expression="execution(* cn.vincent.service.impl.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> <context:component-scan base-package="cn.vincent"></context:component-scan> </beans>
jdbc.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password=root
9. Adding Tests
UserService Test. Java under cn.vincent.service under test
package cn.vincent.service; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.vincent.vo.User; public class UserServiceTest { @Test public void testFindAll(){ ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=ac.getBean(UserService.class); List<User> list=userService.findAll(); for(User u:list){ System.out.println(u); } } }
10. Running test
The results are as follows:
The second is to remove the configuration method of mybatis configuration file.
Modify the configuration of SqlSessionFactory in beans.xml
<!-- according to mybatis Configuration files to create sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="cn.vincent.vo"></property> <property name="mapperLocations"> <list> <value>classpath:cn/vincent/mapper/UserMapper.xml</value> </list> </property> </bean>
3. Configuration of implementation classes can be generated by proxy
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="1111"/> </bean> <!-- according to mybatis Configuration files to create sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="cn.sxt.vo"></property> <property name="mapperLocations"> <list> <value>classpath:cn/sxt/mapper/UserMapper.xml</value> </list> </property> </bean> <!-- mapper Interface and mapper The mapping file is in the same package, and mapper Interface name and mapper The mapping file name of mapper Method names and mapper In the mapping file id The same name mapper In the mapping file namespace and mapper The package name of the file+mapper file name //This class scans the specified package and generates the id name of the class generated by the implementation class of the mapper interface through the proxy mapper Interface initials lowercase --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> <property name="basePackage" value="cn.vincent.mapper"></property> </bean> <!-- Transaction Manager --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- Transaction notification --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- Represented by save The way to start requires transactions propagation Represents the propagation characteristics of transactions REQUIRED Check to see if there are currently transactions, if so, use the current transaction, if no new transaction is opened --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <!--expression Identify where transactions work //First* Represents all return values //The second* Represents all classes //Third* Represents all methods in a class .. Represents all parameters --> <aop:pointcut expression="execution(* cn.vincent.service.impl.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> <context:component-scan base-package="cn.vincent"></context:component-scan> </beans>
github address: https://github.com/Vincent-yuan/spring_mybatis