Spring與Mybatis的整合辦法有哪些。本站提示廣大學習愛好者:(Spring與Mybatis的整合辦法有哪些)文章只能為提供參考,不一定能成為您想要的結果。以下是Spring與Mybatis的整合辦法有哪些正文
本文重要引見Spring與Mybatis三種經常使用整合辦法,須要的整合架包是mybatis-spring.jar,可經由過程鏈接
http://code.谷歌.com/p/mybatis/下載到。
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); } }