1. 高级:插件机制
2.1 自动填充
-
步骤:
- 步骤1:编写处理类,接口 MetaObjectHandler
- 插入填充:insertFill , setxxxValByName(“字段名”, 值, 对象)
- 更新添加:updateFill
- 步骤2:修改JavaBean,确定那些字段需要添加 @TableField(fill = insert/update/insert_update)
- 步骤1:编写处理类,接口 MetaObjectHandler
-
步骤1:编写处理类,接口 MetaObjectHandler
- 插入填充:insertFill , setxxxValByName(“字段名”, 值, 对象)
- 更新添加:updateFill
-
步骤2:修改JavaBean,确定那些字段需要添加 @TableField(fill = insert/update/insert_update)
3 .重写:使用MyBatisPlus重写学生服务
3.1 环境
-
项目名:nacos-ssm-student-service-student-plus-bg12
-
坐标
3.2 修改JavaBean
-
添加坐标
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-annotation</artifactId> <version>3.4.0</version> </dependency>
-
修改JavaBean的注解
package com.czxy.domain; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.util.ArrayList; import java.util.Date; import java.util.List; @Data @TableName("tb_student") public class StudentPlus { @TableId("s_id") private Integer sid; //学生ID @TableField("sname") private String sname; //姓名 @TableField("age") private Integer age; //年龄 @TableField("birthday") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") private Date birthday; //生日 @TableField("gender") private String gender; //性别 @TableField("c_id") private Integer cid; //班级外键 @TableField("city_ids") private String cityIds; //城市 @TableField(exist = false) private List<City> cityList; //所选城市 @TableField(exist = false) private Classes classes; //班级对象 @TableField(exist = false) private Integer courseCount; //选课数 @TableField(exist = false) private List<Course> courseList = new ArrayList<>(); //选课详情 @TableField(exist = false) private List<Integer> courseIds = new ArrayList(); //选课id }
3.3 基本结构
-
步骤:
- 步骤1:配置类,配置分页
- 步骤2:编写vo封装查询条件
- 步骤3:mapper
- 步骤4:service
- 步骤5:controller
-
步骤1:配置类,配置分页
package com.czxy.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisPlusConfig { /** * 配置插件 * @return */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); // 分页插件 mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return mybatisPlusInterceptor; } }
-
步骤2:编写vo封装查询条件
package com.czxy.vo; import lombok.Data; @Data public class StudentPlusVo { private String cid; //班级id private String sname; //学生姓名 private String startAge; //开始年龄 private String endAge; //结束年龄 }
-
步骤3:mapper
package com.czxy.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.czxy.domain.StudentPlus; import org.apache.ibatis.annotations.Mapper; @Mapper public interface StudentPlusMapper extends BaseMapper<StudentPlus> { }
-
步骤4:service
-
接口
package com.czxy.service; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; import com.czxy.domain.StudentPlus; import com.czxy.vo.StudentPlusVo; public interface StudentPlusService extends IService<StudentPlus> { /** * 查询+条件+分页 * @param size * @param current * @param studentPlusVo * @return */ Page<StudentPlus> condition(Integer size, Integer current, StudentPlusVo studentPlusVo); }
-
-
实现类
package com.czxy.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.czxy.domain.Classes; import com.czxy.domain.Student; import com.czxy.domain.StudentPlus; import com.czxy.mapper.StudentPlusMapper; import com.czxy.service.StudentPlusService; import com.czxy.vo.StudentPlusVo; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @Service @Transactional public class StudentPlusServiceImpl extends ServiceImpl<StudentPlusMapper, StudentPlus> implements StudentPlusService { @Override public Page<StudentPlus> condition(Integer size, Integer current, StudentPlusVo studentPlusVo) { //1 条件 QueryWrapper<StudentPlus> queryWrapper = new QueryWrapper<>(); queryWrapper.eq(StringUtils.isNotBlank(studentPlusVo.getCid()), "c_id", studentPlusVo.getCid()); queryWrapper.like(StringUtils.isNotBlank(studentPlusVo.getSname()), "sname", studentPlusVo.getSname()); queryWrapper.ge(StringUtils.isNotBlank(studentPlusVo.getStartAge()), "age", studentPlusVo.getStartAge()); queryWrapper.le(StringUtils.isNotBlank(studentPlusVo.getEndAge()),"age", studentPlusVo.getEndAge()); //2 分页 Page<StudentPlus> page = new Page<>(current, size); //3 查询 this.baseMapper.selectPage(page,queryWrapper); //4 关联 page.getRecords().forEach(student -> { // Classes classes = classesMapper.selectByPrimaryKey(student.getCid()); // student.setClasses(classes); }); //5 封装 return page; } }
-
步骤5:controller
package com.czxy.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.czxy.domain.Student; import com.czxy.domain.StudentPlus; import com.czxy.service.StudentPlusService; import com.czxy.vo.BaseResult; import com.czxy.vo.StudentPlusVo; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @RestController @RequestMapping("/studentPlus") public class StudentPlusController { @Resource private StudentPlusService studentPlusService; /** * 访问路径 /studentPlus/condition/3/1 * @param size 每页显示个数 * @param current 当前页 * @param studentPlusVo 条件查询 * @return */ @PostMapping("/condition/{size}/{current}") public BaseResult condition(@PathVariable("size") Integer size, @PathVariable("current") Integer current, @RequestBody StudentPlusVo studentPlusVo) { // 查询 Page<StudentPlus> page = studentPlusService.condition(size,current,studentPlusVo); // 返回 return BaseResult.ok("查询成功",page); } }
-
启动类
package com.czxy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class StudentPlusServiceApplication { public static void main(String[] args) { SpringApplication.run(StudentPlusServiceApplication.class,args); } }