本文主要介绍spring与mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接
http://code.google.com/p/mybatis/下载到。
1、采用数据映射器(mapperfactorybean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。
(1)spring配置文件:
"jdbc.properties"/>
"datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close">
"driverclassname" value="${driver}"/>
"url" value="${url}"/>
"username" value="${username}"/>
"password" value="${password}"/>
"initialsize" value="${initialsize}"/>
"maxactive" value="${maxactive}"/>
"maxidle" value="${maxidle}"/>
"minidle" value="${minidle}"/>
"sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
"datasource" ref="datasource" />
"usermapper" class="org.mybatis.spring.mapper.mapperfactorybean">
"mapperinterface" value="com.xxt.ibatis.dbcp.dao.usermapper" />
"sqlsessionfactory" ref="sqlsessionfactory" />
"userdaoimpl2" class="com.xxt.ibatis.dbcp.dao.impl.userdaoimpl2">
"usermapper" ref="usermapper"/>
数据映射器usermapper,代码如下:
public interface usermapper {
@select("select * from user where id = #{userid}")
user getuser(@param("userid") long id);
}
dao接口类userdao,代码如下:
public interface userdao {
public user getuserbyid(user user);
}
dao实现类userdaoimpl2,,代码如下:
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、采用接口org.apache.ibatis.session.sqlsession的实现类org.mybatis.spring.sqlsessiontemplate。
mybatis中, sessionfactory可由sqlsessionfactorybuilder.来创建。
mybatis-spring 中,使用了sqlsessionfactorybean来替代。
sqlsessionfactorybean有一个必须属性datasource,
另外其还有一个通用属性configlocation(用来指定mybatis的xml配置文件路径)。
(1)spring配置文件:
id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
<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总配置文件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)实体类映射文件user.map.xml:
"com.xxt.ibatis.dbcp.domain.user">
"user" id="usermap">
"id" column="id" />
"name" column="name" />
"password" column="password" />
"createtime" column="createtime" />
select * from user where id = #{id}
(4)dao层接口实现类userdaoimpl:
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、采用抽象类org.mybatis.spring.support.sqlsessiondaosupport提供sqlsession。
(1)spring配置文件:
"sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
"datasource" ref="datasource" />
"configlocation" value="classpath:sqlmapconfig.xml"/>
"sqlsession" class="org.mybatis.spring.sqlsessiontemplate">
"0" ref="sqlsessionfactory" />
"userdaoimpl3" class="com.xxt.ibatis.dbcp.dao.impl.userdaoimpl3">
"sqlsessiontemplate" ref="sqlsession" />
(2) dao层接口实现类userdaoimpl3:
public class userdaoimpl3 extends sqlsessiondaosupport implements userdao {
public user getuserbyid(user user) {
return (user) getsqlsession().selectone("com.xxt.ibatis.dbcp.domain.user.getuser", user);
}
}
文章来源 :