Spring整合MyBatis圖示過程解析
這篇文章主要介紹了Spring整合MyBatis圖示過程解析,文中通過示例代碼介紹的非常詳細(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)過程解析
- SpringBoot整合MyBatis實(shí)現(xiàn)樂觀鎖和悲觀鎖的示例
- SpringBoot整合Mybatis實(shí)現(xiàn)CRUD
- SpringBoot整合MyBatis-Plus3.1教程詳解
- Spring boot整合Mybatis實(shí)現(xiàn)級(jí)聯(lián)一對(duì)多CRUD操作的完整步驟
- 通過Spring Boot整合Mybatis分析自動(dòng)配置詳解
- SpringBoot整合MyBatisPlus配置動(dòng)態(tài)數(shù)據(jù)源的方法
- Spring Boot整合MyBatis連接Oracle數(shù)據(jù)庫的步驟全紀(jì)錄
相關(guān)文章
MyBatis-Plus?條件查詢器的實(shí)現(xiàn)
本文主要介紹了MyBatis-Plus?條件查詢器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Mybatis批量插入返回成功的數(shù)目實(shí)例
這篇文章主要介紹了Mybatis批量插入返回成功的數(shù)目實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Springboot?Vue實(shí)現(xiàn)單點(diǎn)登陸功能示例詳解
這篇文章主要為大家介紹了Springboot?Vue實(shí)現(xiàn)單點(diǎn)登陸功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
SpringMVC生成的驗(yàn)證碼圖片不顯示問題及解決方法
這篇文章主要介紹了SpringMVC生成的驗(yàn)證碼圖片不顯示問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
SpringBoot Redis緩存數(shù)據(jù)實(shí)現(xiàn)解析
這篇文章主要介紹了SpringBoot Redis緩存數(shù)據(jù)實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Java的Struts框架中配置國(guó)際化的資源存儲(chǔ)的要點(diǎn)解析
這篇文章主要介紹了Java的Struts框架中配置國(guó)際化的資源存儲(chǔ)的要點(diǎn)解析,針對(duì)用戶所使用的語言來配置資源文件,需要的朋友可以參考下2016-04-04
Java異常javax.net.ssl.SSLHandshakeException: SSL的解決方法
在Java開發(fā)過程中,SSL(Secure Sockets Layer)握手異常是一個(gè)常見的網(wǎng)絡(luò)通信錯(cuò)誤,特別是在使用HTTPS協(xié)議進(jìn)行安全通信時(shí),本文將詳細(xì)分析javax.net.ssl.SSLHandshakeException: SSL這一異常的背景、可能的原因,并通過代碼示例幫助您理解和解決這一問題2024-12-12

