文章目录

  • 【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/diannao/90061.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/90061.shtml
英文地址,请注明出处:http://en.pswp.cn/diannao/90061.shtml

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

相关文章

Web爬虫编程语言选择指南

刚学爬虫的小伙伴常常为选择那种语言来写爬虫而烦恼&#xff0c;今天我将总结几种语言的优劣势&#xff0c;然后选择适合编写 Web爬虫 的编程语言。这就需要我们考虑开发效率、生态库支持、并发性能等因素。以下是主流选择及特点跟着一起看看吧&#xff1a; 1. Python&#xff…

学习日志06 python

加油&#xff0c;今天的任务是学习面向对象编程&#xff0c;设计一个简单的宠物管理系统&#xff08;宠物类、猫 / 狗子类&#xff09;&#xff0c;先做5道题目开启学习状态吧&#xff01;1 setdefault()在 Python 中&#xff0c;setdefault() 是字典&#xff08;dict&#xff…

基于Java+springboot 的车险理赔信息管理系统

源码、数据库、包调试源码编号&#xff1a;S595源码名称&#xff1a;基于springboot 的车险理赔信息管理系统用户类型&#xff1a;多角色&#xff0c;用户、事故调查员、管理员数据库表数量&#xff1a;14 张表主要技术&#xff1a;Java、Vue、ElementUl 、SpringBoot、Maven运…

MyDockFinder 绿色便携版 | 一键仿Mac桌面,非常简单

如果你既不想升级到Win11&#xff0c;又想体验Mac桌面的高级感&#xff0c;那么MyDockFinder将是你的最佳选择。这是一款专为Windows系统设计的桌面美化工具&#xff0c;能够将你的桌面转变成MacOS的风格。它提供了类似Dock栏和Finder的功能&#xff0c;让你在不更换操作系统的…

Babylon.js 材质克隆与纹理共享:你可能遇到的问题及解决方案

在 Babylon.js 中&#xff0c;材质&#xff08;Material&#xff09;和纹理&#xff08;Texture&#xff09;的克隆行为可能会影响渲染性能和内存管理&#xff0c;尤其是在多个材质共享同一纹理的情况下。本文将探讨&#xff1a;PBRMetallicRoughnessMaterial 的克隆机制&#…

信息素养复赛模拟1和模拟2的编程题标程

信息素养复赛模拟 11&#xff1a;楼层编号 #include<bits/stdc.h> using namespace std; int main(){int n, t;cin >> n >> t;int res 0;for(int i 1; i < n; i ){int x i;bool ok true;while(x){if(x % 10 t){ok false;}x / 10;}res ok;} cout &l…

Hadoop高可用集群搭建

Hadoop高可用(HA)集群是企业级大数据平台的核心基础设施&#xff0c;通过多主节点冗余和自动故障转移机制&#xff0c;确保系统在单点故障时仍能正常运行。本文将详细介绍如何基于CentOS 7搭建Hadoop 3.X高可用集群&#xff0c;涵盖环境准备、组件配置、集群启动及管理的全流程…

Next.js 实战笔记 1.0:架构重构与 App Router 核心机制详解

Next.js 实战笔记 1.0&#xff1a;架构重构与 App Router 核心机制详解 上一次写 Next 相关的东西都是 3 年前的事情了&#xff0c;这 3 年里 Next 也经历了 2-3 次的大版本变化。当时写的时候 Next 是 12 还是 13 的&#xff0c;现在已经是 15 了&#xff0c;从 build 到实现…

Pillow 安装使用教程

一、Pillow 简介 Pillow 是 Python 图像处理库 PIL&#xff08;Python Imaging Library&#xff09;的友好分支&#xff0c;是图像处理的事实标准。它支持打开、编辑、转换、保存多种图像格式&#xff0c;常用于图像批量处理、验证码识别、缩略图生成等应用场景。 二、安装 Pi…

SQL Server从入门到项目实践(超值版)读书笔记 20

9.4 数据的嵌套查询所谓嵌套查询&#xff0c;就是在一个查询语句中&#xff0c;嵌套进另一个查询语句&#xff0c;即&#xff0c;查询语句中可以使用另一个查询语句中得到的查询结果&#xff0c;子查询可以基于一张表或者多张表。子查询中常用的操作符有ANY、SOME、ALL、IN、EX…

【MySQL\Oracle\PostgreSQL】迁移到openGauss数据出现的问题解决方案

【MySQL\Oracle\PostgreSQL】迁移到openGauss数据出现的问题解决方案 问题1&#xff1a;序列值不自动刷新问题 下面SQL只针对单库操作以及每个序列只绑定一张表的情况 -- 自动生成的序列&#xff0c;设置序列值 with sequences as (select *from (select table_schema,table_…

【Maven】Maven命令大全手册:28个核心指令使用场景

Maven命令大全手册&#xff1a;28个核心指令使用场景 Maven命令大全手册&#xff1a;28个核心指令深度解析一、构建生命周期核心命令1. mvn clean2. mvn compile3. mvn test4. mvn package5. mvn install6. mvn deploy二、依赖管理命令7. mvn dependency:tree8. mvn dependency…

大语言模型(LLM)按架构分类

大语言模型&#xff08;LLM&#xff09;按架构分类的深度解析 1. 仅编码器架构&#xff08;Encoder-Only&#xff09; 原理 双向注意力机制&#xff1a;通过Transformer编码器同时捕捉上下文所有位置的依赖关系# 伪代码示例&#xff1a;BERT的MLM任务 masked_input "Th…

MySQL(120)如何进行数据脱敏?

数据脱敏&#xff08;Data Masking&#xff09;是指通过某种方式对敏感数据进行变形&#xff0c;使其在使用过程中无法识别原始数据&#xff0c;从而保护数据隐私。数据脱敏通常应用在开发、测试和数据分析等场景中。下面我们详细介绍如何在Java应用程序中进行数据脱敏&#xf…

使用 Dockerfile 构建基于 .NET9 的跨平台基础镜像

官方基础镜像准备 微软官方 dotnet sdk 基础镜像&#xff1a; docker pull mcr.microsoft.com/dotnet/sdk:9.0拉取 ubuntu 镜像&#xff1a; docker pull ubuntu:24.04更多资源请参考&#xff1a; dotnet sdk images&#xff0c;https://mcr.microsoft.com/en-us/artifact/mar/…

C++ : 线程库

C : 线程库一、线程thread1.1 thread类1.1.1 thread对象构造函数1.1.2 thread类的成员函数1.1.3 线程函数的参数问题1.2 this_thread 命名空间域1.2.1 chrono二、mutex互斥量库2.1 mutex的四种类型2.1.1 mutex 互斥锁2.2.2 timed_mutex 时间锁2.2.3 recursive_muetx 递归锁2.2.…

idea的使用小技巧,个人向

idea的使用小技巧&#xff0c;个人向 一、前言二、过程1、显示内存的使用情况2、去掉xml文件中的黄色背景3、显示所有打开文件4、显示工具栏到菜单下面5、使用JDK8 一、前言 每次重装idea都需要重新设置一下&#xff0c;这里做个记录。 这些技巧只是个人感觉的好用 演示用的…

debian及衍生发行版apt包管理常见操作

好的&#xff0c;这是 Debian 及其衍生版&#xff08;如 Ubuntu&#xff09;使用的 apt 包管理器的常用命令速查表。 一点说明&#xff1a;apt 是新一代的命令行工具&#xff0c;整合了 apt-get 和 apt-cache 的常用功能&#xff0c;并提供了更友好的交互体验。本表主要使用现…

vue调用函数

好的&#xff0c;我们来讲解如何在 Vue 模板中调用函数。您提供的代码是一个非常棒的、很实用的例子。 在 Vue 模板中&#xff0c;你可以在两个主要地方调用函数&#xff1a; 文本插值中&#xff1a;像 {{ formatDate(date) }} 这样&#xff0c;函数的返回值会作为文本被渲染到…

前端常用构建工具介绍及对比

打包构建工具是现代软件开发中必不可少的,它们帮助开发者自动化构建、打包、部署等流程,提升开发效率。不过,不同时期构建工具略有差异。 每个构建工具都有其擅长的领域,我们需要知道其优势,才能在我们实际开发中选择合适的构建工具进行构建处理。 1. Gulp Gulp 是一个…