Spring整合MyBatis圖示過程解析
這篇文章主要介紹了Spring整合MyBatis圖示過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
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實體類
3.service層接口
4.serviceimpl實現(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"/> <!--注冊DAO層:mapper的代理對象--> <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層對象--> <bean id="personService" class="com.spring.service.PersonServiceImpl"> <property name="dao" ref="personDao"></property> </bean> <!-- sqlSessionFactory 創(chuàng)建SqlSession對象的工廠 --> <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ù)的隔離級別和傳播行為--> <tx:method name="get*" isolation="READ_COMMITTED" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>
8.測試類
結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot整合MybatisPlus的實現(xiàn)過程解析
- SpringBoot整合MyBatis實現(xiàn)樂觀鎖和悲觀鎖的示例
- SpringBoot整合Mybatis實現(xiàn)CRUD
- SpringBoot整合MyBatis-Plus3.1教程詳解
- Spring boot整合Mybatis實現(xiàn)級聯(lián)一對多CRUD操作的完整步驟
- 通過Spring Boot整合Mybatis分析自動配置詳解
- SpringBoot整合MyBatisPlus配置動態(tài)數(shù)據(jù)源的方法
- Spring Boot整合MyBatis連接Oracle數(shù)據(jù)庫的步驟全紀(jì)錄
相關(guān)文章
使用spring aop 統(tǒng)一捕獲異常和寫日志的示例demo
本文通過一個小demo給大家介紹spring AOP 實現(xiàn)的異常捕獲和日志的方法技巧,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-08-08SpringBoot快速搭建web項目詳細(xì)步驟總結(jié)
這篇文章主要介紹了SpringBoot快速搭建web項目詳細(xì)步驟總結(jié) ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12spring profile 多環(huán)境配置管理詳解
這篇文章主要介紹了 spring profile 多環(huán)境配置管理詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01詳解spring cloud分布式整合zipkin的鏈路跟蹤
這篇文章主要介紹了詳解spring cloud分布式整合zipkin的鏈路跟蹤,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07IDEA新建bootstrap.yml文件不顯示葉子圖標(biāo)的問題
這篇文章主要介紹了IDEA新建bootstrap.yml文件不顯示葉子圖標(biāo)的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07SpringBoot實現(xiàn)動態(tài)數(shù)據(jù)源切換的項目實踐
在實際開發(fā)過程中,我們經(jīng)常遇到需要同時操作多個數(shù)據(jù)源的情況,本文主要介紹了SpringBoot實現(xiàn)動態(tài)數(shù)據(jù)源切換的項目實踐,具有一定的參考價值,感興趣的可以了解一下2024-04-04