如何使用MybatisPlus快速進(jìn)行增刪改查詳解
前言:
mybatisplus 可以說(shuō)是對(duì)mybatis更好的拓展,一些簡(jiǎn)單的增刪改查的操作已經(jīng)被作者實(shí)現(xiàn),我們只需引用即可。
1.數(shù)據(jù)庫(kù)建表
這里使用的是MySQL數(shù)據(jù)庫(kù),表名為student


2.新建一個(gè)springboot項(xiàng)目
這里使用的idea
(1)、引入相應(yīng)的jar包



修改一下springboot的版本 最好與此一致,其他版本不確定是否兼容

這里如有需要復(fù)制時(shí),注意空白格,直接復(fù)制可能會(huì)報(bào)錯(cuò)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>(2)、快速啟動(dòng)項(xiàng)目
在此之前,先看看一看我的項(xiàng)目結(jié)構(gòu)
![[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳(img-vdJ6cRtB-1649861709999)(C:\Users\jsj rj201zdp\AppData\Roaming\Typora\typora-user-images\1649861657658.png)]](http://img.jbzj.com/file_images/article/202208/2022080511380261.png)
新建一個(gè)controller包,在controller包下新建一個(gè)HelloController.java
package com.zhu.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}springboot啟動(dòng)類(lèi),運(yùn)行main即可
package com.zhu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MybatisplusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusDemoApplication.class, args);
}
}在瀏覽器(這里使用的谷歌瀏覽器)中輸入地址: http://localhost:8080/test/hello
至此,一個(gè)springboot項(xiàng)目快速啟動(dòng)完成,下面我們需要引入mybatisplus相關(guān)依賴
3.springboot結(jié)合mybatisplus
(1)、引入mybatisplus以及其他依賴
<!--mybatisplus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--數(shù)據(jù)庫(kù)連接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
(2)、創(chuàng)建application.yml文件,修改配置
# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/此處為你的數(shù)據(jù)庫(kù)名?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: 你的數(shù)據(jù)庫(kù)用戶名
password: 你的數(shù)據(jù)庫(kù)密碼
mybatis-plus:
# xml文件掃描
mapper-locations: classpath*:/mapper/**Mapper.xml(3)、創(chuàng)建mybaisplus配置類(lèi)
package com.zhu.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@MapperScan("com.zhu.mapper")//mapper接口掃描注解
@EnableTransactionManagement
public class MyBatisPlusConfig {//分頁(yè)配置,本博客不展示分頁(yè)操作
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}(4)、創(chuàng)建實(shí)體類(lèi)
package com.zhu.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author xiaozhu
* @since 2022-04-13
*/
//使用lombok,簡(jiǎn)化了代碼,不用書(shū)寫(xiě)set get等方法
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 自動(dòng)遞增
*/
@TableId(value = "sno", type = IdType.AUTO)
private Integer sno;
private String sname;
private String sex;
private Integer age;
private Integer clas;
}(5)、創(chuàng)建mapper接口
package com.zhu.mapper;
import com.zhu.entity.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author xiaozhu
* @since 2022-04-13
*/
public interface StudentMapper extends BaseMapper<Student> {
}(6)、創(chuàng)建service接口及其實(shí)現(xiàn)類(lèi)
package com.zhu.service;
import com.zhu.entity.Student;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 服務(wù)類(lèi)
* </p>
*
* @author xiaozhu
* @since 2022-04-13
*/
public interface StudentService extends IService<Student> {
}package com.zhu.service.impl;
import com.zhu.entity.Student;
import com.zhu.mapper.StudentMapper;
import com.zhu.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 服務(wù)實(shí)現(xiàn)類(lèi)
* </p>
*
* @author xiaozhu
* @since 2022-04-13
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
}(7)、創(chuàng)建controller
package com.zhu.controller;
import com.zhu.entity.Student;
import com.zhu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author xiaozhu
* @since 2022-04-13
*/
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
//返回所有學(xué)生
@GetMapping("/allStudent")
public List<Student> findAllStudent(){
return studentService.list();
}
//根據(jù)學(xué)號(hào)查詢學(xué)生
@GetMapping("/findBySno/{sno}")
public Student findBySno(@PathVariable("sno") Integer sno){
return studentService.getById(sno);
}
//根據(jù)學(xué)號(hào)刪除學(xué)生信息(此方法可以使用軟件postman進(jìn)行測(cè)試)
@DeleteMapping("/deleteBySno/{sno}")
public boolean deleteBySno(@PathVariable("sno") Integer sno){
return studentService.removeById(sno);
}
//增加一個(gè)學(xué)生信息(此方法可以使用軟件postman進(jìn)行測(cè)試),注意學(xué)號(hào)自增
@PostMapping("/add")
public boolean add(@RequestBody Student student){
return studentService.save(student);
}
//根據(jù)學(xué)號(hào)修改學(xué)生信息(此方法可以使用軟件postman進(jìn)行測(cè)試),注意學(xué)號(hào)自增
@PutMapping("/update")
public boolean update(@RequestBody Student student){
return studentService.updateById(student);
}
}至此,我們完成了mybatisplus的快速的入門(mén)級(jí)別的增刪改查
總結(jié)
到此這篇關(guān)于如何使用MybatisPlus快速進(jìn)行增刪改查的文章就介紹到這了,更多相關(guān)MybatisPlus增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis Plus 增刪改查的實(shí)現(xiàn)(小白教程)
- MybatisPlus實(shí)現(xiàn)簡(jiǎn)單增刪改查功能
- 搭建MyBatis-Plus框架并進(jìn)行數(shù)據(jù)庫(kù)增刪改查功能
- Mybatis Plus使用條件構(gòu)造器增刪改查功能的實(shí)現(xiàn)方法
- SpringBoot整合MybatisPlus實(shí)現(xiàn)增刪改查功能
- MybatisPlus?BaseMapper?實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)增刪改查源碼
- MyBatis-Plus里面的增刪改查詳解(化繁為簡(jiǎn))
相關(guān)文章
詳解Spring AOP 實(shí)現(xiàn)“切面式”valid校驗(yàn)
本篇文章主要介紹了詳解Spring AOP 實(shí)現(xiàn)“切面式”valid校驗(yàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Java中Set與List的關(guān)系與區(qū)別介紹
這篇文章主要介紹了Java中Set與List的關(guān)系與區(qū)別介紹,本文總結(jié)它們兩個(gè)接口都是繼承自Collection、它們之間的存儲(chǔ)方式不一樣,需要的朋友可以參考下2015-03-03
Spring Validation和Hibernate Validator結(jié)合國(guó)際化代碼實(shí)例
這篇文章主要介紹了Spring Validation和Hibernate Validator結(jié)合國(guó)際化代碼實(shí)例,我們需要對(duì)請(qǐng)求參數(shù)進(jìn)行非空、長(zhǎng)度、正確性進(jìn)行校驗(yàn), 本文主要講解Spring Validation 和 Hibernate Validator, 同時(shí)整合i18n(國(guó)際化)實(shí)現(xiàn)參數(shù)校驗(yàn)自動(dòng),需要的朋友可以參考下2023-10-10
Java中如何使用Redis GEO測(cè)算經(jīng)緯度距離
GEO也是Redis中提供的一種數(shù)據(jù)類(lèi)型,可以將其理解成ZSet有序集合類(lèi)型,本文給大家介紹Java-使用Redis GEO測(cè)算經(jīng)緯度距離,感興趣的朋友一起看看吧2023-11-11
Spring切面優(yōu)先級(jí)與基于xml的AOP實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Spring切面的優(yōu)先級(jí)與基于xml的AOP的詳細(xì)步驟,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(54)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-08-08

