This article mainly introduces three common integration methods of spring and mybatis.The required integration package is mybatis-spring.jar, which can be accessed through the link
Download to.
1, using the data mapper (mapperfactorybean) method, without writing mybatis mapping file,The corresponding sql statement and input parameters are provided in annotation mode.
(1) Spring configuration file:
<!-Introduce jdbc configuration file->
<context:property-placeholder location="jdbc.properties" />
<!-Create jdbc data source->
<bean destroy-method="close">
<property name="driverclassname" value="${driver}" />
<property name="url" value="${url}" />
<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 and specify the data source->
<bean>
<property name="datasource" ref="datasource" />
</bean>
<!-Create a data mapper,The data mapper must be an interface->
<bean>
<property name="mapperinterface" value="com.xxt.ibatis.dbcp.dao.usermapper" />
<property name="sqlsessionfactory" ref="sqlsessionfactory" />
</bean>
<bean>
<property name="usermapper" ref="usermapper" />
</bean>
(2) The data mapper usermapper, the code is as follows:
public interface usermapper {
@select ("select * from user where id=#{userid}")
user getuser (@param ("userid") long id);
}
</pre>
</div>
<p>
<strong>
(3) dao interface class userdao, the code is as follows:
</strong>
</p>
<div>
<pre>
public interface userdao {
public user getuserbyid (user user);
}
(4) dao class userdaoimpl2, the code is as follows:
public class userdaoimpl2 implements userdao {
private usermapper usermapper;
public void setusermapper (usermapper usermapper) {
this.usermapper=usermapper;
}
public user getuserbyid (user user) {
return usermapper.getuser (user.getid ());
}
}
2. Use the implementation class org.mybatis.spring.sqlsessiontemplate of the interface org.apache.ibatis.session.sqlsession.
In mybatis, sessionfactory can be created by sqlsessionfactorybuilder.
In mybatis-spring, sqlsessionfactorybean is used instead.
sqlsessionfactorybean has a required attribute datasource, and it also has a common attribute configlocation (used to specify the path to the xml configuration file for mybatis).
(1) Spring configuration file:
<bean>
<property name="datasource" ref="datasource" />
<property name="configlocation" value="classpath:sqlmapconfig.xml" />
<!--<property name="mapperlocations" value="classpath *:com/xxt/ibatis/dbcp/**/*. Xml" />->
<bean>
(2) 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/user.map.xml" />
</mappers>
</configuration>
(3) entity class mapping file user.map.xml:
<mapper namespace="com.xxt.ibatis.dbcp.domain.user">
<resultmap type="user">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="password" column="password" />
<result property="createtime" column="createtime" />
</resultmap>
<select parametertype="user" resultmap="usermap">
select * from user where id=#(id)
</select>
<mapper />
(4) dao layer interface implementation class userdaoimpl:
java code
public class userdaoimpl implements userdao {
public sqlsessiontemplate sqlsession;
public user getuserbyid (user user) {
return (user) sqlsession.selectone ("com.xxt.ibatis.dbcp.domain.user.getuser", user);
}
public void setsqlsession (sqlsessiontemplate sqlsession) {
this.sqlsession=sqlsession;}
}
3. Use abstract class org.mybatis.spring.support.sqlsessiondaosupport to provide sqlsession.
(1) Spring configuration file:
java code
<bean>
<property name="datasource" ref="datasource" />
<property name="configlocation" value="classpath:sqlmapconfig.xml" />
<!-<property name="mapperlocations" value="classpath *:com/xxt/ibatis/dbcp/domain/user.map.xml" />->
</bean>
<bean>
<constructor-arg index="0" ref="sqlsessionfactory" />
</bean>
<bean>
<!-Inject sqlsessiontemplate instance->
<property name="sqlsessiontemplate" ref="sqlsession" />
<!-You can also directly inject the sqlsessionfactory instance. When both are specified,sqlsessionfactory failed->
<!-<property name="sqlsessionfactory" ref="sqlsessionfactory" />->
</bean>
(2) dao layer interface implementation class userdaoimpl3:
java code
public class userdaoimpl3 extends sqlsessiondaosupport implements userdao {
public user getuserbyid (user user) {
return (user) getsqlsession (). selectone ("com.xxt.ibatis.dbcp.domain.user.getuser", user);
}
}
Related articles
- Spring and Mybatis integrate Redis based on annotations
- Detailed explanation of the integration example of springboot and mybatis (perfect fusion)
- Spring integrated MyBatis (Maven + MySQL) graphic tutorials
- Analysis of Spring and MyBatis integration and reverse engineering
- Detailed Mapper Injection in the Integration of Java's MyBatis Framework and Spring Framework
- SpringMVC integrates mybatis instance code
- AngularJS integrates Springmvc, Spring, Mybatis to build a development environment
- Analysis of the implementation process of mybatis and spring integration
- Three integration methods of spring and mybatis