MyBatisPlus-01-環境初始化及簡單應用

文章目錄

  • 【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 是一個…