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;
}四、在啟動(dòng)類上添加注解,表示mapper接口所在位置
注意: 如果接口上面有 注解 @Mapper 的話,就可以不用在使用掃描包注解 @MapperScan 了(當(dāng)然兩個(gè)可以同時(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)文章
在Spring?Boot使用Undertow服務(wù)的方法
Undertow是RedHAT紅帽公司開源的產(chǎn)品,采用JAVA開發(fā),是一款靈活,高性能的web服務(wù)器,提供了NIO的阻塞/非阻塞API,也是Wildfly的默認(rèn)Web容器,這篇文章給大家介紹了在Spring?Boot使用Undertow服務(wù)的方法,感興趣的朋友跟隨小編一起看看吧2023-05-05
關(guān)于LocalDateTime最常用方法和時(shí)間轉(zhuǎn)換方式
Java8版本引入了LocalDateTime和LocalDate類,極大地方便了日期和時(shí)間的處理,本文主要介紹了字符串與LocalDateTime的互轉(zhuǎn),Long型時(shí)間戳與UTC時(shí)間字符串的轉(zhuǎn)換,獲取今天、某天的起止時(shí)間,自定義時(shí)間的設(shè)置,以及LocalDateTime與Date的相互轉(zhuǎn)換等常用方法2024-11-11
SpringBoot使用validation做參數(shù)校驗(yàn)說明
這篇文章主要介紹了SpringBoot使用validation做參數(shù)校驗(yàn)說明,首先通過添加hibernate-validator展開全文內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考與喜愛2022-04-04
詳解如何更改SpringBoot TomCat運(yùn)行方式
這篇文章主要介紹了詳解如何更改SpringBoot TomCat運(yùn)行方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
SpringBoot使用責(zé)任鏈模式優(yōu)化業(yè)務(wù)邏輯中的if-else代碼
在開發(fā)過程中,我們經(jīng)常會遇到需要根據(jù)不同的條件執(zhí)行不同的邏輯的情況,我們可以考慮使用責(zé)任鏈模式來優(yōu)化代碼結(jié)構(gòu),使得代碼更加清晰、可擴(kuò)展和易于維護(hù)2023-06-06

