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

MyBatis無縫對接Spring的方法

 更新時間:2017年09月01日 15:34:22   作者:落葉飛逝的戀  
Spring框架與MyBatis框架是Java互聯網技術的主流框架。那么mybatis如何無縫對接spring呢?下面通過本文給大家介紹,需要的的朋友參考下吧

1.為什么會出現MyBatis-Spring

Spring框架與MyBatis框架是Java互聯網技術的主流框架。但是如何將MyBatis無縫整合到Spring框架中呢?這時候就誕生了MyBatis-Spring。使用這個類庫中得類,Spring將會加載必要的MyBatis工廠類和session類。

Spring3.0也僅僅支持ibatis2.0。本來將MyBatis3的支持添加到Spring3.0中。而不幸,Spring3.0的開發(fā)在MyBatis3.0官方發(fā)布前就結束了。因為Spring開發(fā)團隊不想發(fā)布一個非發(fā)布版的MyBatis的整合支持。就放棄了對MyBatis的支持。

隨著Spring越來越成為java事實標準的技術框架。Spring 4.0 移除了對iBatis的直接支持。MyBatis團隊開發(fā)出來了基于Spring的MyBatis整合Jar---MyBatis-Spring。

2.使用MyBatis-Spring的好處

1.使得業(yè)務層和模型層得到更好的分離。再Spring框架中MyBatis也更加簡單,節(jié)約不少的代碼

2.甚至不需要顯示的使用SqlSessionFactory、SqlSessiond等對象

3.MyBatis-Spring組成部分

1.配置數據源

2.配置SqlSessionFactory

3.配置SqlSessionTemplate

4.配置Mapper

5.事務處理

MyBatis中要構建SqlSessionFactory對象,讓它產生SqlSession,而在MyBatis-Spring項目中SqlSession的使用是通過SqlSessionTemplate來實現的,它提供了對SqlSession操作的封裝。所以可以通過SqlSessionTemplate可以得到Mapper。

4.在Spring MVC中配置

4.1 配置SqlSessionFactoryBean

在基本的 MyBatis中,session工廠可以使用SqlSessionFactoryBuilder 來創(chuàng)建。而在 MyBatis-Spring 中,則使用 SqlSessionFactoryBean 來替代。

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
</bean>

注意點

SqlSessionFactory 有一個單獨的必須屬性,就是 JDBC 的 DataSource

在SqlSessionFactoryBean的返回getObject的時候,有個驗證。

@Override
 public SqlSessionFactory getObject() throws Exception {
  if (this.sqlSessionFactory == null) {
   afterPropertiesSet();
  }

  return this.sqlSessionFactory;
 }
 @Override
 public void afterPropertiesSet() throws Exception {
  notNull(dataSource, "Property 'dataSource' is required");
  notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
  state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
       "Property 'configuration' and 'configLocation' can not specified with together");
  this.sqlSessionFactory = buildSqlSessionFactory();
 }

4.2配置datasource

這里我們使用的是阿里巴巴的數據庫連接池。https://github.com/alibaba/druid

compile group: 'com.alibaba', name: 'druid', version: '1.1.2'
<!--阿里巴巴的數據庫連接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  <!-- 基本屬性 url、user、password -->
  <property name="url" value="${jdbc_url}"/>
  <property name="username" value="${jdbc_user}"/>
  <property name="password" value="${jdbc_password}"/>
</bean>

4.3配置數據庫鏈接屬性

在resource目錄下創(chuàng)建properties文件夾,并創(chuàng)建datasource.properties

jdbc_url=jdbc:mysql://localhost:3306/cnblogs?serverTimezone=Asia/Shanghai&characterEncoding=utf8
jdbc_user=root
jdbc_password=root

使用阿里巴巴的數據庫連接池是無需配置數據庫驅動。是根據url的前綴來判斷的

4.4配置Spring加載配置屬性及配置替換動態(tài)標簽

<!--加載配置文件路徑-->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="location" value="classpath:properties/datasource.properties"></property>
</bean>
<!--獲取配置屬性之后動態(tài)替換標簽-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
  <property name="properties" ref="configProperties"/>
</bean>

4.5配置Spring自動創(chuàng)建Mapper接口的bean

MyBatis-Spring提供了一個轉換器MapperScannerConfigurer,可以將映射接口轉換為Spring容器中Bean。這樣就可以在代碼中注入映射的bean

<!--采用自動掃描方式創(chuàng)建Mapper Bean-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.cnblogs.dao"></property>
</bean>

basePackage屬性是讓你為映射器接口文件設置基本的包路徑。 你可以使用分號或逗號作為分隔符設置多于一個的包路徑。每個映射器將會在指定的包路徑中遞歸地被搜索到。

4.6配置事務

MyBatis和Spring結合后是使用Spring AOP去管理事務的,使用Spring AOP是相當的簡單的,它分為聲明式事務和編程性事務。大部分場景下使用聲明式事務就可以了。

引入Jar包

compile group: 'org.springframework', name: 'spring-tx', version: '4.3.10.RELEASE'
compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.10.RELEASE'
<!--事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
</bean>
<!--使用聲明式事務管理方式-->
<tx:annotation-driven transaction-manager="transactionManager"/>

5.完整的Spring xml配置及引用的Jar包

引入的Jar列表

compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.10.RELEASE'
compile group: 'org.springframework', name: 'spring-tx', version: '4.3.10.RELEASE'
compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.10.RELEASE'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.1'
compile group: 'org.mybatis', name: 'mybatis', version: '3.4.5'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
compile group: 'com.alibaba', name: 'druid', version: '1.1.2'

Spring的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!--spring可以自動去掃描base-pack下面的包或者子包下面的Java文件-->
  <context:component-scan base-package="com.cnblogs.controller,com.cnblogs.service,com.cnblogs.dao"/>
  <!--自動注冊RequestMappingHandlerMapping與RequestMappingHandlerAdpter-->
  <mvc:annotation-driven/>
  <!-- 對靜態(tài)資源文件的訪問-->
  <mvc:default-servlet-handler/>
  <!--加載配置文件路徑-->
  <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:properties/datasource.properties"></property>
  </bean>
  <!--獲取配置屬性之后動態(tài)替換標簽-->
  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
    <property name="properties" ref="configProperties"/>
  </bean>
  <!--阿里巴巴的數據庫連接池-->
  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <!-- 基本屬性 url、user、password -->
    <property name="url" value="${jdbc_url}"/>
    <property name="username" value="${jdbc_user}"/>
    <property name="password" value="${jdbc_password}"/>
  </bean>
  <!--構建SqlSessionFactory-->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:config/mybatis-config.xml"/>
    <property name="mapperLocations" value="classpath:mappers/*.xml"/>
  </bean>
  <!--采用自動掃描方式創(chuàng)建Mapper Bean-->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.cnblogs.dao"></property>
  </bean>
  <!--事務管理器-->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  <!--使用聲明式事務管理方式-->
  <tx:annotation-driven transaction-manager="transactionManager"/>
  <!--視圖解析器-->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/content"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>
</beans>

總結

以上所述是小編給大家介紹的MyBatis無縫對接Spring的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

相關文章

  • java 解決Eclipse掛掉問題的方法

    java 解決Eclipse掛掉問題的方法

    本篇文章是對在java中解決Eclipse掛掉問題的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • IDEA插件EasyCode及MyBatis最優(yōu)配置步驟詳解

    IDEA插件EasyCode及MyBatis最優(yōu)配置步驟詳解

    這篇文章主要介紹了IDEA插件EasyCode MyBatis最優(yōu)配置步驟詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • tomcat connection-timeout連接超時源碼解析

    tomcat connection-timeout連接超時源碼解析

    這篇文章主要為大家介紹了tomcat connection-timeout連接超時源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Java中EasyPoi導出復雜合并單元格的方法

    Java中EasyPoi導出復雜合并單元格的方法

    這篇文章主要介紹了Java中EasyPoi導出復雜合并單元格的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • Java設計模式中的設計原則之合成復用原則詳解

    Java設計模式中的設計原則之合成復用原則詳解

    這篇文章主要介紹了Java設計模式中的設計原則之合成復用原則詳解,原則是盡量使用合成/聚合的方式,而不是使用繼承聚合關系表示的是整體和部分的關系,整體與部分可以分開,可以理解為成員變量和當前類的關系就是聚合關系,需要的朋友可以參考下
    2023-11-11
  • java三層架構原理與作用小結

    java三層架構原理與作用小結

    這篇文章主要對Java三層架構的概念、作用等進行了介紹,需要的朋友可以參考下
    2017-04-04
  • Spring詳細解讀事務管理

    Spring詳細解讀事務管理

    Spring事務的本質就是對數據庫事務的支持,沒有數據庫事務,Spring是無法提供事務功能的。Spring只提供統一的事務管理接口,具體實現都是由數據庫自己實現的,Spring會在事務開始時,根據當前設置的隔離級別,調整數據庫的隔離級別,由此保持一致
    2022-04-04
  • springboot 整合druid及配置依賴

    springboot 整合druid及配置依賴

    這篇文章主要介紹了springboot 整合druid及jdbc 依賴、數據庫依賴(mysql),druid 依賴的實現代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • 關于Maven混合配置私有倉庫和公共倉庫的問題

    關于Maven混合配置私有倉庫和公共倉庫的問題

    這篇文章主要介紹了Maven混合配置私有倉庫和公共倉庫,通過實例代碼詳細介紹了私有和公共倉庫混合配置的方法,需要的朋友可以參考下
    2022-06-06
  • Java雙重校驗鎖單例原理

    Java雙重校驗鎖單例原理

    大家好,本篇文章主要講的是Java雙重校驗鎖單例原理,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01

最新評論

三门峡市| 青河县| 会泽县| 吴桥县| 千阳县| 宿迁市| 同德县| 湖南省| 喜德县| 孙吴县| 宜州市| 伊吾县| 任丘市| 长海县| 高雄县| 平谷区| 丰镇市| 织金县| 徐闻县| 泰宁县| 兴海县| 江山市| 卢龙县| 仙桃市| 牙克石市| 浦北县| 化德县| 孝感市| 达拉特旗| 贵港市| 拉孜县| 洪泽县| 安顺市| 邢台县| 高淳县| 安乡县| 堆龙德庆县| 江陵县| 荣成市| 苍溪县| 新津县|