SpringBoot3整合Mybatis完整版實例
本文記錄一下完整的 SpringBoot3 整合 Mybatis 的步驟。
只要按照本步驟來操作,整合完成后就可以正常使用。
1. 添加數(shù)據(jù)庫驅(qū)動依賴
以 MySQL 為例。
當(dāng)不指定 依賴版本的時候,會 由 springboot 自動管理。
<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <!-- <version>8.0.32</version> --> </dependency>
2. 添加 MyBatis 依賴
第三方的依賴庫,需要明確的指定版本號。推薦使用最新的即可。
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
3. 配置數(shù)據(jù)源信息
在 application.yaml 文件中添加數(shù)據(jù)源的信息
spring: datasource: # 數(shù)據(jù)庫連接驅(qū)動 driver-class-name: com.mysql.cj.jdbc.Driver # 數(shù)據(jù)源類型: 默認(rèn)的是 Hikari type: com.zaxxer.hikari.HikariDataSource # 數(shù)據(jù)庫連接地址 url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai # 數(shù)據(jù)庫連接用戶名 username: root # 數(shù)據(jù)庫連接密碼 password: 12345678
4. 配置 mybatis
在 application.yaml 文件中添加mybatis的相關(guān)配置。
# mybatis 的配置 mybatis: # 配置 mybatis 的xml文件的掃描路徑 mapper-locations: classpath:mybatis/**/*.xml # 配置實體類的掃描路徑 type-aliases-package: com.testabc.demo.ssmtest configuration: # 開啟駝峰命名轉(zhuǎn)換 map-underscore-to-camel-case: true # 開啟日志 #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl # 指定日志級別 : 對mybatis的日志輸出 logging: level: com.testabc.demo.ssmtest: debug
5. 功能開發(fā)
5.1 建表
簡單創(chuàng)建一張表。包含了普通屬性,標(biāo)準(zhǔn)的下劃線屬性。
CREATE TABLE `test`.`student` ( `id` int NOT NULL, `name` varchar(20) NOT NULL, `age` int NOT NULL, `other_message` varchar(100) NULL, PRIMARY KEY (`id`) );
5.2 創(chuàng)建普通的bean類
結(jié)合表結(jié)構(gòu),創(chuàng)建普通的一個bean類。此時屬性用標(biāo)準(zhǔn)的駝峰命名。
package com.testabc.demo.ssmtest; public class Student { private int id; private String name; private int age; private String otherMessage; 。。。。。。 構(gòu)造方法 getter/setter toString 方法 }
5.3 創(chuàng)建mapper接口
注意 : 此處的接口用到了 @Mapper
注解。先寫上吧,沒有副作用。
package com.testabc.demo.ssmtest; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Mapper public interface StudentMapper { // 根據(jù)id查詢student的方法 Student getStudentById(@Param("id") int id); }
5.4 創(chuàng)建xml文件
在 classpath:/resources/mybatis/ 目錄下新增 StudentMapper.xml
文件。
<?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.testabc.demo.ssmtest.StudentMapper"> <select id="getStudentById" resultType="com.testabc.demo.ssmtest.Student"> select * from student where id = #{id} </select> </mapper>
5.5 創(chuàng)建controller類
package com.testabc.demo.ssmtest; @RestController public class StudentController { /** * 通過構(gòu)造方法的方式注入 StudentMapper */ private final StudentMapper studentMapper; public StudentController(StudentMapper studentMapper) { this.studentMapper = studentMapper; } @GetMapping("/getStudentById/{id}") public Student getStudentById(@PathVariable("id") int id){ Student student = null; student = studentMapper.getStudentById(id); return student; } }
5.6 配置掃描的包
在 項目的 啟動類上添加注解 MapperScan(xxxx)
, 指定要掃描的 mapper 接口的包路徑。
package com.testabc.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.testabc.demo.ssmtest") public class DemoApplication { public static void main(String[] args) { // 這個工具會返回一個 ApplicationContext 的對象 var ioc = SpringApplication.run(DemoApplication.class, args); } }
6. 功能測試
瀏覽器中訪問測試。
成功,至此,已經(jīng)完成了 SpringBoot3 整合 Mybatis 的步驟。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于String.format()格式化輸出方式
String.format()是Java的格式化輸出方法,支持多種數(shù)據(jù)類型和格式化選項,它在格式化和拼接字符串時具有較高的靈活性,但效率相對較低,特別是在處理大量數(shù)據(jù)時,在實際編程中,應(yīng)根據(jù)具體需求選擇合適的字符串拼接方式2024-12-12Hibernate實現(xiàn)many-to-many的映射關(guān)系
今天小編就為大家分享一篇關(guān)于Hibernate實現(xiàn)many-to-many的映射關(guān)系,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03SpringCloud Feign參數(shù)問題及解決方法
這篇文章主要介紹了SpringCloud Feign參數(shù)問題及解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12Java經(jīng)典設(shè)計模式之觀察者模式原理與用法詳解
這篇文章主要介紹了Java經(jīng)典設(shè)計模式之觀察者模式,簡單分析了觀察者模式的概念、原理并結(jié)合實例形式給出了java觀察者模式的具體用法與相關(guān)注意事項,需要的朋友可以參考下2017-08-08Spring Boot 中使用 JSON Schema 校驗復(fù)雜JSO
在數(shù)據(jù)交換領(lǐng)域,JSON Schema 以其強大的標(biāo)準(zhǔn)化能力,為定義和規(guī)范 JSON 數(shù)據(jù)的結(jié)構(gòu)與規(guī)則提供了有力支持,下面給大家介紹Spring Boot 中使用 JSON Schema 校驗復(fù)雜JSON數(shù)據(jù)的過程,感興趣的朋友跟隨小編一起看看吧2024-08-08Java微信掃碼登錄功能并實現(xiàn)認(rèn)證授權(quán)全過程
這篇文章主要給大家介紹了關(guān)于Java微信掃碼登錄功能并實現(xiàn)認(rèn)證授權(quán)的相關(guān)資料,要在Java中實現(xiàn)微信掃碼登錄,您可以按照以下步驟進行操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10SpringMVC+Mybatis二維碼實現(xiàn)多平臺付款(附源碼)
本文主要實現(xiàn)微信支付寶等支付平臺合多為一的二維碼支付,并且實現(xiàn)有效時間內(nèi)支付有效,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08