国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

瀏覽:100日期:2023-03-17 10:27:37

記錄創(chuàng)建springboot項目并配置mybatis中間件:

資源準(zhǔn)備及版本說明

編程工具: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項目

通過IDEA創(chuàng)建很便捷,參考《IDEA創(chuàng)建SpirngBoot項目》。

配置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ù)表映射實體類路徑type-aliases-package和數(shù)據(jù)表映射配置文件路徑mapper-locations

完整application.yml配置如下:

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

創(chuàng)建項目啟動文件

在Application啟動文件配置掃描持久化層的路徑的注解@MapperScan

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

代碼結(jié)構(gòu)

以user表為例子,創(chuàng)建controller目錄、dao目錄、service目錄、model目錄以及在resources目錄下創(chuàng)建mapper目錄用來保存映射xml文件。

完整代碼結(jié)構(gòu)如下:

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

映射實體類User:

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

持久層UserDao:

注意添加@Repository注解

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

業(yè)務(wù)層UserService:

創(chuàng)建根據(jù)ID查詢記錄的接口getById(Long id);

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

業(yè)務(wù)層接口實現(xiàn)類UserServiceImpl:

注意添加@Service注解,引入UserDao,實現(xiàn)根據(jù)ID`查詢記錄

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

控制層UserController:

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

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

映射mapper文件:

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

其中namespace對應(yīng)持久化層dao的路徑,resultMap為數(shù)據(jù)表字段與實體映射類屬性的關(guān)聯(lián),type為實體映射類的路徑,select查詢配置中resultType為查詢結(jié)果的對象類型路徑。

啟動項目

啟動項目并訪問http://localhost:8866/test測試配置情況

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

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

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

重啟項目后再次測試接口:

springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)

springboot默認(rèn)使用HikariPool數(shù)據(jù)庫連接池。

到此這篇關(guān)于springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)的文章就介紹到這了,更多相關(guān)springboot整合mybatis內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 镇远县| 措勤县| 白水县| 会理县| 青田县| 满洲里市| 东兰县| 清远市| 余庆县| 新巴尔虎右旗| 封开县| 黄浦区| 榆树市| 资阳市| 甘南县| 锡林郭勒盟| 商南县| 景东| 宁蒗| 富川| 张家川| 武城县| 和龙市| 来安县| 辽宁省| 昌平区| 武冈市| 太仓市| 延庆县| 郸城县| 阳曲县| 兖州市| 柳州市| 策勒县| 二连浩特市| 紫云| 玛沁县| 鄂托克前旗| 新邵县| 封开县| 湖口县|