前言
读书须用意,一字值千金。
目标
MyBatis-Plus分页和增删改查(CRUD)
关键字
pagehelper
一、使用及场景
1添加pom
-- 版本
<mybatisplus.version>3.4.1</mybatisplus.version>
<pagehelper.version>5.1.11</pagehelper.version>
<!--pagehelper分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
2添加配置
@Configuration
public class MybatisPlusPageConfig {
/**
* pagehelper的分页插件
*/
@Bean
public PageInterceptor pageInterceptor() {
return new PageInterceptor();
}
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
3 分页 CRUD
@RestController
@RequestMapping("/demoPlus")
public class DemoPlusController {
private Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private DemoPlusServiceImpl demoPlusServiceImpl;
/**
* 分页实现
* @param page 当前页 默认1
* @param limit 最大查询条数 默认10
* @return
*/
@GetMapping("/getDemoPlus")
public Object getDemoPlus(@RequestParam(defaultValue="1") Integer page,
@RequestParam(defaultValue="10") Integer limit) {
DemoPlus demoPlus = new DemoPlus();
log.info("前台参数{}", JSONUtil.parseObj(demoPlus));
LambdaQueryWrapper<DemoPlus> queryWrapper = new QueryWrapper<DemoPlus>().lambda()
//模糊查询
.like(DemoPlus::getAge, "1");
Page<DemoPlus> mpPage = demoPlusServiceImpl.page(new Page<>(page, limit),queryWrapper);
return ResponseEntity.success(mpPage);
}
/**
* 添加用户
* @return
*/
@PostMapping("/changeDemoPlus")
public ResponseEntity changeDemoPlus(@RequestBody DemoPlus user) {
boolean update = demoPlusServiceImpl.saveOrUpdate(user);
return ResponseEntity.success(update);
}
/*
* 根据用户删除
*/
@GetMapping("/delete/{id}")
public ResponseEntity delete(@PathVariable Integer id) {
demoPlusServiceImpl.remove(Wrappers.<DemoPlus>query().in("id", id));
return ResponseEntity.success();
}
}
多留言多点赞你们的只支持是我坚持下去的动力,都支棱起来!!!