Spring整合MyBatis圖示過(guò)程解析
這篇文章主要介紹了Spring整合MyBatis圖示過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1.導(dǎo)入所需要的jar依賴
!--MyBatis和Spring的整合包 由MyBatis提供--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--MyBatis的核心jar文件--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency>
2.MyBatis和Spring整合(XML版)
1.dao接口
2.entity實(shí)體類
3.service層接口
4.serviceimpl實(shí)現(xiàn)類
5.PersonDao.xml配置
6.jdbc.properties配置
7.大配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--<context:component-scan base-package="com.spring"/>--> <!-- 配置數(shù)據(jù)源 spring內(nèi)置的數(shù)據(jù)源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 引入屬性文件 --> <context:property-placeholder location="jdbc.properties.properties"/> <!--注冊(cè)DAO層:mapper的代理對(duì)象--> <bean id="personDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.spring.dao.PersonDao"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!--配置service層對(duì)象--> <bean id="personService" class="com.spring.service.PersonServiceImpl"> <property name="dao" ref="personDao"></property> </bean> <!-- sqlSessionFactory 創(chuàng)建SqlSession對(duì)象的工廠 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- Mybatis的大配置文件 --> <property name="typeAliasesPackage" value="com.spring.entity"></property> <!-- 掃描sql配置文件:mapper需要的xml文件 --> <property name="mapperLocations" value="classpath*:mapper/*.xml"/> </bean> <!-- MapperScannerConfigurer 掃描mapper文件掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.spring.dao"></property> </bean> <!-- transactionManager 事務(wù)管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事務(wù)通知--> <tx:advice transaction-manager="transactionManager" id="txAdvice"> <tx:attributes> <!--get系列方法設(shè)置事務(wù)的隔離級(jí)別和傳播行為--> <tx:method name="get*" isolation="READ_COMMITTED" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>
8.測(cè)試類
結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot整合MybatisPlus的實(shí)現(xiàn)過(guò)程解析
- SpringBoot整合MyBatis實(shí)現(xiàn)樂(lè)觀鎖和悲觀鎖的示例
- SpringBoot整合Mybatis實(shí)現(xiàn)CRUD
- SpringBoot整合MyBatis-Plus3.1教程詳解
- Spring boot整合Mybatis實(shí)現(xiàn)級(jí)聯(lián)一對(duì)多CRUD操作的完整步驟
- 通過(guò)Spring Boot整合Mybatis分析自動(dòng)配置詳解
- SpringBoot整合MyBatisPlus配置動(dòng)態(tài)數(shù)據(jù)源的方法
- Spring Boot整合MyBatis連接Oracle數(shù)據(jù)庫(kù)的步驟全紀(jì)錄
相關(guān)文章
使用spring aop 統(tǒng)一捕獲異常和寫日志的示例demo
本文通過(guò)一個(gè)小demo給大家介紹spring AOP 實(shí)現(xiàn)的異常捕獲和日志的方法技巧,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-08-08SpringBoot快速搭建web項(xiàng)目詳細(xì)步驟總結(jié)
這篇文章主要介紹了SpringBoot快速搭建web項(xiàng)目詳細(xì)步驟總結(jié) ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12spring profile 多環(huán)境配置管理詳解
這篇文章主要介紹了 spring profile 多環(huán)境配置管理詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01詳解spring cloud分布式整合zipkin的鏈路跟蹤
這篇文章主要介紹了詳解spring cloud分布式整合zipkin的鏈路跟蹤,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07IDEA新建bootstrap.yml文件不顯示葉子圖標(biāo)的問(wèn)題
這篇文章主要介紹了IDEA新建bootstrap.yml文件不顯示葉子圖標(biāo)的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07SpringBoot實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的項(xiàng)目實(shí)踐
在實(shí)際開發(fā)過(guò)程中,我們經(jīng)常遇到需要同時(shí)操作多個(gè)數(shù)據(jù)源的情況,本文主要介紹了SpringBoot實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04