最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring與Mybatis的整合方法有哪些

 更新時(shí)間:2015年10月30日 11:27:44   作者:愛(ài)上一只狐  
本文主要給大家介紹Spring與Mybatis的三種常用整合方法,需要用到的整合框架包mybatis-spring.jar,對(duì)spring mybatis整合感興趣的朋友可以參考下本文

本文主要介紹Spring與Mybatis三種常用整合方法,需要的整合架包是mybatis-spring.jar,可通過(guò)鏈接
http://code.google.com/p/mybatis/下載到。

  1、采用數(shù)據(jù)映射器(MapperFactoryBean)的方式,不用寫(xiě)mybatis映射文件,采用注解方式提供相應(yīng)的sql語(yǔ)句和輸入?yún)?shù)。

  (1)Spring配置文件:

 <!-- 引入jdbc配置文件 -->   
 <context:property-placeholder location="jdbc.properties"/>     
 <!--創(chuàng)建jdbc數(shù)據(jù)源 -->    
<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>    
 <!-- 創(chuàng)建SqlSessionFactory,同時(shí)指定數(shù)據(jù)源-->    
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
 <property name="dataSource" ref="dataSource" />     
</bean>     
 <!--創(chuàng)建數(shù)據(jù)映射器,數(shù)據(jù)映射器必須為接口--> 
   <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)數(shù)據(jù)映射器UserMapper,代碼如下:

public interface UserMapper {   
  @Select("SELECT * FROM user WHERE id = #{userId}")    
 User getUser(@Param("userId") long id);  
 } 

(3)dao接口類(lèi)UserDao,代碼如下:

public interface UserDao {  
  public User getUserById(User user); 
 } 

(4)dao實(shí)現(xiàn)類(lèi)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的實(shí)現(xiàn)類(lèi)org.mybatis.spring.SqlSessionTemplate。

    mybatis中, sessionFactory可由SqlSessionFactoryBuilder.來(lái)創(chuàng)建。

MyBatis-Spring 中,使用了SqlSessionFactoryBean來(lái)替代。
SqlSessionFactoryBean有一個(gè)必須屬性dataSource,另外其還有一個(gè)通用屬性configLocation(用來(lái)指定mybatis的xml配置文件路徑)。

   (1)Spring配置文件:

<!-- 創(chuàng)建SqlSessionFactory,同時(shí)指定數(shù)據(jù)源--> 
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
 <property name="dataSource" ref="dataSource" />    
 <!-- 指定sqlMapConfig總配置文件,訂制的environment在spring容器中不在生效-->  
 <property  name="configLocation"  value="classpath:sqlMapConfig.xml"/>  
 <!--指定實(shí)體類(lèi)映射文件,可以指定同時(shí)指定某一包以及子包下面的所有配置文件,mapperLocations和configLocation有一個(gè)即可,當(dāng)需要為實(shí)體類(lèi)指定別名時(shí),可指定configLocation屬性,再在mybatis總配置文件中采用mapper引入實(shí)體類(lèi)映射文件 --> 
  <!- - <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)實(shí)體類(lèi)映射文件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層接口實(shí)現(xiàn)類(lèi)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、采用抽象類(lèi)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實(shí)例 -->   
<property name="sqlSessionTemplate" ref="sqlSession" />   
 <!--也可直接注入SqlSessionFactory實(shí)例,二者都指定時(shí),SqlSessionFactory失效 -->   
 <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" />  --> 
 </bean> 

  (2) dao層接口實(shí)現(xiàn)類(lèi)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);   
} 
 } 

相關(guān)文章

  • Spring項(xiàng)目中Ordered接口的應(yīng)用之全局過(guò)濾器(GlobalFilter)的順序控制

    Spring項(xiàng)目中Ordered接口的應(yīng)用之全局過(guò)濾器(GlobalFilter)的順序控制

    在Spring框架,尤其是Spring Cloud Gateway或Spring WebFlux項(xiàng)目中,Ordered接口扮演著重要的角色,特別是在實(shí)現(xiàn)全局過(guò)濾器(GlobalFilter)時(shí),用于控制過(guò)濾器執(zhí)行的優(yōu)先級(jí),下面將介紹如何在Spring項(xiàng)目中使用Ordered接口來(lái)管理Global Filter的執(zhí)行順序,需要的朋友可以參考下
    2024-06-06
  • 模仿J2EE的session機(jī)制的App后端會(huì)話信息管理實(shí)例

    模仿J2EE的session機(jī)制的App后端會(huì)話信息管理實(shí)例

    下面小編就為大家分享一篇模仿J2EE的session機(jī)制的App后端會(huì)話信息管理實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • java 中堆內(nèi)存和棧內(nèi)存理解

    java 中堆內(nèi)存和棧內(nèi)存理解

    這篇文章主要介紹了java 中的堆內(nèi)存和棧內(nèi)存的知識(shí),有需要的朋友可以參考下
    2017-03-03
  • java實(shí)現(xiàn)角色及菜單權(quán)限的項(xiàng)目實(shí)踐

    java實(shí)現(xiàn)角色及菜單權(quán)限的項(xiàng)目實(shí)踐

    在Java中,實(shí)現(xiàn)角色及菜單權(quán)限管理涉及定義實(shí)體類(lèi)、設(shè)計(jì)數(shù)據(jù)庫(kù)表、實(shí)現(xiàn)服務(wù)層和控制器層,這種管理方式有助于有效控制用戶(hù)權(quán)限,適用于企業(yè)級(jí)應(yīng)用,感興趣的可以一起來(lái)了解一下
    2024-09-09
  • SpringBoot捕獲feign拋出異常的方法

    SpringBoot捕獲feign拋出異常的方法

    使用Springboot時(shí),使用feign客戶(hù)端作為http請(qǐng)求工具時(shí),當(dāng)接口拋出異常信息時(shí),使用全局異常是捕獲不了異常的,本文小編給大家介紹了SpringBoot捕獲feign拋出異常的方法,需要的朋友可以參考下
    2025-04-04
  • java實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理系統(tǒng)

    java實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 基于SpringBoot中activeMq的JmsTemplate的實(shí)例

    基于SpringBoot中activeMq的JmsTemplate的實(shí)例

    這篇文章主要介紹了基于SpringBoot中activeMq的JmsTemplate的實(shí)例問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Springboot日期轉(zhuǎn)換器實(shí)現(xiàn)代碼及示例

    Springboot日期轉(zhuǎn)換器實(shí)現(xiàn)代碼及示例

    這篇文章主要介紹了Springboot日期轉(zhuǎn)換器實(shí)現(xiàn)代碼及示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Invalid bound statement(not found):錯(cuò)誤的解決方案

    Invalid bound statement(not found):錯(cuò)誤的解決方案

    本文介紹了在開(kāi)發(fā)Java SpringBoot應(yīng)用程序時(shí)出現(xiàn)的"Invalidboundstatement(notfound)"錯(cuò)誤的原因及解決方法,該錯(cuò)誤通常與MyBatis或其他持久化框架相關(guān),可能是由于配置錯(cuò)誤、拼寫(xiě)錯(cuò)誤或其他問(wèn)題引起的,解決方法包括檢查SQL映射文件
    2025-01-01
  • Java使用Condition實(shí)現(xiàn)精準(zhǔn)喚醒線程詳解

    Java使用Condition實(shí)現(xiàn)精準(zhǔn)喚醒線程詳解

    這篇文章主要為大家詳細(xì)介紹了Java如何使用Condition實(shí)現(xiàn)精準(zhǔn)喚醒線程效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-02-02

最新評(píng)論

若羌县| 揭西县| 元谋县| 武清区| 祁门县| 枣庄市| 宁明县| 东乌珠穆沁旗| 巧家县| 敖汉旗| 浮山县| 新疆| 武冈市| 马龙县| 安阳市| 滦南县| 化州市| 华容县| 体育| 商城县| 农安县| 涟源市| 双鸭山市| 满洲里市| 巩留县| 西丰县| 乌兰县| 永登县| 桐梓县| 肃宁县| 阆中市| 丰原市| 乐清市| 镇宁| 施甸县| 嘉黎县| 吐鲁番市| 大庆市| 成都市| 于田县| 化德县|