一篇超詳細(xì)的Spring Boot整合Mybatis文章
配置文件形式
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 https://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>com.keafmd</groupId> <artifactId>spring-boot-09-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-09-mybatis</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置數(shù)據(jù)源
在yml文件中配置數(shù)據(jù)源。
application.yml:
server: port: 80 # 配置數(shù)據(jù)源 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 18044229 # 整合mybatis mybatis: # typeAliasesPackage: com.neuedu.entity mapper-locations: classpath*:com/neuedu/boot/mapper/*.xml
UserMapper.xml
這里注意!!!:一定是和UserMapper相同的目錄,是個(gè)三級(jí)目錄,創(chuàng)建時(shí)仿照這樣創(chuàng)建com/keafm/mapper(正確的) 別這樣com.keafam.mapper(錯(cuò)誤的),這樣錯(cuò)誤的創(chuàng)建的話,是個(gè)一級(jí)目錄,不是三級(jí)的,后面運(yùn)行的時(shí)候可能會(huì)提示找不到Mapper。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.keafmd.mapper.UserMapper"> <select id="list" resultType="map"> select * from user </select> </mapper>
UserMapper
package com.keafmd.mapper; import org.apache.ibatis.annotations.Mapper; import java.util.List; /** * Keafmd * * @ClassName: UserMapper * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-08 16:09 * @Blog: https://keafmd.blog.csdn.net/ */ public interface UserMapper { List list(); }
配置springboot整合mybatis
在application.yml中配置:
# 整合mybatis mybatis: # typeAliasesPackage: com.neuedu.entity mapper-locations: classpath*:com/neuedu/boot/mapper/*.xml
在運(yùn)行類上添加@MapperScan注解
SpringBoot09MybatisApplication:
package com.keafmd; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.keafmd.mapper") public class SpringBoot09MybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringBoot09MybatisApplication.class, args); } }
測(cè)試類
UserMapperTest :
package com.keafmd.mapper; import com.keafmd.SpringBoot09MybatisApplication; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest(classes = SpringBoot09MybatisApplication.class) class UserMapperTest { @Autowired UserMapper userMapper; @Test void list(){ List list = userMapper.list(); for (Object o : list) { System.out.println(o); } } }
效果
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Spring Boot2配置Swagger2生成API接口文檔詳情
這篇文章主要介紹了Spring Boot2配置Swagger2生成API接口文檔詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Java實(shí)現(xiàn)分庫分表實(shí)踐指南
在開發(fā)中我們經(jīng)常使用到分庫分表,但是一般是我們前期就已經(jīng)做了規(guī)劃,對(duì)數(shù)據(jù)庫怎么劃分,對(duì)哪些表進(jìn)行分表,這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)分庫分表的相關(guān)資料,需要的朋友可以參考下2024-01-01Jenkins集成sonarQube實(shí)現(xiàn)代碼質(zhì)量檢查過程圖解
這篇文章主要介紹了Jenkins集成sonarQube實(shí)現(xiàn)代碼質(zhì)量檢查過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Spring @ExceptionHandler注解統(tǒng)一異常處理和獲取方法名
這篇文章主要介紹了Spring注解之@ExceptionHandler 統(tǒng)一異常處理和獲取方法名,在實(shí)際項(xiàng)目中,合理使用@ExceptionHandler能夠提高代碼的可維護(hù)性和用戶體驗(yàn),通過本文的解析和實(shí)踐,讀者可以更好地理解和掌握@ExceptionHandler的用法和原理2023-09-09Springboot整合GateWay+Nacos實(shí)現(xiàn)動(dòng)態(tài)路由
本文主要介紹了Springboot整合GateWay+Nacos實(shí)現(xiàn)動(dòng)態(tài)路由,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08從0構(gòu)建Oauth2Server服務(wù)之Refreshing-access-tokens
這篇文章主要為大家介紹了從0構(gòu)建Oauth2Server服務(wù)之Refreshing-access-tokens刷新令牌示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Java實(shí)現(xiàn)EasyCaptcha圖形驗(yàn)證碼的具體使用
Java圖形驗(yàn)證碼,支持gif、中文、算術(shù)等類型,可用于Java Web、JavaSE等項(xiàng)目,下面就跟隨小編一起來了解一下2021-08-08Java?POI導(dǎo)出Excel時(shí)合并單元格沒有邊框的問題解決
這篇文章主要給大家介紹了關(guān)于Java?POI導(dǎo)出Excel時(shí)合并單元格沒有邊框的問題解決辦法,文中通過代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07微服務(wù)Spring?Boot?整合Redis?阻塞隊(duì)列實(shí)現(xiàn)異步秒殺下單思路詳解
這篇文章主要介紹了微服務(wù)Spring?Boot?整合Redis?阻塞隊(duì)列實(shí)現(xiàn)異步秒殺下單,使用阻塞隊(duì)列實(shí)現(xiàn)秒殺的優(yōu)化,采用異步秒殺完成下單的優(yōu)化,本文給大家分享詳細(xì)步驟及實(shí)現(xiàn)思路,需要的朋友可以參考下2022-10-10