Springboot3整合Mybatis3的完整步驟記錄
一、導(dǎo)入依賴
mybatis 的必要依賴
注意:使用 springboot3 的話要使用 mybatis3 的版本以及 java17及以上的版本
<!-- mybaitis 依賴-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!--mysql鏈接依賴-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
二、編寫配置文件
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/user?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username: root
password: 200718
mybatis:
# mapper映射文件包掃描 (這里是對應(yīng) resources 的文件路徑)
mapper-locations: classpath:/mappers/*.xml
# 實(shí)體類別名包掃描
type-aliases-package: com.yun.pojo
三、定義模型 entity 實(shí)體類
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
private int id;
private String username;
private String password;
}四、在啟動類上添加注解,表示mapper接口所在位置
注意: 如果接口上面有 注解 @Mapper 的話,就可以不用在使用掃描包注解 @MapperScan 了(當(dāng)然兩個可以同時(shí)存在)
@SpringBootApplication
@MapperScan("com.example.mybaitis_01.mapper") // 掃描的mapper
public class Mybaitis01Application {
public static void main(String[] args) {
SpringApplication.run(Mybaitis01Application.class, args);
}
}
五、定義mapper接口
注意: 最好要加上 @Mapper注解,防止忘記開啟掃描
@Mapper
public interface TestMapper {
List<String> selectNameAll();
}
六、定義mapper.xml映射文件
注意:頭文件這里的網(wǎng)站鏈接是沒有 www 的,且能識別到 文件時(shí),里面的 SQL 是有顏色的,否則就是白色
<?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.yun.mappers.TestMapper">
<select id="selectNameAll" resultType="com.yun.pojo.User">
select * from tb_user
</select>
</mapper>
七、service層
注意: 接口和實(shí)現(xiàn)類最好把 @Service 加上,否則會出現(xiàn)找不到 bean 的問題
1、接口:
@Service
public interface TestService {
List<String> selectNameAll();
}
2、實(shí)現(xiàn)類:
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestMapper testMapper;
@Override
public List<String> selectNameAll() {
return testMapper.selectNameAll();
}
}
八、測試
這里測試是調(diào)用Service層的,也可以調(diào)用Mapper層來實(shí)現(xiàn) 查詢
@SpringBootTest
class Demo1ApplicationTests {
@Autowired
private TestService testService;
@Test
void contextLoads() {
System.out.println(testService.selectNameAll());
}
}補(bǔ)充:Springboot中Mybatis屬性映射--開啟駝峰命名
mybatis默認(rèn)是屬性名和數(shù)據(jù)庫字段名一一對應(yīng)的,即
- 數(shù)據(jù)庫表列:user_name
- 實(shí)體類屬性:user_name
但是java中一般使用駝峰命名
- 數(shù)據(jù)庫表列:user_name
- 實(shí)體類屬性:userName
在Springboot中,可以通過設(shè)置map-underscore-to-camel-case屬性為true來開啟駝峰功能。
application.yml中:
mybatis:
configuration:
map-underscore-to-camel-case: true
application.properties中:
mybatis.configuration.map-underscore-to-camel-case=true
總結(jié)
到此這篇關(guān)于Springboot3整合Mybatis3的文章就介紹到這了,更多相關(guān)Springboot3整合Mybatis3內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ArrayList在for循環(huán)中使用remove方法移除元素方法介紹
這篇文章主要介紹了ArrayList在for循環(huán)中使用remove方法移除元素的內(nèi)容,介紹了具體代碼實(shí)現(xiàn),需要的朋友可以參考下。2017-09-09
SpringBoot中驗(yàn)證用戶上傳的圖片資源的方法
這篇文章主要介紹了在SpringBoot中驗(yàn)證用戶上傳的圖片資源,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Java實(shí)現(xiàn)的zip工具類完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的zip工具類,結(jié)合完整實(shí)例形式分析了Java針對zip文件指定路徑壓縮、遞歸壓縮等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
Maven發(fā)布封裝到中央倉庫時(shí)候報(bào)錯:no default secret key
這篇文章主要介紹了Maven發(fā)布封裝到中央倉庫時(shí)候報(bào)錯:no default secret key,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Spring Cloud實(shí)現(xiàn)跨語言微服務(wù)通信的三種方案
本文深入探討Spring Cloud實(shí)現(xiàn)跨語言微服務(wù)通信的三大核心方案: Sidecar模式:通過代理非Java服務(wù),性能提升5倍,延遲從150ms降至30ms ASM+MSE方案:結(jié)合阿里云服務(wù)網(wǎng)格,吞吐量提升3倍至2400TPS Feign+自定義序列化,需要的朋友可以參考下2026-05-05
詳解Java程序并發(fā)的Wait-Notify機(jī)制
這篇文章主要介紹了詳解Java程序并發(fā)的Wait-Notify機(jī)制,多線程并發(fā)是Java編程中的重要部分,需要的朋友可以參考下2015-07-07
SpringCloud筆記(Hoxton)Netflix之Ribbon負(fù)載均衡示例代碼
這篇文章主要介紹了SpringCloud筆記HoxtonNetflix之Ribbon負(fù)載均衡,Ribbon是管理HTTP和TCP服務(wù)客戶端的負(fù)載均衡器,Ribbon具有一系列帶有名稱的客戶端(Named?Client),對SpringCloud?Ribbon負(fù)載均衡相關(guān)知識感興趣的朋友一起看看吧2022-06-06
淺談一下Java為什么不能使用字符流讀取非文本的二進(jìn)制文件
這篇文章主要介紹了淺談一下為什么不能使用字符流讀取非文本的二進(jìn)制文件,剛學(xué)Java的IO流部分時(shí),書上說只能使用字節(jié)流去讀取圖片、視頻等非文本二進(jìn)制文件,不能使用字符流,否則文件會損壞,需要的朋友可以參考下2023-04-04

