In front of the test code, whether based on the original DAO or Mapper interface development have a lot of duplicate code, the spring and MyBatis integration can reduce this duplication of code, through the spring template method pattern, the repeated code is encapsulated, such as: Get Sqlsessionfactory, sqlsession, sqlsession Close, we only need to achieve specific business processing. In addition, Spring uses its IOC to manage the placement of DAO or mapper interfaces into containers for better understanding of decoupling.
- Spring and MyBatis Integration
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 is required by the Spring to manage.
2. Integrated environment
Create a new Java project to import MyBatis and spring-integrated jar packages into the project.
3, Sqlsessionfactory
Configure sqlsessionfactory and data sources in applicationcontext.xml :
1 <!--Load configuration file -2 <Context:property-placeholder Location= "Classpath:db.properties" />3 4 <!--data source, using DBCP -5 <BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource"6 Destroy-method= "Close">7 < Propertyname= "Driverclassname"value= "${jdbc.driver}" />8 < Propertyname= "url"value= "${jdbc.url}" />9 < Propertyname= "username"value= "${jdbc.username}" />Ten < Propertyname= "Password"value= "${jdbc.password}" /> One < Propertyname= "Maxactive"value= "Ten" /> A < Propertyname= "Maxidle"value= "5" /> - </Bean> - the - <!--sqlsessinfactory - - <BeanID= "Sqlsessionfactory"class= "Org.mybatis.spring.SqlSessionFactoryBean"> - <!--load the MyBatis configuration file - + < Propertyname= "Configlocation"value= "Mybatis/sqlmapconfig.xml" /> - <!--Data Source - + < Propertyname= "DataSource"ref= "DataSource" /> A </Bean>
1, the other is basically the same, is the DAO implementation class has changed:
1 Public classUserdaoimplextendsSqlsessiondaosupportImplementsuserdao{2 @Override3 PublicUser Finduserbyid (intIdthrowsException {4Sqlsession sqlsession = This. Getsqlsession ();//Get sqlsession5User user = Sqlsession.selectone ("User.finduserbyid", id);6 returnuser;7 }8}
The implementation class inherits Sqlsessiondaosupport so that spring encapsulates the acquisition sqlsessionfactory, get sqlsession, transaction management, shutdown sqlsession through the template method, We just have to be responsible for the business logic.
2. Configure DAO
Configure daoin applicationcontext.xml .
1 <!--Configure Userdaoimpl -2 <BeanID= "Userdao"class= "Com.luchao.mybatis.first.daoimpl.UserDaoImpl">3 < Propertyname= "Sqlsessionfactory"ref= "Sqlsessionfactory" />4 </Bean>
3. Test procedure
1 Public classMybatis_mapper_test {2 PrivateApplicationContext ApplicationContext;3 @Before4 Public voidInit ()throwsIOException {5 This. ApplicationContext =NewClasspathxmlapplicationcontext ("Classpath:spring/applicationcontext.xml");6 }7 @Test8 Public voidTestfinduserbyid ()throwsException {9 //Create a Usermapper objectTenUsermapper Usermapper = (usermapper) applicationcontext.getbean ("Usermapper"); One //methods for calling Usermapper AUser user = Usermapper.finduserbyid (10); - //Print Customer Information - System.out.println (user); the } -}
1. Configuring the Mapper interface in spring
(1), using Org.mybatis.spring.mapper.MapperFactoryBean, generate proxy object according to mapper interface
1 <BeanID=""class= "Org.mybatis.spring.mapper.MapperFactoryBean">2 < Propertyname= "Mapperinterface"value= "Mapper interface Address"/>3 < Propertyname= "Sqlsessionfactory"ref= "Sqlsessionfactory"/>4 </Bean>
It needs to be configured for each mapper , which is more cumbersome.
(2), through Mapperscannerconfigurer for mapper scanning
1 <Beanclass= "Org.mybatis.spring.mapper.MapperScannerConfigurer">2 < Propertyname= "Basepackage"value= "Mapper Interface package Address"></ Property>3 < Propertyname= "Sqlsessionfactorybeanname"value= "Sqlsessionfactory"/>4 </Bean>
Basepackage: Scans the package path, in which multiple packages can be defined by commas or semicolons.
In this way, the mapper.xml filename is consistent with the interface name of the mapper and is placed in the same directory. if the names of the Mapper.xml and mapper interfaces are consistent and placed in a directory, they are not configured in Sqlmapconfig.xml.
It can be seen that integrating MyBatis into spring reduces the writing of the template code and puts DAO and mapper through the configuration into the spring container, which enables code decoupling.
MyBatis Learning--spring and MyBatis integration