文章目录

  • 【README】
  • 【1】springboot集成mybatis-plus配置
    • 【1.1】目录结构
      • 【相关说明】
    • 【1.2】代码示例
      • 【pom.xml】
      • 【application.properties】
      • 【MybatisPlusNoteController】
      • 【UserAppService】
      • 【UserMapper】
      • 【UserPO】
      • 【建表语句】
  • 【2】演示

【README】

本文代码参见: https://github.com/TomJourney/mybatis-plus-test

本文集成了springboot与mybatis-plus,并提供了一个mybatis-plus的简单应用;

【使用MyBatisPlus的关键】springboot集成了mybatis的基础上,使用mybatis-plus就2步:

  • 步骤1)引入mybatis-plus依赖,删除 mybatis-spring-boot-starter 依赖;
  • 步骤2)业务Mapper如UserMapper继承MyBatis-Plus中的BaseMapper;

【pom.xml中spring-mybatis与mybaits-plus依赖配置】

<!-- mybatis conf (新增mybatis-plus-boot-starter依赖,需要把mybatis-spring-boot-starter注释或删除,否则报包冲突)-->
<!--    <dependency>-->
<!--      <groupId>org.mybatis.spring.boot</groupId>-->
<!--      <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!--      <version>3.0.3</version>-->
<!--    </dependency>-->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.12</version>
</dependency>
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>3.0.3</version>
</dependency>


【1】springboot集成mybatis-plus配置

【1.1】目录结构

在这里插入图片描述

【相关说明】

本文项目使用了springboot, lombok,mybatis-plus;采用不规范的三层架构(为了求快),正式的生产环境不要参考本文代码;

补充:规范的代码应该是4层架构,包括adapter,app,domain,infra (有兴趣的同学参考ddd);



【1.2】代码示例

【pom.xml】

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.3.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.tom.studynote</groupId><artifactId>mybatis-plus-test</artifactId><version>0.0.1-SNAPSHOT</version><name>mybatis-plus-test</name><description>mybatis-plus-test</description><url/><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>3.3.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.29</version></dependency><!--【数据库】数据库连接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.11</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>provided</scope></dependency><!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct --><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId><version>1.6.2</version></dependency><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>1.6.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- mybatis conf (新增mybatis-plus-boot-starter依赖,需要把mybatis-spring-boot-starter注释或删除,否则报包冲突)--><!--    <dependency>--><!--      <groupId>org.mybatis.spring.boot</groupId>--><!--      <artifactId>mybatis-spring-boot-starter</artifactId>--><!--      <version>3.0.3</version>--><!--    </dependency>--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.12</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>3.0.3</version></dependency></dependencies><build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource><resource><directory>src/main/resources</directory></resource></resources><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

【application.properties】

server.port = 8081# spring datasource conf
spring.datasource.url=jdbc:mysql://localhost:3306/mywarn?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource# druid
spring.datasource.druid.initial-size=1
spring.datasource.druid.max-active=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-wait=60000# mybatis conf
mybatis.mapper-locations=classpath:com/mybatisplustest/infrastructure/dao/**/*.xml
mybatis.config-location=classpath:mybatis-config.xml
check-config-location=true# template path
spring.web.resources.static-locations=classpath:/templates# mybaits-plus
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

【TomMybatisPlusNoteApplication】

@SpringBootApplication
@MapperScan(basePackages = {"com.tom.study.mybatisplustest.infrastructure.dao"})
public class TomMybatisPlusNoteApplication {public static void main(String[] args) {SpringApplication.run(TomMybatisPlusNoteApplication.class, args);}
}

【MybatisPlusNoteController】

@RestController
@RequestMapping("/mybatis-plus-note")
public class MybatisPlusNoteController {@Autowiredprivate UserAppService userAppService;@GetMapping("/user/{id}")public UserPO findUserById(@PathVariable("id") String id) {return userAppService.findUserById(id);}@PostMapping(path = "/add-user", consumes = "application/json")public void addUser(@RequestBody UserPO userPO) {userAppService.saveNewUser(userPO);}
}

【UserAppService】

@Service
public class UserAppService {@AutowiredUserMapper userMapper;public UserPO findUserById(String id) {return userMapper.selectById(id);}public void saveNewUser(UserPO UserPO) {userMapper.insert(UserPO);}
}

【UserMapper】

【关键】业务Mapper-UserMapper只需要继承BaseMapper即可使用mybatis-plus 提供的api

public interface UserMapper extends BaseMapper<UserPO> {
}

【UserPO】

@Data
@TableName("user_tbl")
public class UserPO {private Long id;private String name;private String mobilePhone;private String addr;
}

【建表语句】

-- mywarn.user_tbl definitionCREATE TABLE `user_tbl` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '用户名称',`mobile_phone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '移动电话',`addr` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '地址',`user_state` char(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '用户状态/ON-在线/OFF-离线',`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,`last_modify_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=117 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户表';


【2】演示

请求路径:localhost:8081/mybatis-plus-note/add-user

报文:

{"id": 10,"name": "user10","mobilePhone": "17712340010","addr": "成都天府三街010号"
}

在这里插入图片描述



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/bicheng/87670.shtml
繁体地址,请注明出处:http://hk.pswp.cn/bicheng/87670.shtml
英文地址,请注明出处:http://en.pswp.cn/bicheng/87670.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VR小鼠解剖虚拟仿真:开启生命科学教育新视野​

VR 小鼠解剖虚拟仿真&#xff0c;是一项将虚拟现实(VR)技术深度融入小鼠解剖学习与研究过程的创新应用&#xff0c;即 VR 小鼠解剖虚拟仿真。其核心原理在于&#xff0c;借助 VR 技术所构建的高度逼真的虚拟环境&#xff0c;突破了传统小鼠解剖在时间、空间以及实体操作上的诸多…

计算机网络(网页显示过程,TCP三次握手,HTTP1.0,1.1,2.0,3.0,JWT cookie)

前言 最近一直在后端开发的面经&#x1f64c;&#xff0c;里面涉及到了好多计算机网络的知识&#x1f601;&#xff0c;在这里以问题的形式写一个学习笔记&#xff08;其中参考了: JavaGuide 和 小林coding 这两个很好的学习网站&#x1f618;&#xff09; 1.当键入网址后&am…

Redis 消息的发布和订阅

Redis 消息的发布和订阅 1、什么是发布和订阅 Redis 发布订阅 (pub/sub) 是一种消息通信模式&#xff1a;发送者 (pub) 发送消息&#xff0c;订阅者 (sub) 接收消息。 Redis 客户端可以订阅任意数量的频道。 2、Redis的发布和订阅示意 1、客户端可以订阅频道如下图 2、当…

python优先队列使用

heapq 是 Python 的一个内置模块&#xff0c;提供了堆队列算法的实现&#xff0c;也称为优先队列算法。以下是关于 heapq 模块的详细使用说明。 基本概念 堆&#xff1a;一种特殊的二叉树结构&#xff0c;满足父节点总是小于或等于其子节点&#xff08;最小堆&#xff09;特性…

在 Windows 机器上安装和配置 RabbitMQ

RabbitMQ 它是一款基于 AMQP&#xff08;高级消息队列协议&#xff09;的流行消息代理。RabbitMQ 适用于 Windows、Linux 和 macOS&#xff0c;易于安装和使用&#xff0c;并提供一系列强大的消息队列和路由功能。要在 Windows 计算机上使用 RabbitMQ&#xff0c;您必须先安装 …

第十五节:第六部分:日志技术:logback的核心配置文件详解、日志级别

核心配置文件logback.xml 什么是日志级别&#xff0c;为什么要学日志级别

从入门到精通:数据库全攻略

目录一、数据库基础概念1.1 数据库定义1.2 数据库与文件系统的区别1.3 数据库系统组成部分1.4 关系型数据库与非关系型数据库二、数据库安装与配置2.1 下载 MySQL2.2 安装 MySQL2.3 初始化数据库服务器2.4 启动和停止 MySQL 服务2.5 登录 MySQL2.6 创建数据库2.7 创建数据表三、…

【JAVA】消息队列(MQ)是个好东西

一、前言再JAVA系统开发中&#xff0c;再高并发的场景经常需要使用到消息队列&#xff0c;有时候是不得不使用到消息对了。特别是大数据量的并发处理。对数据实时性要求又没那么高的情况下。用户请求 → 接入层(Nginx) → 限流 → 消息队列 → 订单服务 → 库存服务 → 支付服务…

【Golang面试题】Go结构体的特点,与其它语言的区别

Go 结构体深度解析&#xff1a;与 C/C、Java 的全面对比 一、核心概念对比特性Go 结构体 (struct)C/C 结构体 (struct)Java 类 (class)本质值类型复合数据类型值类型复合数据类型引用类型内存分配栈或堆 (编译器决定)栈 (显式控制)堆 (JVM管理)默认访问权限首字母大写导出publi…

CppCon 2018 学习:OOP is dead, long live Data-oriented design

探讨了面向对象编程&#xff08;OOP&#xff09;的一些根本性问题深入理解&#xff1a; 标题&#xff1a;What is so wrong with OOP? 什么是面向对象的问题&#xff1f; 这不是说 OOP “绝对错误”&#xff0c;而是指出它在实践中经常引发的问题&#xff0c;尤其是在性能敏…

科学的第五范式:人工智能如何重塑发现之疆

在人类探索未知的壮阔史诗中&#xff0c;科学方法的演进如同照亮迷雾的灯塔。从基于经验的第一范式&#xff08;描述自然现象&#xff09;&#xff0c;到以理论推演为核心的第二范式&#xff08;牛顿定律、麦克斯韦方程&#xff09;&#xff0c;再到以计算机模拟为标志的第三范…

tmux 左下角会话名显示不全的解决方法

在 tmux 中显示完整的会话名 有时候我们要在服务器上长时间跑某个任务&#xff0c;但不可能时时刻刻保持终端模拟器开启&#xff0c;这时候就需要用到 tmux &#xff0c;可以在关闭会话的同时让任务继续在后台跑&#xff0c;后续还可以连回来。但在 tmux 会话中&#xff0c;左…

【期末分布式】分布式的期末考试资料大题整理

&#x1f9f8;安清h&#xff1a;个人主页 &#x1f3a5;个人专栏&#xff1a;【Spring篇】【计算机网络】【Mybatis篇】 &#x1f3af;大题 ✨一.Nacos的服务注册与发现 &#x1f6a6;1.怎么来进行服务的注册与发现的这样的一个流程&#xff0c;描述一下。 &#x1f383;描述…

Android手机无网离线使用FunASR识别麦克风语音内容

手机断网离线使用FunASR识别麦克风语音内容 --本地AI电话机器人 上一篇&#xff1a;阿里FunASR本地断网离线识别模型简析 下一篇&#xff1a;手机无网离线使用FunASR识别手机历史通话录音 一、前言 继上一篇《阿里FunASR本地断网离线识别模型简析》和前面几篇ASR相关理论的…

Stable Diffusion 项目实战落地:从0到1 掌握ControlNet 第五篇 线稿到高清修复:一步步教你用AI做出完美IP形象

大家好!上一篇,我们一起玩转了字体风格变换 ,让文字根据提示词进行自如变换,个性十足又充满创意! 如果你错过了那篇文章,别担心,赶紧点这里补课:Stable Diffusion 项目实战落地:从0到1 掌握ControlNet 第四篇 风格化字体大揭秘:从线稿到涂鸦,ControlNet让文字焕发新生…

Java网络编程:TCP/UDP套接字通信详解

TCP客户端套接字创建与使用 Socket类基础概念 Socket类的对象代表TCP客户端套接字&#xff0c;用于与TCP服务器套接字进行通信。与服务器端通过accept()方法获取Socket对象不同&#xff0c;客户端需要主动执行三个关键步骤&#xff1a;创建套接字、绑定地址和建立连接。 客户端…

VMware vSphere 9与ESXi 9正式发布:云原生与AI驱动的虚拟化平台革新

2025年6月18日&#xff0c;VMware正式推出其旗舰虚拟化平台vSphere 9及配套的ESXi 9操作系统&#xff0c;标志着企业级虚拟化技术迈入以云原生、人工智能&#xff08;AI&#xff09;和硬件加速为核心的新纪元。此次更新不仅在功能层面实现突破&#xff0c;更通过授权模式革新为…

汽车功能安全概念阶段开发【相关项定义HARA】2

文章目录 1 浅谈概念阶段开发2 功能安全概念阶段开发2.1 相关项定义2.2 危害分析与风险评估&#xff08;HARA-Hazard Analysis and Risk Assessment&#xff09; 3 关键输出与对后续阶段的影响4 总结 1 浅谈概念阶段开发 概念阶段开发是整个研发流程的起点和基石。它发生在任何…

WPF中依赖属性和附加属性

依赖属性&#xff08;DependencyProperty&#xff09; 依赖属性是WPF中的一种特殊属性&#xff0c;它的实现依赖于DependencyObject类提供的基础设施。与普通的.NET属性不同&#xff0c;依赖属性的值可以通过多种方式确定&#xff0c;包括继承、样式、数据绑定和动画等。 主要特…

Docker 中如何实现镜像的推送和拉取

在 Docker 中&#xff0c;镜像的推送&#xff08;push&#xff09;和拉取&#xff08;pull&#xff09;是通过与**Docker 镜像仓库&#xff08;Registry&#xff09;**交互完成的。默认仓库是 Docker Hub&#xff0c;但你也可以使用私有仓库&#xff08;Harbor、Nexus、AWS ECR…