spring与mybatis三种整合方法

[url]http://blog.youkuaiyun.com/skylovedaim/article/details/18553081[/url]
本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接
http://code.google.com/p/mybatis/下载到。

[color=red][b] 1、采用数据映射器(MapperFactoryBean)的方式[/b][/color],不用写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>




数据映射器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());
}
}





[color=red][b]2、采用接口org.apache.ibatis.session.SqlSession的实现类[/b][/color]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:
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; }
}



附加MyBatis SqlMapConfig.xml例子
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<settings>
<setting name="useGeneratedKeys" value="false"/>
</settings>

<!-- Use type aliases to avoid typing the full classname every time. -->
<typeAliases>
<typeAlias alias="Order" type="org.apache.camel.example.mybatis.Order"/>
</typeAliases>

<!-- setup environment with JDBC data source -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:memory:mybatis;create=true"/>
</dataSource>
</environment>
</environments>

<!-- mapping files -->
<mappers>
<mapper resource="org/apache/camel/example/mybatis/Order.xml"/>
</mappers>

</configuration>

[color=red]使用mapperLocations属性错误[/color]: Error parsing Mapper XML
可能解决方式1: [url]http://www.cnblogs.com/huanmieuroshui/archive/2012/12/18/2822754.html[/url]
可能解决方式2: <property name="mapperLocations" value="classpath:com/pandy/app/bean/mapper/*Mapper.xml" />, 这个属性是直接扫描Mapper.xml文件, 而不是查找SqlMapConfig.xml文件.


[color=red][b]3、采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession。[/b][/color]

(1)spring配置文件:
<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:
public class UserDaoImpl3 extends SqlSessionDaoSupport implements UserDao {  
public User getUserById(User user) {
return (User) getSqlSession().selectOne("com.xxt.ibatis.dbcp.domain.User.getUser", user);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值