Spring与Mybatis的整合方法有哪些
本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接
http://code.google.com/p/mybatis/下载到。
1、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数。
(1)Spring配置文件:
<!--引入jdbc配置文件--> <context:property-placeholderlocation="jdbc.properties"/> <!--创建jdbc数据源--> <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"> <propertyname="driverClassName"value="${driver}"/> <propertyname="url"value="${url}"/> <propertyname="username"value="${username}"/> <propertyname="password"value="${password}"/> <propertyname="initialSize"value="${initialSize}"/> <propertyname="maxActive"value="${maxActive}"/> <propertyname="maxIdle"value="${maxIdle}"/> <propertyname="minIdle"value="${minIdle}"/> </bean> <!--创建SqlSessionFactory,同时指定数据源--> <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <propertyname="dataSource"ref="dataSource"/> </bean> <!--创建数据映射器,数据映射器必须为接口--> <beanid="userMapper"class="org.mybatis.spring.mapper.MapperFactoryBean"> <propertyname="mapperInterface"value="com.xxt.ibatis.dbcp.dao.UserMapper"/> <propertyname="sqlSessionFactory"ref="sqlSessionFactory"/> </bean> <beanid="userDaoImpl2"class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl2"> <propertyname="userMapper"ref="userMapper"/> </bean>
(2)数据映射器UserMapper,代码如下:
publicinterfaceUserMapper{ @Select("SELECT*FROMuserWHEREid=#{userId}") UsergetUser(@Param("userId")longid); }
(3)dao接口类UserDao,代码如下:
publicinterfaceUserDao{ publicUsergetUserById(Useruser); }
(4)dao实现类UserDaoImpl2,,代码如下:
publicclassUserDaoImpl2implementsUserDao{ privateUserMapperuserMapper; publicvoidsetUserMapper(UserMapperuserMapper){ this.userMapper=userMapper; } publicUsergetUserById(Useruser){ returnuserMapper.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,同时指定数据源-->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="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> <typeAliastype="com.xxt.ibatis.dbcp.domain.User"alias="User"/> </typeAliases> <mappers> <mapperresource="com/xxt/ibatis/dbcp/domain/user.map.xml"/> </mappers> </configuration>
(3)实体类映射文件user.map.xml:
<mappernamespace="com.xxt.ibatis.dbcp.domain.User"> <resultMaptype="User"id="userMap"> <idproperty="id"column="id"/> <resultproperty="name"column="name"/> <resultproperty="password"column="password"/> <resultproperty="createTime"column="createtime"/> </resultMap> <selectid="getUser"parameterType="User"resultMap="userMap"> select*fromuserwhereid=#{id} </select> <mapper/>
(4)dao层接口实现类UserDaoImpl:
Java代码
publicclassUserDaoImplimplementsUserDao{ publicSqlSessionTemplatesqlSession; publicUsergetUserById(Useruser){ return(User)sqlSession.selectOne("com.xxt.ibatis.dbcp.domain.User.getUser",user); } publicvoidsetSqlSession(SqlSessionTemplatesqlSession){ this.sqlSession=sqlSession;} }
3、采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession。
(1)spring配置文件:
Java代码
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <propertyname="dataSource"ref="dataSource"/> <propertyname="configLocation"value="classpath:sqlMapConfig.xml"/> <!--<propertyname="mapperLocations"value="classpath*:com/xxt/ibatis/dbcp/domain/user.map.xml"/>--> </bean> <beanid="sqlSession"class="org.mybatis.spring.SqlSessionTemplate"> <constructor-argindex="0"ref="sqlSessionFactory"/> </bean> <beanid="userDaoImpl3"class="com.xxt.ibatis.dbcp.dao.impl.UserDaoImpl3"> <!--注入SqlSessionTemplate实例--> <propertyname="sqlSessionTemplate"ref="sqlSession"/> <!--也可直接注入SqlSessionFactory实例,二者都指定时,SqlSessionFactory失效--> <!--<propertyname="sqlSessionFactory"ref="sqlSessionFactory"/>--> </bean>
(2)dao层接口实现类UserDaoImpl3:
Java代码
publicclassUserDaoImpl3extendsSqlSessionDaoSupportimplementsUserDao{ publicUsergetUserById(Useruser){ return(User)getSqlSession().selectOne("com.xxt.ibatis.dbcp.domain.User.getUser",user); } }