快速上手Mybatis-plus結(jié)構(gòu)構(gòu)建過程
前言
如何快速上手使用一次Mybatis-plus?
話不多說 直接開始
配置實體類(和自己數(shù)據(jù)庫表對應)
package com.example.jsr303.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
/**
* 學習測試JSR303
*/
@Data
@TableName("test")
public class TestEntity implements Serializable {
//忽略掉 設(shè)置序列化和反序列化的serialVersionUID
//想了解一下可以去這篇博客 https://blog.csdn.net/qq_45503106/article/details/107950914
private static final long serialVersionUID = 1L;
/**
* test表主鍵ID
*/
@TableId
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 郵箱
*/
private String email;
}
一定記得使用@TableName 綁定你這個數(shù)據(jù)庫的表
否則按照 數(shù)據(jù)庫名+實體類 名去找你的表
一般都找不到

一、導入依賴
<dependencies>
<!-- web mvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql鏈接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- lombok簡化插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- test測試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- myabtis-plus依賴-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
二、配置配置文件yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/jsr303?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
server:
port: 8082
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

三 配置Mapper Service Controller三層架構(gòu)
Mapper層
package com.example.jsr303.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.jsr303.entity.TestEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TestDao extends BaseMapper<TestEntity> {
}

Service層
package com.example.jsr303.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.jsr303.entity.TestEntity;
import org.springframework.stereotype.Service;
public interface TestService extends IService<TestEntity> {
}

ServiceImpl層
package com.example.jsr303.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.jsr303.dao.TestDao;
import com.example.jsr303.entity.TestEntity;
import com.example.jsr303.service.TestService;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl extends ServiceImpl<TestDao, TestEntity> implements TestService {
}

Controller層
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
TestService testService;
@RequestMapping("/list")
public List<TestEntity> selectAll(){
return testService.list();
}
}

常用的Service接口官方文檔
總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決RestTemplate 請求url中包含百分號 會被轉(zhuǎn)義成25的問題
這篇文章主要介紹了解決RestTemplate 請求url中包含百分號 會被轉(zhuǎn)義成25的問題,具有很好的參考價值,希望對大家有所幫助。2021-10-10
JavaWeb之Servlet注冊頁面的實現(xiàn)示例
注冊頁面是很多網(wǎng)站都會是使用的到,本文主要介紹了JavaWeb之Servlet注冊頁面的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
Spring Boot中快速操作Mongodb數(shù)據(jù)庫指南
這篇文章主要給大家介紹了關(guān)于Spring Boot中如何快速操作Mongodb的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05
Java中將異步調(diào)用轉(zhuǎn)為同步的五種實現(xiàn)方法
本文介紹了將異步調(diào)用轉(zhuǎn)為同步阻塞模式的五種方法:wait/notify、ReentrantLock+Condition、Future、CountDownLatch和CyclicBarrier,每種方法都有其適用場景和核心機制,可以根據(jù)具體需求選擇合適的方法,需要的朋友可以參考下2025-02-02
SpringBoot如何配置文件properties和yml
這篇文章主要介紹了SpringBoot如何配置文件properties和yml問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
IntelliJ?IDEA?2022.2.3最新激活圖文教程(親測有用永久激活)
今天給大家分享一個?IDEA?2022.2.3?的激活破解教程,全文通過文字+圖片的方式講解,手把手教你如何激活破解?IDEA,?只需要幾分鐘即可搞定,對idea2022.2.3激活碼感興趣的朋友跟隨小編一起看看吧2022-11-11
Netty分布式NioSocketChannel注冊到selector方法解析
這篇文章主要為大家介紹了Netty分布式源碼分析NioSocketChannel注冊到selector方法的解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03

