Spring与Mybatis的整合方法有哪些
本文主要介绍spring与mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接
下载到。
1、采用数据映射器(mapperfactorybean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。
(1)spring配置文件:
<!-- 引入jdbc配置文件 --> <context:property-placeholder location="jdbc.properties"/> <!--创建jdbc数据源 --> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" 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> <!-- 创建sqlsessionfactory,同时指定数据源--> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> </bean> <!--创建数据映射器,数据映射器必须为接口--> <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="userdaoimpl2" class="com.xxt.ibatis.dbcp.dao.impl.userdaoimpl2"> <property name="usermapper" ref="usermapper"/> </bean>
(2)数据映射器usermapper,代码如下:
public interface usermapper { @select("select * from user where id = #{userid}") user getuser(@param("userid") long id); }
(3)dao接口类userdao,代码如下:
public interface userdao { public user getuserbyid(user user); }
(4)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配置文件:
<!-- 创建sqlsessionfactory,同时指定数据源-->
<bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
<property name="datasource" ref="datasource" />
<!-- 指定sqlmapconfig总配置文件,订制的environment在spring容器中不在生效-->
<property name="configlocation" value="classpath:sqlmapconfig.xml"/>
<!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件,mapperlocations和configlocation有一个即可,当需要为实体类指定别名时,可指定configlocation属性,再在mybatis总配置文件中采用mapper引入实体类映射文件 -->
<!- - <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:
<mapper namespace="com.xxt.ibatis.dbcp.domain.user"> <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="getuser" parametertype="user" resultmap="usermap"> select * from user where id = #{id} </select> <mapper/>
(4)dao层接口实现类userdaoimpl:
java代码
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配置文件:
java代码
<bean 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/domain/user.map.xml"/ > --> </bean> <bean id="sqlsession" class="org.mybatis.spring.sqlsessiontemplate"> <constructor-arg index="0" ref="sqlsessionfactory" /> </bean> <bean id="userdaoimpl3" class="com.xxt.ibatis.dbcp.dao.impl.userdaoimpl3"> <!--注入sqlsessiontemplate实例 --> <property name="sqlsessiontemplate" ref="sqlsession" /> <!--也可直接注入sqlsessionfactory实例,二者都指定时,sqlsessionfactory失效 --> <!-- <property name="sqlsessionfactory" ref="sqlsessionfactory" /> --> </bean>
(2) dao层接口实现类userdaoimpl3:
java代码
public class userdaoimpl3 extends sqlsessiondaosupport implements userdao { public user getuserbyid(user user) { return (user) getsqlsession().selectone("com.xxt.ibatis.dbcp.domain.user.getuser", user); } }
下一篇: PHP编程 SSO详细介绍及简单实例