Table of contents
- 1. Mybatis-plus common annotations
- Second, create a tool class and startup class
- 3. Create entity class
- 4. Create mapper interface
- 5. Create service interface and impl class
- 6. Create configuration class
- 7. Create controller
- 8. Use testing tools to test additions, deletions, modifications and paging.
- 9. Introduction to QueryWrapper
1. Mybatis-plus common annotations
@TableName 用于定义表名
@TableId 用于定义表的主键
Attributes
value 用于定义主键字段名
type 用于定义主键类型(主键策略 IdType),具体策略如下:
IdType.AUTO 主键自增,系统分配,不需要手动输入
IdType.NONE 未设置主键
IdType.INPUT 需要自己输入 主键值
IdType.ASSIGN_ID 系统分配 ID,用于数值型数据(Long,对应 mysql 中 BIGINT 类型)
IdType.ASSIGN_UUID 系统分配 UUID,用于字符串型数据(String,对应 mysql 中 varchar(32) 类型)
Unified configuration of primary key strategies
配置全局默认主键类型,实体类就不用加 @TableId(value = "id", type = IdType.AUTO)
mybatis-plus.global-config.db-config.id-type=auto
@TableField 用于定义表的非主键字段
Attributes
value 用于定义非主键字段名,用于别名匹配,假如java对象属性和数据库属性不一样
exist 用于指明是否为数据表的字段, true 表示是,false 为不是,假如某个java属性在数据库没对应的字段则要标记为faslse
fill 用于指定字段填充策略(FieldFill,用的不多)
字段填充策略:一般用于填充 创建时间、修改时间等字段
FieldFill.DEFAULT 默认不填充
FieldFill.INSERT 插入时填充
FieldFill.UPDATE 更新时填充
FieldFill.INSERT_UPDATE 插入、更新时填充。
Second, create a tool class and startup class
util包
JsonData类
DemoApplication启动类
Startup class
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);
}
}
Tools
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);
}
}
3. Create entity class
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 ;
}
4. Create mapper interface
mapper包
LapopMapper接口
package com.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.bean.Lapop;
public interface LapopMapper extends BaseMapper<Lapop> {
}
5. Create service interface and impl class
service包
LapopService接口
impl包
LapopServiceImpl类
LapopService interface
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);
}
LapopServiceImpl class
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;
}
}
6. Create configuration class
config包
MybatisPlusPageConfig类
Configure paging plugin
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;
}
}
7. Create 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));
}
}
8. Use testing tools to test additions, deletions, modifications and paging.
8.1, test all queries
8.2, test based on ID
8.3. Test fuzzy query
8.4, new test
8.5, test modifications
8.6, test deletion
8.7, test paging
9. Introduction to QueryWrapper
Introduction to QueryWrapper
可以封装sql对象,包括where条件,order by排序,select哪些字段等等
查询包装类,可以封装多数查询条件,泛型指定返回的实体类
List<BannerDO> list = bannerMapper.selectList(new QueryWrapper<BannerDO>());
Core API
- eq 等于
- ne 不等于
- gt 大于
- ge 大于等于
- lt 小于
- le 小于等于
- or 拼接or
- between 两个值中间
- notBetween 不在两个值中间
- like 模糊匹配
- notLike 不像
- likeLeft 左匹配
- likeRight 右边匹配
- isNull 字段为空
- in in查询
- groupBy 分组
- orderByAsc 升序
- orderByDesc 降序
- having having查询