Mybatis learning-Integration of spring and Mybatis, and mybatis-spring

Source: Internet
Author: User

Mybatis learning-Integration of spring and Mybatis, and mybatis-spring

  • Introduction

When writing the test code earlier, both the original dao and Mapper interface development have a lot of repeated code. Integrating spring and mybatis can reduce this duplicate code, use the spring template method mode to encapsulate these repeated codes, such as obtaining SqlSessionFactory, SqlSession, and closing SqlSession. We only need to implement specific business processing. In addition, spring also uses its IOC to put Dao or Mapper interfaces into the container for management, better achieving decoupling.

  • Integration of Spring and MyBatis

  1. Integration ideas:

Spring needs to manage SqlSessionFactory in a singleton mode. Spring and mybatis are integrated to generate proxy objects and use SqlSessionFactory to create SqlSession. (Spring and mybatis are automatically integrated) The er of the persistent layer must be managed by spring.

2. Integrated Environment

 

Create a new java project and import the jar packages integrated with Mybatis and Spring into the project.

3. sqlSessionFactory

 

Configure sqlSessionFactory and data source in applicationContext. xml:

 

1 <! -- Load the configuration file --> 2 <context: property-placeholder location = "classpath: db. properties"/> 3 4 <! -- Data source, use dbcp --> 5 <bean id = "dataSource" class = "org. apache. commons. dbcp. basicDataSource "6 destroy-method =" close "> 7 <property name =" driverClassName "value =" $ {jdbc. driver} "/> 8 <property name =" url "value =" $ {jdbc. url} "/> 9 <property name =" username "value =" $ {jdbc. username} "/> 10 <property name =" password "value =" $ {jdbc. password} "/> 11 <property name =" maxActive "value =" 10 "/> 12 <property name =" maxId Le "value =" 5 "/> 13 </bean> 14 15 16 <! -- SqlSessinFactory --> 17 <bean id = "sqlSessionFactory" class = "org. mybatis. spring. SqlSessionFactoryBean"> 18 <! -- Load the configuration file of mybatis --> 19 <property name = "configLocation" value = "mybatis/SqlMapConfig. xml"/> 20 <! -- Data source --> 21 <property name = "dataSource" ref = "dataSource"/> 22 </bean>

 

  • Original Dao Development

 1. The others are basically the same, that is, the Dao implementation class has changed:

1 public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {2 @ Override3 public User findUserById (int id) throws Exception {4 SqlSession sqlSession = this. getSqlSession (); // obtain sqlSession5 User user = sqlSession. selectOne ("user. findUserById ", id); 6 return user; 7} 8}

The implementation class inherits SqlSessionDaoSupport. spring uses the template method to encapsulate sqlSessionFactory, sqlSession, transaction management, and sqlSession closure. We only need to take charge of the business logic.

2. Configure dao

Configure dao in applicationContext. xml.

 

1 <! -- Configure userDaoImpl --> 2 <bean id = "userDao" class = "com. luchao. mybatis. first. daoimpl. userDaoImpl "> 3 <property name =" sqlSessionFactory "ref =" sqlSessionFactory "/> 4 </bean>

 

3. Test Procedure

1 public class MyBatis_mapper_test {2 private ApplicationContext applicationContext; 3 @ Before 4 public void init () throws IOException {5 this. applicationContext = new ClassPathXmlApplicationContext ("classpath: spring/applicationContext. xml "); 6} 7 @ Test 8 public void testFindUserById () throws Exception {9 // create UserMapper object 10 UserMapper userMapper = (UserMapper) applicationContext. getBean ("userMapper"); 11 // call userMapper's Method 12 User user = userMapper. findUserById (10); 13 // print customer information 14 System. out. println (user); 15} 16}
  • Er Agent Development

  1. Configure the ER interface in Spring

(1) Use org. mybatis. spring. mapper. MapperFactoryBean to generate a proxy object based on the mapper interface.

1 <bean id = "" class = "org. mybatis. spring. mapper. mapperFactoryBean "> 2 <property name =" mapperInterface "value =" mapper interface address "/> 3 <property name =" sqlSessionFactory "ref =" sqlSessionFactory "/> 4 </bean>

It is troublesome to configure each er.

(2) er scanning through MapperScannerConfigurer

 

1 <bean class = "org. mybatis. spring. mapper. mapperScannerConfigurer "> 2 <property name =" basePackage "value =" mapper interface package address "> </property> 3 <property name =" sqlSessionFactoryBeanName "value =" sqlSessionFactory "/> 4 </bean>

 

BasePackage: Scan package path. Multiple packages can be defined by commas or semicolons.

In this way, the mapper. xml file name is consistent with the mapper Interface Name and placed in the same directory. If you keep the names of mapper. xml and mapper interfaces consistent and put them in a directory, you do not need to configure them in sqlMapConfig. xml.

It can be seen that the integration of Mybatis into Spring can reduce the writing of template code, and put Dao and Mapper into the Spring container through configuration, which can achieve code decoupling.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.