当前位置: 代码网 > it编程>编程语言>Java > Spring Boot 中使用 Mybatis Plus的操作方法

Spring Boot 中使用 Mybatis Plus的操作方法

2024年12月20日 Java 我要评论
spring boot 中使用 mybatis plus在现代的企业级开发中,mybatis plus 是 mybatis 的增强工具,它简化了很多常见的数据库操作。通过 spring boot 集成

spring boot 中使用 mybatis plus

在现代的企业级开发中,mybatis plus 是 mybatis 的增强工具,它简化了很多常见的数据库操作。通过 spring boot 集成 mybatis plus,可以快速构建高效、简洁的数据库操作层。本文将介绍如何在 spring boot 项目中集成 mybatis plus。

1. 添加 maven 依赖

在spring boot 项目的 pom.xml 文件中添加如下相关的依赖:

<dependency>
  <groupid>com.mysql</groupid>
  <artifactid>mysql-connector-j</artifactid>
</dependency>
<dependency>
  <groupid>com.baomidou</groupid>
  <artifactid>mybatis-plus-boot-starter</artifactid>
  <version>3.5.7</version>
</dependency>

2. 在配置文件中加入相关配置

spring:
  datasource:
    url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/test?useunicode=true&characterencoding=utf-8&autoreconnect=true&usessl=false&zerodatetimebehavior=converttonull
    username: root
    password: xxxxxx
    hikari: # 数据库连接池
      minimum-idle: 5 # 连接池最小空闲连接数
      maximum-pool-size: 20 # 连接池最大连接数
      auto-commit: true # 自动提交从连接池中返回的连接
      idle-timeout: 30000 # 连接允许在连接池中闲置的最长时间
      pool-name: springbootdemo-hikaricp # 连接池的用户定义名称
      max-lifetime: 1800000 # 连接池中连接最长生命周期
      connection-timeout: 30000 # 等待来自连接池的连接的最大毫秒数
      connection-test-query: select 1 # 连接池连接测试语句

3. 创建 mybatis 配置类

// 扫描 mapper 所在包路径
@mapperscan("com.xxxx.xxx")
@configuration
public class mybatisplusconfig {
  	/**
     * 添加 mybatis plus 分页插件
     */
    @bean
    public mybatisplusinterceptor mybatisplusinterceptor() {
        mybatisplusinterceptor mybatisplusinterceptor = new mybatisplusinterceptor();
        mybatisplusinterceptor.addinnerinterceptor(new paginationinnerinterceptor());
        return mybatisplusinterceptor;
    }
}

4. 创建实体类

public class user {
    @tableid(type = idtype.auto)
    private long id;
    private string name;
    private integer age;
    @tablelogic
    private boolean isdeleted;
    public long getid() {
        return id;
    }
    public void setid(long id) {
        this.id = id;
    }
    public string getname() {
        return name;
    }
    public void setname(string name) {
        this.name = name;
    }
    public integer getage() {
        return age;
    }
    public void setage(integer age) {
        this.age = age;
    }
    public boolean isdeleted() {
        return isdeleted;
    }
    public void setdeleted(boolean deleted) {
        isdeleted = deleted;
    }
}

5.创建mapper

mybatis plus 提供了基础的 mapper 接口,继承它即可拥有常用的增、删、改、查功能。

public interface usermapper extends basemapper<user> {
}

6. 创建 service

mybatis plus 的 serviceimpl 是实现了 iservice 接口的抽象类, 对 mapper 进行了增强封装

@service
public class userserviceimpl extends serviceimpl<usermapper, user> implements userservice {
}

service接口层

public interface userservice extends iservice<user> {
}

7. 结论

本文介绍了如何在 spring boot 项目中集成 mybatis plus,spring boot 与 mybatis plus 的集成非常简单,通过自动配置和简洁的 api,可以大大减少开发中常见的数据库操作代码。mybatis plus 提供了很多实用的功能,如分页查询、条件构造、自动填充等,能大大提高开发效率。

到此这篇关于spring boot 中使用 mybatis plus的文章就介绍到这了,更多相关spring boot使用 mybatis plus内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com