当前位置:   article > 正文

springboot_com.baomidou.mybatisplus.extension

com.baomidou.mybatisplus.extension

最近在学习springboot框架,利用springboot结合数据库实现基本的增删改查分页

首先是数据库的字段 id,name,type,description,id设置自增,因为是学习的基础的没有加version,deleted,create_time,update_time等字段

其次是导入依赖,因为在创建springboot工程的时候可以选择导入一些坐标,比如

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>mysql</groupId>
  7. <artifactId>mysql-connector-java</artifactId>
  8. <scope>runtime</scope>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.projectlombok</groupId>
  12. <artifactId>lombok</artifactId>
  13. <optional>true</optional>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-test</artifactId>
  18. <scope>test</scope>
  19. </dependency>

结合mybatisplus能使开发更加快速,所以导入其坐标

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.4.1</version>
  5. </dependency>

数据源druid坐标

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>druid-spring-boot-starter</artifactId>
  4. <version>1.2.3</version>
  5. </dependency>

在application.yml下还要加入配置

#关于连接数据库的

spring:
  datasource:
    druid:
      password: 1234
      username: root
      url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
      driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
  global-config:
    db-config:
      table-prefix: tb_   
      id-type: auto
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

 #设置端口为80

server:
  port: 80

 

1.实体类 entity     Book

  1. package com.lh.entity;
  2. import lombok.Data;
  3. @Data
  4. public class Book {
  5. private Integer id;
  6. private String type;
  7. private String name;
  8. private String description;
  9. }

2.dao层  BookDao接口

结合mybatisplus,BookDao继承BaseMapper<Book> 开发更加快速,BaseMapper接口中写了若干种方法 增删改查等等

  1. package com.lh.dao;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.lh.entity.Book;
  4. import org.apache.ibatis.annotations.Mapper;
  5. @Mapper
  6. public interface BookDao extends BaseMapper<Book> {
  7. }

2.1 测试dao层代码是否正确

使用@springBootTest   , @Test,    @Autowired 注解 

要测试BookDao   所以自动注入BookDao

  1. package com.lh.dao;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.lh.entity.Book;
  6. import org.junit.jupiter.api.Test;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. @SpringBootTest
  10. class testbookdao {
  11. @Autowired
  12. private BookDao bookDao;
  13. @Test
  14. void testupdate() {
  15. Book book=new Book();
  16. book.setId(45);
  17. book.setName("信503系统");
  18. book.setType("通3036信");
  19. book.setDescription("学习666基础");
  20. bookDao.updateById(book);
  21. }
  22. @Test
  23. void getall(){
  24. bookDao.selectList(null);
  25. }
  26. @Test //分页查询
  27. void testgetpage(){
  28. IPage page=new Page(2,3);
  29. bookDao.selectPage(page,null);
  30. System.out.println(page.getCurrent());
  31. System.out.println(page.getPages());
  32. System.out.println(page.getRecords());
  33. System.out.println(page.getSize());
  34. System.out.println(page.getTotal());
  35. }
  36. @Test//条件查询
  37. void testgetby(){
  38. String name="信";
  39. LambdaQueryWrapper<Book> lqw=new LambdaQueryWrapper<>();
  40. lqw.like(name!=null,Book::getName,name);
  41. bookDao.selectList(lqw);
  42. }
  43. }

2.1.1

这里直接测查询分页显示的还是全部数据

我们要加入一个拦截器

在config包下创建mabatisplusconfig类

  1. package com.lh.config;
  2. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  3. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration //加入到配置中
  7. public class MybaitisplusConfig {
  8. @Bean //创建bean 交给spring管理
  9. //直接使用分页 控制台得不出来数据 需要创建拦截器
  10. public MybatisPlusInterceptor mybatisPlusInterceptor(){
  11. MybatisPlusInterceptor mp=new MybatisPlusInterceptor();
  12. mp.addInnerInterceptor(new PaginationInnerInterceptor());
  13. return mp;
  14. }
  15. }

3.service层  

  3.1基础操作 BookService   编写各种方法  ,增删改查

 
  1. package com.lh.service;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.lh.entity.Book;
  4. import java.util.List;
  5. public interface BookService {
  6. boolean save(Book book);
  7. boolean delete(Integer id);
  8. boolean update(Book book);
  9. Book getById(Integer id);
  10. List<Book> getall();
  11. IPage<Book> getpage(int current, int size);
  12. }

下面是BookServiceimpl  实现BookService 

  1. package com.lh.service.impl;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.lh.dao.BookDao;
  5. import com.lh.entity.Book;
  6. import com.lh.service.BookService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.util.List;
  10. @Service
  11. public class BookServiceimpl implements BookService {
  12. @Autowired
  13. private BookDao bookDao;
  14. @Override
  15. public boolean save(Book book) {
  16. return bookDao.insert(book)>0;
  17. }
  18. @Override
  19. public boolean delete(Integer id) {
  20. return bookDao.deleteById(id)>0;
  21. }
  22. @Override
  23. public boolean update(Book book) {
  24. return bookDao.updateById(book)>0;
  25. }
  26. @Override
  27. public Book getById(Integer id) {
  28. return bookDao.selectById(id);
  29. }
  30. @Override
  31. public List<Book> getall() {
  32. return bookDao.selectList(null);
  33. }
  34. @Override
  35. public IPage<Book> getpage(int current, int size) {
  36. IPage<Book> page=new Page<>(current,size);
  37. return bookDao.selectPage(page,null);
  38. }
  39. }

3.2  利用mybatisplus快速开发  IBookService1 继承IService<Book>

接口下面可以继续写方法,但是尽量不要覆盖IService里面写的方法,避免加重负担

  1. package com.lh.service;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.baomidou.mybatisplus.extension.service.IService;
  4. import com.lh.entity.Book;
  5. public interface ibookservice1 extends IService<Book> {
  6. IPage<Book> getpage(Integer current,Integer size);
  7. }

下面是IBookServiceimpl1

这里我重写了一个查询分页的操作 

  1. package com.lh.service.impl;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.lh.dao.BookDao;
  6. import com.lh.entity.Book;
  7. import com.lh.service.ibookservice1;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. @Service//业务层快速开发
  11. public class ibooserviceimpl1 extends ServiceImpl<BookDao, Book> implements ibookservice1 {
  12. @Autowired
  13. private BookDao bookDao;
  14. //可以重载或增加 不要覆盖原始操作
  15. @Override
  16. public IPage<Book> getpage(Integer current, Integer size) {
  17. IPage page=new Page<>(current,size);
  18. bookDao.selectPage(page,null);
  19. return page;
  20. }
  21. }

3.3测试service层

  3.3.1 测试BookService  创建测试类testbookservice

  1. package com.lh.service;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.lh.entity.Book;
  4. import org.junit.jupiter.api.Test;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. @SpringBootTest
  8. public class testbookservice {
  9. @Autowired
  10. private BookService bookService;
  11. @Test
  12. void testsave(){
  13. Book book=new Book();
  14. book.setName("电子技术");
  15. book.setType("通信");
  16. book.setDescription("学习666电子技术基础");
  17. bookService.save(book);
  18. }
  19. @Test
  20. void testdelete(){
  21. bookService.delete(46);
  22. }
  23. @Test
  24. void testupdate(){
  25. Book book=new Book();
  26. book.setId(45);
  27. book.setName("模拟电子技术");
  28. book.setType("通信");
  29. book.setDescription("学习555电子技术基础");
  30. bookService.update(book);
  31. }
  32. @Test
  33. void testgetbyid(){
  34. System.out.println(bookService.getById(45));
  35. }
  36. @Test
  37. void testgetpage(){
  38. IPage<Book> page = bookService.getpage(1, 3);
  39. System.out.println(page.getTotal());
  40. System.out.println(page.getSize());
  41. System.out.println(page.getCurrent());
  42. System.out.println(page.getPages());
  43. }
  44. }

3.3.2 测试ibookservice  

  1. package com.lh.service;
  2. import com.lh.entity.Book;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. @SpringBootTest
  7. public class testibooservice1 {
  8. @Autowired
  9. private ibookservice1 ibookservice1;
  10. @Test
  11. void testinsert(){
  12. Book book=new Book();
  13. book.setName("单片adadaadadad机原理");
  14. book.setType("通信");
  15. book.setDescription("学习777rggrr电子技术基础");
  16. ibookservice1.save(book);
  17. }
  18. @Test
  19. void testdelete(){
  20. ibookservice1.removeById(49);
  21. }
  22. @Test
  23. void testupdate(){
  24. Book book=new Book();
  25. book.setId(50);
  26. book.setName("大学物理");
  27. book.setType("通信");
  28. book.setDescription("学习77技术基础");
  29. ibookservice1.updateById(book);
  30. }
  31. }

4.controller层   在页面上展示数据

  4.1因为要让前端减少工作量,我们不能返回任意一数据,所以我们可以与前端约定返回的数据格式

所以可以创建一个类R,设置返回的数据格式 

  1. package com.lh.controller.untils;
  2. import lombok.Data;
  3. /***
  4. * 前后端数据协议
  5. */
  6. @Data
  7. public class R {
  8. private boolean flag;
  9. private Object date;
  10. public R(){}
  11. public R(boolean flag){
  12. this.flag=flag;
  13. }
  14. public R(boolean flag,Object date){
  15. this.flag=flag;
  16. this.date=date;
  17. }
  18. }

 4.2创建BookController  结合restful风格开发

  1. package com.lh.controller;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.lh.controller.untils.R;
  4. import com.lh.entity.Book;
  5. import com.lh.service.ibookservice1;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.*;
  8. /***
  9. * 加入前后端数据协议
  10. */
  11. @RestController
  12. @RequestMapping("/books")
  13. public class BookController {
  14.     @Autowired
  15.     private ibookservice1 ibookservice1;
  16.     @GetMapping
  17.     public R selectAll(){
  18.         return new R(true,ibookservice1.list());
  19.     }
  20.     @GetMapping("/{id}")
  21.    public R selectById(@PathVariable Integer id){
  22.         return new R(true,ibookservice1.getById(id));
  23.     }
  24.     @DeleteMapping("/{id}")
  25.     public  R delete(@PathVariable Integer id){
  26.         return new R(ibookservice1.removeById(id));
  27.     }
  28.     @PutMapping
  29.     public R update(@RequestBody Book book){
  30.         return new R(ibookservice1.updateById(book));
  31.     }
  32.     @PostMapping ()
  33.    public R save(@RequestBody Book book){
  34.         return new R(ibookservice1.save(book));
  35.     }
  36.     @GetMapping("/{current}/{size}")
  37.     public R getpage(@PathVariable  Integer current,@PathVariable Integer size){
  38.         return new R(true,ibookservice1.getpage(current, size));
  39.     }
  40. }

4.3  启动引导类 springbootssmapplication利用postman测试BookController

  1. package com.lh;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class SpringbootssmApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(SpringbootssmApplication.class, args);
  8. }
  9. }

4.3.1测试查全部

 4.3.2测试查分页

 

 4.3.3测试新增 

 

4.3.4 测试更新

首先我们先随便插入一个数据

 

 然后在进行更改

 

 

 

 

更改成功!!!

springboot,mybatisplus和注解开发真的很强,增强了开发效率!!!

欢迎大佬教我学习springboot!!!

 

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号