欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot3整合Mybatis完整版實例

 更新時間:2025年01月25日 11:47:34   作者:m0_74824574  
本文詳細(xì)介紹了SpringBoot3整合MyBatis的完整步驟,包括添加數(shù)據(jù)庫驅(qū)動和MyBatis依賴、配置數(shù)據(jù)源和MyBatis、創(chuàng)建表和Bean類、編寫Mapper接口和XML文件、創(chuàng)建Controller類以及配置掃描包,通過這些步驟,可以實現(xiàn)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()格式化輸出方式

    關(guān)于String.format()格式化輸出方式

    String.format()是Java的格式化輸出方法,支持多種數(shù)據(jù)類型和格式化選項,它在格式化和拼接字符串時具有較高的靈活性,但效率相對較低,特別是在處理大量數(shù)據(jù)時,在實際編程中,應(yīng)根據(jù)具體需求選擇合適的字符串拼接方式
    2024-12-12
  • Hibernate實現(xiàn)many-to-many的映射關(guān)系

    Hibernate實現(xiàn)many-to-many的映射關(guān)系

    今天小編就為大家分享一篇關(guān)于Hibernate實現(xiàn)many-to-many的映射關(guān)系,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Spring?IOC容器使用詳細(xì)講解

    Spring?IOC容器使用詳細(xì)講解

    IOC-Inversion?of?Control,即控制反轉(zhuǎn)。它不是什么技術(shù),而是一種設(shè)計思想。這篇文章將為大家介紹一下Spring控制反轉(zhuǎn)IOC的原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-12-12
  • SpringCloud Feign參數(shù)問題及解決方法

    SpringCloud Feign參數(shù)問題及解決方法

    這篇文章主要介紹了SpringCloud Feign參數(shù)問題及解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • Java經(jīng)典設(shè)計模式之觀察者模式原理與用法詳解

    Java經(jīng)典設(shè)計模式之觀察者模式原理與用法詳解

    這篇文章主要介紹了Java經(jīng)典設(shè)計模式之觀察者模式,簡單分析了觀察者模式的概念、原理并結(jié)合實例形式給出了java觀察者模式的具體用法與相關(guān)注意事項,需要的朋友可以參考下
    2017-08-08
  • Spring Boot 中使用 JSON Schema 校驗復(fù)雜JSON數(shù)據(jù)的過程

    Spring 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-08
  • Mybatis 入門示例代碼之 Association

    Mybatis 入門示例代碼之 Association

    這篇文章主要介紹了Mybatis 入門示例代碼之 Association,需要的的朋友參考下
    2017-02-02
  • 獲取Spring當(dāng)前配置的兩種方式

    獲取Spring當(dāng)前配置的兩種方式

    這篇文章主要給大家介紹了獲取Spring當(dāng)前配置的,兩種方式文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • Java微信掃碼登錄功能并實現(xiàn)認(rèn)證授權(quán)全過程

    Java微信掃碼登錄功能并實現(xiàn)認(rèn)證授權(quán)全過程

    這篇文章主要給大家介紹了關(guān)于Java微信掃碼登錄功能并實現(xiàn)認(rèn)證授權(quán)的相關(guān)資料,要在Java中實現(xiàn)微信掃碼登錄,您可以按照以下步驟進行操作,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • SpringMVC+Mybatis二維碼實現(xiàn)多平臺付款(附源碼)

    SpringMVC+Mybatis二維碼實現(xiàn)多平臺付款(附源碼)

    本文主要實現(xiàn)微信支付寶等支付平臺合多為一的二維碼支付,并且實現(xiàn)有效時間內(nèi)支付有效,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評論