• SpringBoot2.0(mybatis-plus常见的增删改查和分页)


    一,mybatis-plus常见注解

    @TableName 用于定义表名
    @TableId 用于定义表的主键

    属性
    value 用于定义主键字段名
    type 用于定义主键类型(主键策略 IdType),具体策略如下:

    IdType.AUTO          主键自增,系统分配,不需要手动输入
    IdType.NONE          未设置主键
    IdType.INPUT         需要自己输入 主键值
    IdType.ASSIGN_ID     系统分配 ID,用于数值型数据(Long,对应 mysql 中 BIGINT 类型)
    IdType.ASSIGN_UUID   系统分配 UUID,用于字符串型数据(String,对应 mysql 中 varchar(32) 类型)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    统一配置主键策略
    配置全局默认主键类型,实体类就不用加 @TableId(value = "id", type = IdType.AUTO)

    mybatis-plus.global-config.db-config.id-type=auto
    
    • 1

    @TableField 用于定义表的非主键字段

    属性
    value 用于定义非主键字段名,用于别名匹配,假如java对象属性和数据库属性不一样
    exist 用于指明是否为数据表的字段, true 表示是,false 为不是,假如某个java属性在数据库没对应的字段则要标记为faslse
    fill 用于指定字段填充策略(FieldFill,用的不多)

     字段填充策略:一般用于填充 创建时间、修改时间等字段
         FieldFill.DEFAULT         默认不填充
         FieldFill.INSERT          插入时填充
         FieldFill.UPDATE          更新时填充
         FieldFill.INSERT_UPDATE   插入、更新时填充。
    
    • 1
    • 2
    • 3
    • 4
    • 5

    二,创建一个工具类和启动类

    util包 JsonData类
    DemoApplication启动类

    在这里插入图片描述
    启动类

    package com.demo;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    
    @SpringBootApplication
    @MapperScan("com.demo.mapper")  //
    
    public class DemoApplication {
    
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class,args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    工具类

    package com.demo.util;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor //会生成一个包含所有变量
    @NoArgsConstructor //生成一个无参数的构造方法
    public class JsonData {
    
        /**
         * 状态码 0 表示成功,1表示处理中,-1表示失败
         */
        private Integer code;
        /**
         * 数据
         */
        private Object data;
        /**
         * 描述
         */
        private String msg;
    
        // 成功,传入数据
        public static JsonData buildSuccess() {
            return new JsonData(0, null, null);
        }
    
        // 成功,传入数据
        public static JsonData buildSuccess(Object data) {
            return new JsonData(0, data, null);
        }
    
        // 失败,传入描述信息
        public static JsonData buildError(String msg) {
            return new JsonData(-1, null, msg);
        }
    
        // 失败,传入描述信息,状态码
        public static JsonData buildError(String msg, Integer code) {
            return new JsonData(code, null, msg);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    三,创建实体类

    Bean包 Lapop类

    package com.demo.bean;
    
    import lombok.Data;
    
    @Data
    public class Lapop {
    
        /** 键盘id */
        private Integer id ;
        /** 键盘名称 */
        private String name ;
        /** 键盘尺寸 */
        private String size ;
        /** 键盘重量 */
        private String weight ;
        /** 电压 */
        private String voltage ;
        /** 电流 */
        private String current ;
        /** 键盘接口 */
        private String interfacepass ;
        /** 按键个数 */
        private String number ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    四,创建mapper接口

    mapper包 LapopMapper接口

    package com.demo.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.demo.bean.Lapop;
    
    public interface LapopMapper extends BaseMapper<Lapop> {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    五,创建service接口和impl类

    service包 LapopService接口
    impl包 LapopServiceImpl类

    LapopService接口

    package com.demo.service;
    
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.demo.bean.Lapop;
    
    import java.util.List;
    import java.util.Map;
    
    public interface LapopService {
    
        // 查询全部
        List<Lapop> getLapop();
    
        // 根据id查
        Lapop getByIdLapop(int id);
    
        //模糊查
        List<Lapop> getLapopBylist(Lapop lapop);
    
        // 新增
        int addLapop(Lapop lapop);
    
        // 修改
        int updateLapop(Lapop lapop);
    
        // 删除
        int deleteLapop(int id);
    
        // 分页
        IPage<Lapop> selectPageVO(Integer LapopIPage, Integer queryWrapper);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    LapopServiceImpl类

    package com.demo.service.impl;
    
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.demo.bean.Lapop;
    import com.demo.mapper.LapopMapper;
    import com.demo.service.LapopService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    import java.util.Map;
    
    @Service
    public class LapopServiceImpl implements LapopService {
    
        @Autowired
        private LapopMapper lapopMapper;
    
        @Override
        public List<Lapop> getLapop() {
            // 查询全部
            List<Lapop> getLapopList = lapopMapper.selectList(new QueryWrapper<Lapop>());
            return getLapopList;
        }
    
        @Override
        public Lapop getByIdLapop(int id) {
            // 根据id查
            return lapopMapper.selectById(id);
        }
    
        @Override
        public List<Lapop> getLapopBylist(Lapop lapop) {
            // 模糊查询
            QueryWrapper queryWrapper = new QueryWrapper<Lapop>();
            queryWrapper.like("name",lapop.getName());
            queryWrapper.gt("Number",lapop.getNumber());
            return lapopMapper.selectList(queryWrapper);
        }
    
    
        @Override
        public int addLapop(Lapop lapop) {
            // 新增
            return lapopMapper.insert(lapop);
        }
    
        @Override
        public int updateLapop(Lapop lapop) {
            // 修改
            return lapopMapper.updateById(lapop);
        }
    
        @Override
        public int deleteLapop(int id) {
            // 删除
            return lapopMapper.deleteById(id);
        }
    
        @Override
        public IPage<Lapop> selectPageVO(Integer LapopIPage, Integer queryWrapper) {
            // 分页
            QueryWrapper<Lapop> wrapper = new QueryWrapper<>();
            //第1页,每页2条
            Page<Lapop> page = new Page<>(LapopIPage, queryWrapper);
            IPage<Lapop> LapopbyIPage = lapopMapper.selectPage(page, wrapper);
            System.out.println("总条数"+LapopbyIPage.getTotal());
            System.out.println("总页数"+LapopbyIPage.getPages());
            //获取当前数据
            return LapopbyIPage;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    六,创建配置类

    config包 MybatisPlusPageConfig类
    配置分页插件

    package com.demo.config;
    
    import com.baomidou.mybatisplus.annotation.DbType;
    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;
    
    @Configuration
    public class MybatisPlusPageConfig {
        /**
         * 新的分页插件
         */
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
            return interceptor;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    七,创建controller

    controller包 LapopController类

    package com.demo.controller;
    
    
    import com.demo.bean.Lapop;
    import com.demo.service.LapopService;
    import com.demo.util.JsonData;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/lapopController")
    public class LapopController {
    
        @Autowired
        private LapopService lapopService;
    
        @RequestMapping("/LapopList")
        @ResponseBody
        public JsonData LapopList(){
            // 查询全部
            return JsonData.buildSuccess(lapopService.getLapop());
        }
    
        @RequestMapping("/LapopByIDDList")
        @ResponseBody
        public JsonData LapopByIDDList(int id){
            // 根据id查
            return JsonData.buildSuccess(lapopService.getByIdLapop(id));
        }
    
        @RequestMapping("/paLapopByList")
        @ResponseBody
        public JsonData paLapopByList(Lapop lapop){
            // 模糊查
            return JsonData.buildSuccess(lapopService.getLapopBylist(lapop));
        }
    
        @RequestMapping("/insertLapop")
        public Object insertLapop(@RequestBody Lapop lapop){
            // 新增
            int restue = lapopService.addLapop(lapop);
            return JsonData.buildSuccess(restue);
        }
    
        @RequestMapping("/updateLapop")
        public Object updateLapop(@RequestBody Lapop lapop){
            // 修改
            int request = lapopService.updateLapop(lapop);
            return JsonData.buildSuccess(request);
        }
    
        @RequestMapping("/deleteLapop")
        public Object deleteLapop(int id){
            // 删除
            return JsonData.buildSuccess(lapopService.deleteLapop(id));
        }
    
        @RequestMapping("/PageLapop")
        public Object PageLapop(Integer passerIPage, Integer queryWrapper ){
            // 分页
            return JsonData.buildSuccess(lapopService.selectPageVO(passerIPage,queryWrapper));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    八,使用测试工具测试增删改查和分页

    8.1,测试全部查询

    在这里插入图片描述
    在这里插入图片描述

    8.2,测试根据id查

    在这里插入图片描述
    在这里插入图片描述

    8.3,测试模糊查询

    在这里插入图片描述
    在这里插入图片描述

    8.4,测试新增

    在这里插入图片描述
    在这里插入图片描述

    8.5,测试修改

    在这里插入图片描述
    在这里插入图片描述

    8.6,测试删除

    在这里插入图片描述
    在这里插入图片描述

    8.7,测试分页

    在这里插入图片描述
    在这里插入图片描述

    九,QueryWrapper介绍

    QueryWrapper介绍
    可以封装sql对象,包括where条件,order by排序,select哪些字段等等
    查询包装类,可以封装多数查询条件,泛型指定返回的实体类

    List<BannerDO> list = bannerMapper.selectList(new QueryWrapper<BannerDO>());
    
    • 1

    核心API

    - eq 等于
    - ne 不等于
    - gt 大于
    - ge 大于等于
    - lt 小于
    - le 小于等于
    - or 拼接or
    - between 两个值中间
    - notBetween 不在两个值中间
    - like 模糊匹配
    - notLike 不像
    - likeLeft 左匹配
    - likeRight 右边匹配
    - isNull 字段为空
    - in in查询
    - groupBy 分组
    - orderByAsc 升序
    - orderByDesc 降序
    - having having查询
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    海康威视工业相机MAC地址
    Kernel Memory 入门系列:快速开始
    【软件测试】 1+X初级 功能测试试题
    React Native调用摄像头画面及拍照和保存图片到相册全流程
    SOC设计:关于reset的细节
    网络安全(黑客)自学
    短视频创作有什么建议吗?如何能更好地吸引用户呢?
    服务器迁移踩的坑
    力扣刷题 day47:10-17
    【Paddle】图像分类竞赛baseline——以智能硬件语音控制的时频图分类挑战赛为例
  • 原文地址:https://blog.csdn.net/H20031011/article/details/133015865