MyBatis無縫對(duì)接Spring的方法
1.為什么會(huì)出現(xiàn)MyBatis-Spring
Spring框架與MyBatis框架是Java互聯(lián)網(wǎng)技術(shù)的主流框架。但是如何將MyBatis無縫整合到Spring框架中呢?這時(shí)候就誕生了MyBatis-Spring。使用這個(gè)類庫中得類,Spring將會(huì)加載必要的MyBatis工廠類和session類。
Spring3.0也僅僅支持ibatis2.0。本來將MyBatis3的支持添加到Spring3.0中。而不幸,Spring3.0的開發(fā)在MyBatis3.0官方發(fā)布前就結(jié)束了。因?yàn)镾pring開發(fā)團(tuán)隊(duì)不想發(fā)布一個(gè)非發(fā)布版的MyBatis的整合支持。就放棄了對(duì)MyBatis的支持。
隨著Spring越來越成為java事實(shí)標(biāo)準(zhǔn)的技術(shù)框架。Spring 4.0 移除了對(duì)iBatis的直接支持。MyBatis團(tuán)隊(duì)開發(fā)出來了基于Spring的MyBatis整合Jar---MyBatis-Spring。
2.使用MyBatis-Spring的好處
1.使得業(yè)務(wù)層和模型層得到更好的分離。再Spring框架中MyBatis也更加簡(jiǎn)單,節(jié)約不少的代碼
2.甚至不需要顯示的使用SqlSessionFactory、SqlSessiond等對(duì)象
3.MyBatis-Spring組成部分
1.配置數(shù)據(jù)源
2.配置SqlSessionFactory
3.配置SqlSessionTemplate
4.配置Mapper
5.事務(wù)處理
MyBatis中要構(gòu)建SqlSessionFactory對(duì)象,讓它產(chǎn)生SqlSession,而在MyBatis-Spring項(xiàng)目中SqlSession的使用是通過SqlSessionTemplate來實(shí)現(xiàn)的,它提供了對(duì)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>
注意點(diǎn)
SqlSessionFactory 有一個(gè)單獨(dú)的必須屬性,就是 JDBC 的 DataSource
在SqlSessionFactoryBean的返回getObject的時(shí)候,有個(gè)驗(yàn)證。
@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
這里我們使用的是阿里巴巴的數(shù)據(jù)庫連接池。https://github.com/alibaba/druid
compile group: 'com.alibaba', name: 'druid', version: '1.1.2' <!--阿里巴巴的數(shù)據(jù)庫連接池--> <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配置數(shù)據(jù)庫鏈接屬性
在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
使用阿里巴巴的數(shù)據(jù)庫連接池是無需配置數(shù)據(jù)庫驅(qū)動(dòng)。是根據(jù)url的前綴來判斷的
4.4配置Spring加載配置屬性及配置替換動(dòng)態(tài)標(biāo)簽
<!--加載配置文件路徑--> <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:properties/datasource.properties"></property> </bean> <!--獲取配置屬性之后動(dòng)態(tài)替換標(biāo)簽--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties"/> </bean>
4.5配置Spring自動(dòng)創(chuàng)建Mapper接口的bean
MyBatis-Spring提供了一個(gè)轉(zhuǎn)換器MapperScannerConfigurer,可以將映射接口轉(zhuǎn)換為Spring容器中Bean。這樣就可以在代碼中注入映射的bean
<!--采用自動(dòng)掃描方式創(chuàng)建Mapper Bean--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cnblogs.dao"></property> </bean>
basePackage屬性是讓你為映射器接口文件設(shè)置基本的包路徑。 你可以使用分號(hào)或逗號(hào)作為分隔符設(shè)置多于一個(gè)的包路徑。每個(gè)映射器將會(huì)在指定的包路徑中遞歸地被搜索到。
4.6配置事務(wù)
MyBatis和Spring結(jié)合后是使用Spring AOP去管理事務(wù)的,使用Spring AOP是相當(dāng)?shù)暮?jiǎn)單的,它分為聲明式事務(wù)和編程性事務(wù)。大部分場(chǎng)景下使用聲明式事務(wù)就可以了。
引入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' <!--事務(wù)管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--使用聲明式事務(wù)管理方式--> <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可以自動(dòng)去掃描base-pack下面的包或者子包下面的Java文件--> <context:component-scan base-package="com.cnblogs.controller,com.cnblogs.service,com.cnblogs.dao"/> <!--自動(dòng)注冊(cè)RequestMappingHandlerMapping與RequestMappingHandlerAdpter--> <mvc:annotation-driven/> <!-- 對(duì)靜態(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> <!--獲取配置屬性之后動(dòng)態(tài)替換標(biāo)簽--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties"/> </bean> <!--阿里巴巴的數(shù)據(jù)庫連接池--> <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> <!--構(gòu)建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> <!--采用自動(dòng)掃描方式創(chuàng)建Mapper Bean--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cnblogs.dao"></property> </bean> <!--事務(wù)管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--使用聲明式事務(wù)管理方式--> <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>
總結(jié)
以上所述是小編給大家介紹的MyBatis無縫對(duì)接Spring的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringBoot整合EasyCaptcha實(shí)現(xiàn)圖形驗(yàn)證碼功能
這篇文章主要介紹了SpringBoot整合EasyCaptcha實(shí)現(xiàn)圖形驗(yàn)證碼功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-02-02IDEA運(yùn)行導(dǎo)入的javaweb項(xiàng)目tomcat正常,但是運(yùn)行失敗404問題
這篇文章主要介紹了IDEA運(yùn)行導(dǎo)入的javaweb項(xiàng)目tomcat正常但是運(yùn)行失敗404問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Java?axios與spring前后端分離傳參規(guī)范總結(jié)
這篇文章主要介紹了Java?axios與spring前后端分離傳參規(guī)范總結(jié),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08Jackson使用示例-Bean、XML、Json之間相互轉(zhuǎn)換
Jackson是一個(gè)強(qiáng)大工具,可用于Json、XML、實(shí)體之間的相互轉(zhuǎn)換,JacksonXmlElementWrapper用于指定List等集合類,外圍標(biāo)簽名,JacksonXmlProperty指定包裝標(biāo)簽名,或者指定標(biāo)簽內(nèi)部屬性名,JacksonXmlRootElement指定生成xml根標(biāo)簽的名字,JacksonXmlText指定當(dāng)前這個(gè)值2024-05-05python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌
這篇文章主要介紹了python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04Java超詳細(xì)教你寫一個(gè)斗地主洗牌發(fā)牌系統(tǒng)
這篇文章主要介紹了怎么用Java來你寫一個(gè)斗地主種洗牌和發(fā)牌的功能,斗地主相信大家都知道,同時(shí)也知道每一局都要洗牌打亂順序再發(fā)牌,本篇我們就來實(shí)現(xiàn)這個(gè)功能,感興趣的朋友跟隨文章往下看看吧2022-03-03解決SpringBoot中的Scheduled單線程執(zhí)行問題
在一次SpringBoot中使用Scheduled定時(shí)任務(wù)時(shí),發(fā)現(xiàn)某一個(gè)任務(wù)出現(xiàn)執(zhí)行占用大量資源,會(huì)導(dǎo)致其他任務(wù)也執(zhí)行失敗,這篇文章主要介紹了SpringBoot中的Scheduled單線程執(zhí)行問題及解決方法,需要的朋友可以參考下2022-06-06