pom.xml
com.baomidou mybatis-plus-boot-starter 3.5.1 com.baomidou mybatis-plus-generator 3.5.1 org.apache.velocity velocity 1.7
分页插件
@Configuration
@MapperScan("com.example.mapper")
public class MybatisPlusConfig {
// 最新版
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/qing?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: root
password: 123456
server:
port: 9091
mybatis:
mapper-locations: classpath:mapper
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
#if(${kotlin})
class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end
#else
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end
@Resource
private ${table.serviceName} ${table.entityPath}Service;
// 新增或者更新
@PostMapping
public boolean save(@RequestBody ${entity} ${table.entityPath}) {
return ${table.entityPath}Service.saveOrUpdate(${table.entityPath});
}
@DeleteMapping("/{id}")
public Boolean delete(@PathVariable Integer id) {
return ${table.entityPath}Service.removeById(id);
}
@PostMapping("/del/batch")
public boolean deleteBatch(@RequestBody List ids) {
return ${table.entityPath}Service.removeByIds(ids);
}
@GetMapping
public List<${entity}> findAll() {
return ${table.entityPath}Service.list();
}
@GetMapping("/{id}")
public ${entity} findOne(@PathVariable Integer id) {
return ${table.entityPath}Service.getById(id);
}
@GetMapping("/page")
public Page<${entity}> findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize) {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("id");
return ${table.entityPath}Service.page(new Page<>(pageNum, pageSize), queryWrapper);
}
}
#end
实现功能有
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private IUserService userService;
// 新增或者更新
@PostMapping
public boolean save(@RequestBody User user) {
return userService.saveOrUpdate( user );
}
删除
@DeleteMapping("/{id}")
public Boolean delete(@PathVariable Integer id) {
return userService.removeById( id );
}
批量删除
@PostMapping("/del/batch")
public boolean deleteBatch(@RequestBody List ids) {
return userService.removeByIds( ids );
}
获取所有数据
@GetMapping
public List findAll() {
return userService.list();
}
按id查询
@GetMapping("/{id}")
public User findOne(@PathVariable Integer id) {
return userService.getById( id );
}
分页查询
@GetMapping("/page")
public Page findPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize) {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc( "id" );
return userService.page( new Page<>( pageNum, pageSize ), queryWrapper );
}
}



