最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

以 MySQL 為例。

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

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

2. 添加 MyBatis 依賴(lài)

第三方的依賴(lài)庫(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ù)源類(lèi)型: 默認(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í)體類(lèi)的掃描路徑
  type-aliases-package: com.testabc.demo.ssmtest
  configuration:
    # 開(kāi)啟駝峰命名轉(zhuǎn)換
    map-underscore-to-camel-case: true
    # 開(kāi)啟日志
    #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. 功能開(kāi)發(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類(lèi)

結(jié)合表結(jié)構(gòu),創(chuàng)建普通的一個(gè)bean類(lèi)。此時(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 注解。先寫(xiě)上吧,沒(méi)有副作用。

package com.testabc.demo.ssmtest;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Mapper
public interface StudentMapper {
    // 根據(jù)id查詢(xún)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類(lèi)

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)類(lèi)上添加注解 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)文章

  • Feign+mybatisplus搭建項(xiàng)目遇到的坑及解決

    Feign+mybatisplus搭建項(xiàng)目遇到的坑及解決

    這篇文章主要介紹了Feign+mybatisplus搭建項(xiàng)目遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • @ConfigurationProperties(prefix=““)不起作用問(wèn)題及解決方案

    @ConfigurationProperties(prefix=““)不起作用問(wèn)題及解決方案

    這段文章主要討論了在Spring Boot項(xiàng)目中使用@ConfigurationProperties進(jìn)行YAML配置自動(dòng)導(dǎo)入時(shí)遇到的問(wèn)題,并分享了三種常見(jiàn)的錯(cuò)誤解決方法,幫助開(kāi)發(fā)者避免相同錯(cuò)誤
    2026-06-06
  • 利用solr實(shí)現(xiàn)商品的搜索功能(實(shí)例講解)

    利用solr實(shí)現(xiàn)商品的搜索功能(實(shí)例講解)

    下面小編就為大家分享一篇利用solr實(shí)現(xiàn)商品的搜索功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • 簡(jiǎn)單的用java實(shí)現(xiàn)讀/寫(xiě)文本文件的示例

    簡(jiǎn)單的用java實(shí)現(xiàn)讀/寫(xiě)文本文件的示例

    同時(shí)也展示了如果從輸入流中讀出來(lái)內(nèi)容寫(xiě)入輸出流中(僅限文本流) 三個(gè)例子可以獨(dú)立存在,所以根據(jù)需要只看其中一個(gè)就行了。
    2008-07-07
  • mybatis多對(duì)多關(guān)聯(lián)實(shí)戰(zhàn)教程(推薦)

    mybatis多對(duì)多關(guān)聯(lián)實(shí)戰(zhàn)教程(推薦)

    下面小編就為大家?guī)?lái)一篇mybatis多對(duì)多關(guān)聯(lián)實(shí)戰(zhàn)教程(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • 泛型的類(lèi)型擦除后fastjson反序列化時(shí)如何還原詳解

    泛型的類(lèi)型擦除后fastjson反序列化時(shí)如何還原詳解

    這篇文章主要為大家介紹了泛型的類(lèi)型擦除后fastjson反序列化時(shí)如何還原詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • nacos配置在代碼中引用的方法講解

    nacos配置在代碼中引用的方法講解

    這篇文章主要介紹了nacos配置在代碼中如何引用,如果主配置中配置的內(nèi)容和拓展配置的內(nèi)容重復(fù)則按主配置的配置 ,如果拓展配置中的內(nèi)容和另一個(gè)拓展配置中的內(nèi)容重復(fù),則按下標(biāo)大的配置作為最終的配置,對(duì)nacos配置代碼引用相關(guān)知識(shí)感興趣朋友一起看看吧
    2022-12-12
  • 關(guān)于EntityWrapper的in用法

    關(guān)于EntityWrapper的in用法

    這篇文章主要介紹了關(guān)于EntityWrapper的in用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java的JSON格式轉(zhuǎn)換庫(kù)GSON的初步使用筆記

    Java的JSON格式轉(zhuǎn)換庫(kù)GSON的初步使用筆記

    GSON是Google開(kāi)發(fā)并在在GitHub上開(kāi)源的Java對(duì)象與JSON互轉(zhuǎn)功能類(lèi)庫(kù),在Android開(kāi)發(fā)者中也大受歡迎,這里我們就來(lái)看一下Java的JSON格式轉(zhuǎn)換庫(kù)GSON的初步使用筆記:
    2016-06-06
  • 淺談spring方法級(jí)參數(shù)校驗(yàn)(@Validated)

    淺談spring方法級(jí)參數(shù)校驗(yàn)(@Validated)

    這篇文章主要介紹了淺談spring方法級(jí)參數(shù)校驗(yàn)(@Validated),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評(píng)論

临沧市| 定陶县| 十堰市| 体育| 葵青区| 安溪县| 云和县| 兴化市| 门头沟区| 曲沃县| 沙田区| 小金县| 顺义区| 家居| 青龙| 治县。| 敦化市| 保亭| 开远市| 万年县| 阿尔山市| 霍城县| 乐陵市| 岫岩| 山东省| 福贡县| 梅河口市| 丘北县| 义乌市| 阳朔县| 屏边| 龙川县| 宁河县| 金乡县| 淳化县| 高淳县| 浏阳市| 自贡市| 策勒县| 沧州市| 盘山县|