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需要添加依賴(lài)
<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í)體類(lèi)路徑type-aliases-package和數(shù)據(jù)表映射配置文件路徑mapper-locations
完整application.yml配置如下:
在Application啟動(dòng)文件配置掃描持久化層的路徑的注解@MapperScan
以u(píng)ser表為例子,創(chuàng)建controller目錄、dao目錄、service目錄、model目錄以及在resources目錄下創(chuàng)建mapper目錄用來(lái)保存映射xml文件。
完整代碼結(jié)構(gòu)如下:
映射實(shí)體類(lèi)User:
持久層UserDao:
注意添加@Repository注解
業(yè)務(wù)層UserService:
創(chuàng)建根據(jù)ID查詢(xún)記錄的接口getById(Long id);
業(yè)務(wù)層接口實(shí)現(xiàn)類(lèi)UserServiceImpl:
注意添加@Service注解,引入U(xiǎn)serDao,實(shí)現(xiàn)根據(jù)ID`查詢(xún)記錄
控制層UserController:
注入業(yè)務(wù)層接口,增加測(cè)試查詢(xún)方法getUserById();
映射mapper文件:
其中namespace對(duì)應(yīng)持久化層dao的路徑,resultMap為數(shù)據(jù)表字段與實(shí)體映射類(lèi)屬性的關(guān)聯(lián),type為實(shí)體映射類(lèi)的路徑,select查詢(xún)配置中resultType為查詢(xún)結(jié)果的對(duì)象類(lèi)型路徑。
啟動(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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. CSS清除浮動(dòng)方法匯總2. js開(kāi)發(fā)中的頁(yè)面、屏幕、瀏覽器的位置原理(高度寬度)說(shuō)明講解(附圖)3. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)4. CSS百分比padding制作圖片自適應(yīng)布局5. vue跳轉(zhuǎn)頁(yè)面常用的幾種方法匯總6. 不要在HTML中濫用div7. XML入門(mén)的常見(jiàn)問(wèn)題(三)8. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)9. 深入了解React中的合成事件10. TypeScript實(shí)現(xiàn)十大排序算法之歸并排序示例詳解
