SpringBoot3整合Mybatis完整版实例

 更新时间:2025年01月25日 11:47:34   作者:m0_74824574  
本文详细介绍了SpringBoot3整合MyBatis的完整步骤,包括添加数据库驱动和MyBatis依赖、配置数据源和MyBatis、创建表和Bean类、编写Mapper接口和XML文件、创建Controller类以及配置扫描包,通过这些步骤,可以实现SpringBoot3与MyBatis的成功整合,并进行功能测试

本文记录一下完整的 SpringBoot3 整合 Mybatis 的步骤。

只要按照本步骤来操作,整合完成后就可以正常使用。

1. 添加数据库驱动依赖

以 MySQL 为例。

当不指定 依赖版本的时候,会 由 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. 配置数据源信息

在 application.yaml 文件中添加数据源的信息

spring:
  datasource:
    # 数据库连接驱动
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 数据源类型: 默认的是 Hikari
    type: com.zaxxer.hikari.HikariDataSource
    # 数据库连接地址
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    # 数据库连接用户名
    username: root
    # 数据库连接密码
    password: 12345678

4. 配置 mybatis

在 application.yaml 文件中添加mybatis的相关配置。

# mybatis 的配置
mybatis:
  # 配置 mybatis 的xml文件的扫描路径
  mapper-locations: classpath:mybatis/**/*.xml
  # 配置实体类的扫描路径
  type-aliases-package: com.testabc.demo.ssmtest
  configuration:
    # 开启驼峰命名转换
    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. 功能开发

5.1 建表

简单创建一张表。包含了普通属性,标准的下划线属性。

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 创建普通的bean类

结合表结构,创建普通的一个bean类。此时属性用标准的驼峰命名

package com.testabc.demo.ssmtest;

public class Student {
    private int id;
    private String name;
    private int age;
    private String otherMessage;

    。。。。。。
    构造方法
    getter/setter
    toString 方法

}

5.3 创建mapper接口

注意 : 此处的接口用到了 @Mapper 注解。先写上吧,没有副作用。

package com.testabc.demo.ssmtest;

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

@Mapper
public interface StudentMapper {
    // 根据id查询student的方法
    Student getStudentById(@Param("id") int id);
}

5.4 创建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 创建controller类

package com.testabc.demo.ssmtest;

@RestController
public class StudentController {

    /**
     * 通过构造方法的方式注入 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. 功能测试

浏览器中访问测试。

成功,至此,已经完成了 SpringBoot3 整合 Mybatis 的步骤。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Feign+mybatisplus搭建项目遇到的坑及解决

    Feign+mybatisplus搭建项目遇到的坑及解决

    这篇文章主要介绍了Feign+mybatisplus搭建项目遇到的坑及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • @ConfigurationProperties(prefix=““)不起作用问题及解决方案

    @ConfigurationProperties(prefix=““)不起作用问题及解决方案

    这段文章主要讨论了在Spring Boot项目中使用@ConfigurationProperties进行YAML配置自动导入时遇到的问题,并分享了三种常见的错误解决方法,帮助开发者避免相同错误
    2026-06-06
  • 利用solr实现商品的搜索功能(实例讲解)

    利用solr实现商品的搜索功能(实例讲解)

    下面小编就为大家分享一篇利用solr实现商品的搜索功能,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-11-11
  • 简单的用java实现读/写文本文件的示例

    简单的用java实现读/写文本文件的示例

    同时也展示了如果从输入流中读出来内容写入输出流中(仅限文本流) 三个例子可以独立存在,所以根据需要只看其中一个就行了。
    2008-07-07
  • mybatis多对多关联实战教程(推荐)

    mybatis多对多关联实战教程(推荐)

    下面小编就为大家带来一篇mybatis多对多关联实战教程(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • 泛型的类型擦除后fastjson反序列化时如何还原详解

    泛型的类型擦除后fastjson反序列化时如何还原详解

    这篇文章主要为大家介绍了泛型的类型擦除后fastjson反序列化时如何还原详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • nacos配置在代码中引用的方法讲解

    nacos配置在代码中引用的方法讲解

    这篇文章主要介绍了nacos配置在代码中如何引用,如果主配置中配置的内容和拓展配置的内容重复则按主配置的配置 ,如果拓展配置中的内容和另一个拓展配置中的内容重复,则按下标大的配置作为最终的配置,对nacos配置代码引用相关知识感兴趣朋友一起看看吧
    2022-12-12
  • 关于EntityWrapper的in用法

    关于EntityWrapper的in用法

    这篇文章主要介绍了关于EntityWrapper的in用法详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java的JSON格式转换库GSON的初步使用笔记

    Java的JSON格式转换库GSON的初步使用笔记

    GSON是Google开发并在在GitHub上开源的Java对象与JSON互转功能类库,在Android开发者中也大受欢迎,这里我们就来看一下Java的JSON格式转换库GSON的初步使用笔记:
    2016-06-06
  • 浅谈spring方法级参数校验(@Validated)

    浅谈spring方法级参数校验(@Validated)

    这篇文章主要介绍了浅谈spring方法级参数校验(@Validated),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06

最新评论