如何详细操作springboot整合mybatisplus?

2026-05-25 23:311阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1141个文字,预计阅读时间需要5分钟。

如何详细操作springboot整合mybatisplus?

目录 + Mybatis-Plus 介绍 + 简介 + 特性(官网提供) + 一、引入POM依赖 + 二、配置文件application.yml + 三、编写表映射实体类 + 四、编写Mapper + 五、测试Controller + 六、启动类 + Mybatis-Plus 介绍 + 简介 + MyBatis-Plus

目录
  • Mybatis-Plus介绍
    • 简介
    • 特性(官网提供)
  • 一、引入POM依赖
    • 二、配置文件application.yml
      • 三、编写表映射实体类
        • 四、编写Mapper
          • 五、测试Controller
            • 六、启动类

              Mybatis-Plus介绍

              简介

              MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

              如何详细操作springboot整合mybatisplus?

              特性(官网提供)

              • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
              • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作,BaseMapper
              • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求,简单的CRUD操作不用自己编写。
              • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
              • 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
              • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
              • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
              • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用(自动生成代码)
              • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
              • 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
              • 内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
              • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

              一、引入POM依赖

              <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency>

              二、配置文件application.yml

              spring: datasource: url: jdbc:mysql://172.26.0.296:3306/he?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai username: root password: P0de driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: mapper-locations: classpath:mapper/*.xml

              三、编写表映射实体类

              @TableName("sys_user") // 指定表名 public class UserEntity { private String id; private String username; public String id() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }

              四、编写Mapper

              简单写个注解sql

              public interface TestMapper extends BaseMapper<UserEntity> { @Select("select id from sys_user limit 1") String getId(); }

              五、测试Controller

              测试通过:QueryWrapper方式查询 + 注解方式查询。

              @RestController @RequestMapping("/wechat/portal") public class WechatController { @Autowired private TestMapper testMapper; @GetMapping("/test") public String getTest() {] // QueryWrapper方式查询 QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>(); List<UserEntity> userEntities = testMapper.selectList(queryWrapper); System.out.println("userEntities --- " + userEntities); // 注解方式查询 String id = testMapper.getId(); System.out.println("id ---" + id); return userEntities.toString(); } }

              六、启动类

              通过@MapperScan指定mapper所在包路径。

              @SpringBootApplication @MapperScan("org.jeecg.modules.mp.mapper") // 指定mapper包路径 public class WxMpDemoApplication { public static void main(String[] args) { SpringApplication.run(WxMpDemoApplication.class, args); } }

              到此这篇关于springboot四步集成mybatisplus的文章就介绍到这了,更多相关springboot集成mybatisplus内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

              标签:详细

              本文共计1141个文字,预计阅读时间需要5分钟。

              如何详细操作springboot整合mybatisplus?

              目录 + Mybatis-Plus 介绍 + 简介 + 特性(官网提供) + 一、引入POM依赖 + 二、配置文件application.yml + 三、编写表映射实体类 + 四、编写Mapper + 五、测试Controller + 六、启动类 + Mybatis-Plus 介绍 + 简介 + MyBatis-Plus

              目录
              • Mybatis-Plus介绍
                • 简介
                • 特性(官网提供)
              • 一、引入POM依赖
                • 二、配置文件application.yml
                  • 三、编写表映射实体类
                    • 四、编写Mapper
                      • 五、测试Controller
                        • 六、启动类

                          Mybatis-Plus介绍

                          简介

                          MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

                          如何详细操作springboot整合mybatisplus?

                          特性(官网提供)

                          • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
                          • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作,BaseMapper
                          • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求,简单的CRUD操作不用自己编写。
                          • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
                          • 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
                          • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
                          • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
                          • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用(自动生成代码)
                          • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
                          • 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
                          • 内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
                          • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

                          一、引入POM依赖

                          <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency>

                          二、配置文件application.yml

                          spring: datasource: url: jdbc:mysql://172.26.0.296:3306/he?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai username: root password: P0de driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: mapper-locations: classpath:mapper/*.xml

                          三、编写表映射实体类

                          @TableName("sys_user") // 指定表名 public class UserEntity { private String id; private String username; public String id() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }

                          四、编写Mapper

                          简单写个注解sql

                          public interface TestMapper extends BaseMapper<UserEntity> { @Select("select id from sys_user limit 1") String getId(); }

                          五、测试Controller

                          测试通过:QueryWrapper方式查询 + 注解方式查询。

                          @RestController @RequestMapping("/wechat/portal") public class WechatController { @Autowired private TestMapper testMapper; @GetMapping("/test") public String getTest() {] // QueryWrapper方式查询 QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>(); List<UserEntity> userEntities = testMapper.selectList(queryWrapper); System.out.println("userEntities --- " + userEntities); // 注解方式查询 String id = testMapper.getId(); System.out.println("id ---" + id); return userEntities.toString(); } }

                          六、启动类

                          通过@MapperScan指定mapper所在包路径。

                          @SpringBootApplication @MapperScan("org.jeecg.modules.mp.mapper") // 指定mapper包路径 public class WxMpDemoApplication { public static void main(String[] args) { SpringApplication.run(WxMpDemoApplication.class, args); } }

                          到此这篇关于springboot四步集成mybatisplus的文章就介绍到这了,更多相关springboot集成mybatisplus内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

                          标签:详细