栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis-plus快速生成CURD and分页查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis-plus快速生成CURD and分页查询

环境准备

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 );
	}

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736848.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号