To take advantage of MyBatis first is the need to import Mybatis-x.x.x.jar, second, to integrate spring and mybatis need to import Mybatis-spring-x.x.x.jar.
jar:mybatis-x.x.x
<dependency> <groupId>org.mybatis</groupId> <artifactid>mybatis</artifactid > <version>3.2.6</version> </dependency>
jar:mybatis-spring-x.x.x
<dependency> <groupId>org.mybatis</groupId> <artifactid>mybatis-spring</ artifactid> <version>1.2.2</version> </dependency>
1. Spring Integrated mybatis XML configuration
Xml:pom
<!--DB--<dependency> <groupId>com.alibaba</groupId> <a Rtifactid>fastjson</artifactid> <version>1.1.41</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <ARTIFACTID>TOMCAT-SERVL Et-api</artifactid> <version>7.0.54</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> <version>7.0.23</version> </dependency > <dependency> <groupId>mysql</groupId> <artifactid>mysql-connecto r-java</artifactid> <version>5.1.18</version> </dependency> <dependenc Y> <groUpid>org.mybatis</groupid> <artifactId>mybatis-spring</artifactId> <version& gt;1.2.2</version> </dependency> <dependency> <groupid>org.mybatis</gr Oupid> <artifactId>mybatis</artifactId> <version>3.2.6</version> & Lt;/dependency>
Xml:datasource
<bean id= "DataSource" class= "Org.apache.tomcat.jdbc.pool.DataSource" destroy-method= "Close" > <property Name= "Poolproperties" > <bean class= "org.apache.tomcat.jdbc.pool.PoolProperties" > <p Roperty name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "${jdbc.ur L} "/> <property name=" username "value=" ${jdbc.user} "/> <property name=" Passwor D "value=" ${jdbc.password} "/> <!--Register the pool with JMX. In order for the connection pool object to create the MBean. --<property name= "jmxenabled" value= "true"/> <!--the indication of whether O Bjects'll is validated by the Idle object evictor. --<property name= "Testwhileidle" value= "true"/> <!--the indication of whethe R objects'll be validated before being borrowed from the pool. --&gT <property name= "Testonborrow" value= "false"/> <property name= "Testonreturn" value= "false"/> <property name= "InitialSize" value= "${jdbc.initialpoolsize}"/> <property name= "MaxA Ctive "value=" ${jdbc.maxactive} "/> <property name=" maxwait "value=" ${jdbc.maxwait} "/> <property name= "Minidle" value= "${jdbc.minidle}"/> <property name= "Maxidle" value= "${jdbc.ma Xidle} "/> <property name=" MaxAge "value=" 60000 "/> <!--the number of Milliseco NDS to sleep between runs of the idle connection Validation/cleaner thread. --<property name= "Timebetweenevictionrunsmillis" value= "15000"/> <!--the min Imum amount of time an object could sit idle in the pool before it was eligible for eviction. --<property name= "Minevictableidletimemillis" value= "60000"/> <property name= "removeabandoned" value= "true"/> <property name= "Removeabando Nedtimeout "value=" "/> <property name=" validationquery "value=" Select 1 "/> < Property Name= "Validationinterval" value= "30000"/> </bean> </property> </bean>
Common configuration:
(If you are using <mappers> for the configuration of the XML map file in Mybatis-config.xml, you can not configure the following mapperlocation attribute)
<!--mybatis file configuration, scan all mapper files--><bean id= "Sqlsessionfactory" class= " Org.mybatis.spring.SqlSessionFactoryBean " p:datasource-ref=" DataSource " p:configlocation=" classpath: Mybatis-config.xml " p:mapperlocations=" Classpath:com/eliteams/quick4j/web/dao/*.xml "/><!-- Spring and MyBatis are integrated with the configuration, scanning all DAO, in the case of single data source can not write Sqlsessionfactorybeanname--><bean class= " Org.mybatis.spring.mapper.MapperScannerConfigurer " p:basepackage=" Com.eliteams.quick4j.web.dao " p: Sqlsessionfactorybeanname= "Sqlsessionfactory"/>
-------------------------Amitabha----Buddha bless----never bug--------------------------
2, Spring and mybatis integration of three ways one
Sqlsessionfactorybean has a must attribute datasource, and it also has a common properties configlocation (used to specify the XML configuration file path for MyBatis).
XML Configuration for Spring:
<!--Create sqlsessionfactory and specify data source-- <bean id= "sqlsessionfactory" class= " Org.mybatis.spring.SqlSessionFactoryBean "> <property name=" DataSource "ref=" DataSource "/> <! --Specify Sqlmapconfig general profile, custom environment not valid in spring container-- <property name= "configlocation" value= "Classpath:sqlMapConfig.xml"/> </bean>
MyBatis Total configuration file Sqlmapconfig.xml:
<configuration> <typeAliases> <typealias type= "Com.xxt.ibatis.dbcp.domain.User" alias= " User "/> </typeAliases> <mappers> <mapper resource=" com/xxt/ibatis/dbcp/domain/ Usermapper.xml "/> </mappers></configuration>
Usermapper.xml:
<mapper namespace= "Com.xxt.ibatis.dbcp.dao.UserDao" > <resultmap type= "User" id= "UserMap" > <id property= "id" column= "id"/> <result property= "name" column= "name"/> <result property= " Password "column=" password "/> <result property=" Createtime "column=" Createtime "/> </ resultmap> <select id= "Getuserbyid" parametertype= "int" resultmap= "UserMap" > select * from user WHERE id = #{id} </select><mapper/>
DAO layer Interface Class Userdao.java: Note that the interface method defined here needs to correspond to the ID of the <select> tag for the usermapper.xml mapping
Public interface Userdao {public User Getuserbyid (int id);}
The class that is called when you need to manipulate data Userservice.java:
public class UserService { //Here omit Sqlsession method of obtaining private sqlsession sqlsession; Private Userdao Userdao; Public User Getuserbyid (int id) { return Userdao.getuserbyid (ID);} }
Two
Sqlsessionfactorybean to replace Sqlsessionfactorybuilder to create sqlsession
Using the Data Mapper (Mapperfactorybean) method
No need to write MyBatis mapping file
The corresponding SQL statements and input parameters are provided in annotated form.
XML Configuration for Spring:
<!--introducing a JDBC configuration file--<context:property-placeholder location= "jdbc.properties"/> <!--creating a JDBC data source --<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" > & Lt;property name= "Driverclassname" value= "${driver}"/> <property name= "url" value= "${url}"/> &L T;property name= "username" value= "${username}"/> <property name= "password" value= "${password}"/> <property name= "InitialSize" value= "${initialsize}"/> <property name= "maxactive" value= "${maxActive}"/ > <property name= "maxidle" value= "${maxidle}"/> <property name= "Minidle" value= "${minidle}"/ > </bean> <!--Create sqlsessionfactory while specifying data source--<bean id= "sqlsessionfactory" class= "Org.myb Atis.spring.SqlSessionFactoryBean "> <property name=" dataSource "ref=" DataSource "/> </bean> <!--Create data mapper, data mapThe device must be interface---<bean id= "Usermapper" class= "Org.mybatis.spring.mapper.MapperFactoryBean" > <property Name= "Mapperinterface" value= "Com.xxt.ibatis.dbcp.dao.UserMapper"/> <property name= "Sqlsessionfactory" ref = "Sqlsessionfactory"/> </bean> <bean id= "Userdaoimpl" class= "Com.xxt.ibatis.dbcp.dao.impl.UserDaoI MPL "> <property name=" usermapper "ref=" Usermapper "/> </bean>
Data Mapper Usermapper.java:
Public interface Usermapper { @Select ("SELECT * from user WHERE id = #{userid}") user GetUser (@Param ("UserId") Lon g ID); }
DAO interface Class Userdao.java:
Public interface Userdao {public user Getuserbyid (user user);}
DAO interface Implementation Class Userdaoimpl.java:
public class Userdaoimpl implements Userdao { private usermapper usermapper; public void Setusermapper (Usermapper usermapper) { this.usermapper = usermapper; } Public user Getuserbyid (user user) { return Usermapper.getuser (User.getid ());} }
three
Sqlsessionfactorybean to replace Sqlsessionfactorybuilder create sqlsession
Do not use the Data Mapper (Mapperfactorybean) method to Mapperscannerconfigurer to scan
No need to write MyBatis mapping file
The corresponding SQL statements and input parameters are provided in annotated form.
Omit beans that define mapper with annotations
The proxy class created by Mapperfactorybean implements the Usermapper interface and injects it into the application. Because the agent is created in the runtime environment (runtime, translator note), the specified mapper must be an interface, not a specific implementation class.
The above Mapperfactorybean configuration has a great disadvantage, that is, the system has a lot of configuration files need to be written manually, so the above way is no longer necessary.
It is not necessary to register all the mappers in the Spring XML configuration file. Instead, you can use a mapperscannerconfigurer, which will look for mappers under the classpath and automatically build them into Mapperfactorybean.
XML Configuration for Spring:
<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "Basepackage" value= "Org.mybatis.spring.sample.mapper"/></bean>
The Basepackage property lets you set the basic package path for the Mapper interface file. You can use a semicolon or comma as a delimiter to set more than one package path. Each mapper will be recursively searched in the specified package path.
Note that it is not necessary to specify Sqlsessionfactory or Sqlsessiontemplate, because Mapperscannerconfigurer will create Mapperfactorybean and then assemble automatically. However, if you use more than one DataSource, automatic assembly may fail. In this case, you can use the Sqlsessionfactorybeanname or Sqlsessiontemplatebeanname property to set the correct bean name.
Spring-mybatis---Configuration Sqlsessionfactorybean, integrated Spring-mybatis (RPM)