亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot3整合Mybatis完整版實(shí)例

 更新時(shí)間:2025年01月25日 11:47:34   作者:m0_74824574  
本文詳細(xì)介紹了SpringBoot3整合MyBatis的完整步驟,包括添加數(shù)據(jù)庫(kù)驅(qū)動(dòng)和MyBatis依賴、配置數(shù)據(jù)源和MyBatis、創(chuàng)建表和Bean類、編寫Mapper接口和XML文件、創(chuàng)建Controller類以及配置掃描包,通過(guò)這些步驟,可以實(shí)現(xiàn)SpringBoot3與MyBatis的成功整合,并進(jìn)行功能測(cè)試

本文記錄一下完整的 SpringBoot3 整合 Mybatis 的步驟。

只要按照本步驟來(lái)操作,整合完成后就可以正常使用。

1. 添加數(shù)據(jù)庫(kù)驅(qū)動(dòng)依賴

以 MySQL 為例。

當(dāng)不指定 依賴版本的時(shí)候,會(huì) 由 springboot 自動(dòng)管理。

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <!-- <version>8.0.32</version> -->
</dependency>

2. 添加 MyBatis 依賴

第三方的依賴庫(kù),需要明確的指定版本號(hào)。推薦使用最新的即可。

<!-- 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ù)庫(kù)連接驅(qū)動(dòng)
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 數(shù)據(jù)源類型: 默認(rèn)的是 Hikari
    type: com.zaxxer.hikari.HikariDataSource
    # 數(shù)據(jù)庫(kù)連接地址
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    # 數(shù)據(jù)庫(kù)連接用戶名
    username: root
    # 數(shù)據(jù)庫(kù)連接密碼
    password: 12345678

4. 配置 mybatis

在 application.yaml 文件中添加mybatis的相關(guān)配置。

# mybatis 的配置
mybatis:
  # 配置 mybatis 的xml文件的掃描路徑
  mapper-locations: classpath:mybatis/**/*.xml
  # 配置實(shí)體類的掃描路徑
  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

# 指定日志級(jí)別 : 對(duì)mybatis的日志輸出
logging:
  level:
    com.testabc.demo.ssmtest: debug

5. 功能開發(fā)

5.1 建表

簡(jiǎn)單創(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)建普通的一個(gè)bean類。此時(shí)屬性用標(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 注解。先寫上吧,沒(méi)有副作用。

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 {

    /**
     * 通過(guò)構(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 配置掃描的包

在 項(xiàng)目的 啟動(dòng)類上添加注解 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) {

        // 這個(gè)工具會(huì)返回一個(gè) ApplicationContext 的對(duì)象
        var ioc = SpringApplication.run(DemoApplication.class, args);

    }

}

6. 功能測(cè)試

瀏覽器中訪問(wèn)測(cè)試。

成功,至此,已經(jīng)完成了 SpringBoot3 整合 Mybatis 的步驟。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

    Spring Boot 中使用 JSON Schema 校驗(yàn)復(fù)雜JSO

    在數(shù)據(jù)交換領(lǐng)域,JSON Schema 以其強(qiáng)大的標(biāo)準(zhǔn)化能力,為定義和規(guī)范 JSON 數(shù)據(jù)的結(jié)構(gòu)與規(guī)則提供了有力支持,下面給大家介紹Spring Boot 中使用 JSON Schema 校驗(yàn)復(fù)雜JSON數(shù)據(jù)的過(guò)程,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • Mybatis 入門示例代碼之 Association

    Mybatis 入門示例代碼之 Association

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

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

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

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

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

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

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

最新評(píng)論