如何使用MybatisPlus快速進行增刪改查詳解
前言:
mybatisplus 可以說是對mybatis更好的拓展,一些簡單的增刪改查的操作已經被作者實現,我們隻需引用即可。
1.數據庫建表
這裡使用的是MySQL數據庫,表名為student
2.新建一個springboot項目
這裡使用的idea
(1)、引入相應的jar包
修改一下springboot的版本 最好與此一致,其他版本不確定是否兼容
這裡如有需要復制時,註意空白格,直接復制可能會報錯
<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)、快速啟動項目
在此之前,先看看一看我的項目結構
新建一個controller包,在controller包下新建一個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啟動類,運行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
至此,一個springboot項目快速啟動完成,下面我們需要引入mybatisplus相關依賴
3.springboot結合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> <!--數據庫連接--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
(2)、創建application.yml文件,修改配置
# DataSource Config spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/此處為你的數據庫名?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: 你的數據庫用戶名 password: 你的數據庫密碼 mybatis-plus: # xml文件掃描 mapper-locations: classpath*:/mapper/**Mapper.xml
(3)、創建mybaisplus配置類
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 {//分頁配置,本博客不展示分頁操作 @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); return paginationInterceptor; } }
(4)、創建實體類
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,簡化瞭代碼,不用書寫set get等方法 @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class Student implements Serializable { private static final long serialVersionUID = 1L; /** * 自動遞增 */ @TableId(value = "sno", type = IdType.AUTO) private Integer sno; private String sname; private String sex; private Integer age; private Integer clas; }
(5)、創建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)、創建service接口及其實現類
package com.zhu.service; import com.zhu.entity.Student; import com.baomidou.mybatisplus.extension.service.IService; /** * <p> * 服務類 * </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> * 服務實現類 * </p> * * @author xiaozhu * @since 2022-04-13 */ @Service public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService { }
(7)、創建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; //返回所有學生 @GetMapping("/allStudent") public List<Student> findAllStudent(){ return studentService.list(); } //根據學號查詢學生 @GetMapping("/findBySno/{sno}") public Student findBySno(@PathVariable("sno") Integer sno){ return studentService.getById(sno); } //根據學號刪除學生信息(此方法可以使用軟件postman進行測試) @DeleteMapping("/deleteBySno/{sno}") public boolean deleteBySno(@PathVariable("sno") Integer sno){ return studentService.removeById(sno); } //增加一個學生信息(此方法可以使用軟件postman進行測試),註意學號自增 @PostMapping("/add") public boolean add(@RequestBody Student student){ return studentService.save(student); } //根據學號修改學生信息(此方法可以使用軟件postman進行測試),註意學號自增 @PutMapping("/update") public boolean update(@RequestBody Student student){ return studentService.updateById(student); } }
至此,我們完成瞭mybatisplus的快速的入門級別的增刪改查
總結
到此這篇關於如何使用MybatisPlus快速進行增刪改查的文章就介紹到這瞭,更多相關MybatisPlus增刪改查內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- SpringBoot實現ORM操作MySQL的幾種方法
- 搭建MyBatis-Plus框架並進行數據庫增刪改查功能
- MyBatis-Plus使用ActiveRecord(AR)實現CRUD
- SpringBoot 整合mybatis+mybatis-plus的詳細步驟
- 一篇超詳細的SpringBoot整合MybatisPlus的文章