springboot項(xiàng)目整合mybatis并配置mybatis中間件的實(shí)現(xiàn)
記錄創(chuàng)建springboot項(xiàng)目并配置mybatis中間件:
資源準(zhǔn)備及版本說(shuō)明
編程工具:IDEA
JDK版本:1.8
Maven版本:Apache Maven 3.6.3
springboot版本:2.4.4
mybatis版本:1.3.2
mysql版本:5.1.48
創(chuàng)建mavem項(xiàng)目
通過(guò)IDEA創(chuàng)建很便捷,參考《IDEA創(chuàng)建SpirngBoot項(xiàng)目》。
配置pom.xml
使用mybatis需要添加依賴
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
完整pom.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis.version>1.3.2</mybatis.version>
<mysql.version>5.1.48</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置application.yml
配置mybatis主要配置數(shù)據(jù)表映射實(shí)體類路徑type-aliases-package和數(shù)據(jù)表映射配置文件路徑mapper-locations
完整application.yml配置如下:

創(chuàng)建項(xiàng)目啟動(dòng)文件
在Application啟動(dòng)文件配置掃描持久化層的路徑的注解@MapperScan

代碼結(jié)構(gòu)
以user表為例子,創(chuàng)建controller目錄、dao目錄、service目錄、model目錄以及在resources目錄下創(chuàng)建mapper目錄用來(lái)保存映射xml文件。
完整代碼結(jié)構(gòu)如下:

映射實(shí)體類User:

持久層UserDao:
注意添加@Repository注解

業(yè)務(wù)層UserService:
創(chuàng)建根據(jù)ID查詢記錄的接口getById(Long id);

業(yè)務(wù)層接口實(shí)現(xiàn)類UserServiceImpl:
注意添加@Service注解,引入UserDao,實(shí)現(xiàn)根據(jù)ID`查詢記錄

控制層UserController:
注入業(yè)務(wù)層接口,增加測(cè)試查詢方法getUserById();

映射mapper文件:

其中namespace對(duì)應(yīng)持久化層dao的路徑,resultMap為數(shù)據(jù)表字段與實(shí)體映射類屬性的關(guān)聯(lián),type為實(shí)體映射類的路徑,select查詢配置中resultType為查詢結(jié)果的對(duì)象類型路徑。
啟動(dòng)項(xiàng)目
啟動(dòng)項(xiàng)目并訪問(wèn)http://localhost:8866/test測(cè)試配置情況

application.xml配置文件中增加日志輸出sql語(yǔ)句的配置:

重啟項(xiàng)目后再次測(cè)試接口:

springboot默認(rèn)使用HikariPool數(shù)據(jù)庫(kù)連接池。
到此這篇關(guān)于springboot項(xiàng)目整合mybatis并配置mybatis中間件的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot整合mybatis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot下mybatis-plus開啟打印sql日志的配置指南
- springboot整合mybatis的超詳細(xì)過(guò)程(配置模式+注解模式)
- springboot mybatis druid配置多數(shù)據(jù)源教程
- springboot配置多數(shù)據(jù)源后mybatis攔截器失效的解決
- SpringBoot整合Mybatis,解決TypeAliases配置失敗的問(wèn)題
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- 詳解Springboot-MyBatis配置-配置端口號(hào)與服務(wù)路徑(idea社區(qū)版2023.1.4+apache-maven-3.9.3-bin)
相關(guān)文章
SpringBoot四大神器之Actuator的使用小結(jié)
這篇文章主要介紹了SpringBoot四大神器之Actuator的使用小結(jié),詳細(xì)的介紹了Actuator的使用和端點(diǎn)的使用,有興趣的可以了解一下2017-11-11
Mybatis中多個(gè)對(duì)象包含同一個(gè)對(duì)象的處理操作
這篇文章主要介紹了Mybatis中多個(gè)對(duì)象包含同一個(gè)對(duì)象的處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
如何保證RabbitMQ全鏈路數(shù)據(jù)100%不丟失問(wèn)題
這篇文章主要介紹了如何保證RabbitMQ全鏈路數(shù)據(jù)100%不丟失問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
ElasticSearch突然采集不到日志問(wèn)題解決分析
這篇文章主要為大家介紹了ElasticSearch突然采集不到日志問(wèn)題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Mybatis-plus實(shí)現(xiàn)主鍵自增和自動(dòng)注入時(shí)間的示例代碼
這篇文章主要介紹了Mybatis-plus實(shí)現(xiàn)主鍵自增和自動(dòng)注入時(shí)間的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
在Struts2中如何將父類屬性序列化為JSON格式的解決方法
本篇文章,小編將為大家介紹關(guān)于在Struts2中如何將父類屬性序列化為JSON格式的解決方法,有需要的朋友可以參考一下2013-04-04

