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>
二、編寫(xiě)配置文件
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映射文件包掃描 (這里是對(duì)應(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注解,防止忘記開(kāi)啟掃描
@Mapper public interface TestMapper { List<String> selectNameAll(); }
六、定義mapper.xml映射文件
注意:頭文件這里的網(wǎng)站鏈接是沒(méi)有 www 的,且能識(shí)別到 文件時(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 加上,否則會(huì)出現(xiàn)找不到 bean 的問(wèn)題
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(); } }
八、測(cè)試
這里測(cè)試是調(diào)用Service層的,也可以調(diào)用Mapper層來(lái)實(shí)現(xiàn) 查詢
@SpringBootTest class Demo1ApplicationTests { @Autowired private TestService testService; @Test void contextLoads() { System.out.println(testService.selectNameAll()); } }
補(bǔ)充:Springboot中Mybatis屬性映射--開(kāi)啟駝峰命名
mybatis默認(rèn)是屬性名和數(shù)據(jù)庫(kù)字段名一一對(duì)應(yīng)的,即
- 數(shù)據(jù)庫(kù)表列:user_name
- 實(shí)體類屬性:user_name
但是java中一般使用駝峰命名
- 數(shù)據(jù)庫(kù)表列:user_name
- 實(shí)體類屬性:userName
在Springboot中,可以通過(guò)設(shè)置map-underscore-to-camel-case屬性為true來(lái)開(kāi)啟駝峰功能。
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Spring?Boot使用Undertow服務(wù)的方法
Undertow是RedHAT紅帽公司開(kāi)源的產(chǎn)品,采用JAVA開(kāi)發(fā),是一款靈活,高性能的web服務(wù)器,提供了NIO的阻塞/非阻塞API,也是Wildfly的默認(rèn)Web容器,這篇文章給大家介紹了在Spring?Boot使用Undertow服務(wù)的方法,感興趣的朋友跟隨小編一起看看吧2023-05-05列舉java語(yǔ)言中反射的常用方法及實(shí)例代碼
反射機(jī)制指的是程序在運(yùn)行時(shí)能夠獲取自身的信息。這篇文章主要介紹了列舉java語(yǔ)言中反射的常用方法,需要的朋友可以參考下2019-07-07關(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-11SpringBoot使用validation做參數(shù)校驗(yàn)說(shuō)明
這篇文章主要介紹了SpringBoot使用validation做參數(shù)校驗(yàn)說(shuō)明,首先通過(guò)添加hibernate-validator展開(kāi)全文內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考與喜愛(ài)2022-04-04SpringBoot接口惡意刷新和暴力請(qǐng)求的解決方法
在實(shí)際項(xiàng)目使用中,必須要考慮服務(wù)的安全性,當(dāng)服務(wù)部署到互聯(lián)網(wǎng)以后,就要考慮服務(wù)被惡意請(qǐng)求和暴力攻擊的情況,所以本文給大家介紹了SpringBoot接口惡意刷新和暴力請(qǐng)求的解決方法,需要的朋友可以參考下2024-11-11詳解如何更改SpringBoot TomCat運(yùn)行方式
這篇文章主要介紹了詳解如何更改SpringBoot TomCat運(yùn)行方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04SpringBoot使用責(zé)任鏈模式優(yōu)化業(yè)務(wù)邏輯中的if-else代碼
在開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)遇到需要根據(jù)不同的條件執(zhí)行不同的邏輯的情況,我們可以考慮使用責(zé)任鏈模式來(lái)優(yōu)化代碼結(jié)構(gòu),使得代碼更加清晰、可擴(kuò)展和易于維護(hù)2023-06-06