SpringBoot整合SSMP案例

146 阅读3分钟

日期:2022年2月24日

出处:

黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)

www.bilibili.com/video/BV15b…

正文


Mybatis-Plus条件查询

在users表中username有“h”的查询出来

    @Test
        //按条件查询
    void testGetBy1() {
        QueryWrapper<Users> qw = new QueryWrapper<>();
        qw.like("username","h");
        bookDao.selectList(qw);
    }

但是以上的代码是不完整的,有bug,有一种情况是当username = null时会有误操作

它会将“null”作为字段来查询

所以应该加以步判断,完整代码:

    @Test
        //按条件查询
    void testGetBy2() {
        String username = "o";
        LambdaQueryWrapper<Users> lqw = new LambdaQueryWrapper<>();
        lqw.like(username !=null,Users::getUsername,username);
        bookDao.selectList(lqw);
    }

查询结果实现分页操作——Mybatis-Plus拦截器

编写一个类来实现拦截操作

package com.ssmp.springboot_smmp.config;

/*MP的拦截器(固定格式)*/

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//相当于交给Spring管理了一个Bean
@Configuration
public class MpConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());//具体的拦截器
        return interceptor;
    }
}

在测试类中的代码:

    @Test
    //分页
    void testGetPage() {
        //展示第2页,每页5条数据
        IPage page = new Page(2,5);
        bookDao.selectPage(page,null);
    }

如何开启Mybatis-Plus运行日志

在.yml配置文件中添加如下配置

      # 开启MP运行日志
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

实体类快速开发——lombok

在pow.xml配置文件中的坐标

<!--        lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

作用:简化实体类代码的编写

package com.ssmp.springboot_smmp.domain;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

//lombok

//@Getter
//@Setter

@Data
//为当前实体类在编译期设置对应的get/set方法,toString方法,hashCode方法,equals方法等
public class Users {
    private Integer id;
    private String username;
    private String password;
}

整合Mybatis和整合Mybatis-Plus之间的区别

Mybatis是通过sql语句来锁定数据库中的表的

所以在实体类中的命名可以任意

而Mybatis-Plus是通过实体类的命名来锁定是数据库中的那个表的

名称不一致就会报错

修改和数据库表名一致之后测试成功

并且不区分大小写

统一数据类型

这个是我们写的表现层:

package com.ssmp.springboot_smmp.controller_biaoxian;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ssmp.springboot_smmp.domain.Users;
import com.ssmp.springboot_smmp.service_yewu.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private IBookService iBookService;

    @GetMapping
    public List<Users> getAll(){
        return iBookService.list();
    }
    @PostMapping
    public boolean save(@RequestBody Users users){
        return iBookService.save(users);
    }
    @PutMapping
    public boolean update(@RequestBody Users users){
        return iBookService.modify(users);
    }
    @DeleteMapping("{id}")
    public boolean delete(@PathVariable Integer id){
        return iBookService.delete(id);
    }
    @GetMapping("{id}")
    public Users getById(@PathVariable Integer id){
        return iBookService.getById(id);
    }
    @GetMapping("{currentPage}/{pageSize}")
    public IPage<Users> getPage(@PathVariable int currentPage,@PathVariable int pageSize){
        return iBookService.getPage(currentPage,pageSize);
    }
}

可以发现我们写的确实没毛病呢,但是对于前端的开发人员就不太友好了,因为我们的数据类型即多又杂,增加了前端开发的难度。

由于现在是前后端分离,所以我们在写好后端代码的时候同时也要考虑到怎么更加使前端开发的小姐姐们轻松一些。

这里的解决方案就是创建一个模型类,用于代替其他不同的各种数据类型

这里的flag表示是否传输成功

data为新创造的统一的数据类型

package com.ssmp.springboot_smmp.controller_biaoxian.utils;
//模型类用于发送给前端时统一数据格式
import lombok.Data;

//表现层消息类型一致性处理——前后端数据协议
@Data
public class R {
    private Boolean flag;
    private Object data;

    public R(){}

    public R(Boolean flag){
        this.flag = flag;
    }

    public R(Boolean flag,Object data){
        this.flag = flag;
        this.data = data;
    }
}

然后通过引用新的模型类新的代码:

package com.ssmp.springboot_smmp.controller_biaoxian;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ssmp.springboot_smmp.controller_biaoxian.utils.R;
import com.ssmp.springboot_smmp.domain.Users;
import com.ssmp.springboot_smmp.service_yewu.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/books")
public class iBookController {

    @Autowired
    private IBookService iBookService;

    @GetMapping
    public R getAll(){
        return new R(true,iBookService.list());
    }
    @PostMapping
    public R save(@RequestBody Users users){
        return new R(iBookService.save(users));
    }
    @PutMapping
    public R update(@RequestBody Users users){
        return new R(iBookService.modify(users));
    }
    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return new R(iBookService.delete(id));
    }
    @GetMapping("{id}")
    public R getById(@PathVariable Integer id){
        return new R(true,iBookService.getById(id));
    }
    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){
        IPage<Users> page = iBookService.getPage(currentPage, pageSize);
        return new R(true,page);
    }
}

发现问题及解决方案

解决方案如下图:

总结


在学习SpringBoot整合SSMP的过程中,学到了一些开发时通用的细节操作,比如lobok,@Data注解等等,最大的收获就是通多对比整合Mybatis和MP对他们之间的联系与区别有了很深的体会

做项目的基础开发步骤: