Skip to content

Commit

Permalink
Support generate mybatis plus query condition
Browse files Browse the repository at this point in the history
  • Loading branch information
tangllty committed May 16, 2024
1 parent 08b7ab2 commit 91cf284
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 147 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@
* Support java 8 time type
* Support mybatis flex
* Support mybatis flex order query
* Support mybatis flex generate query condition
* Support generate mybatis flex query condition
* Support generate mybatis plus query condition
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.tang.extension.mybatisplus.utils

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.tang.commons.utils.page.PageResult
import com.tang.commons.utils.page.PageSupport

/**
* MyBatis Plus 分页工具类
*
* @author Tang
*/
object PageUtils {

/**
* 分页查询
*
* @param baseMapper 通用 Mapper 接口
* @param queryWrapper 查询条件
* @param <T> 实体类
* @return 分页结果
*/
@JvmStatic
fun <T> page(baseMapper: BaseMapper<T>, queryWrapper: QueryWrapper<T>): PageResult {
val pageDomain = PageSupport.buildPageRequest()
queryWrapper.orderBy(pageDomain.orderBy.isNotBlank(), pageDomain.isAscBoolean, pageDomain.orderByColumn)
val page = Page<T>(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong())
val selectPage = baseMapper.selectPage(page, queryWrapper)
return PageResult(selectPage.records, selectPage.total)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,24 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tang.commons.utils.AjaxResult;
import com.tang.commons.utils.page.PageUtils;
import com.tang.commons.utils.page.PageResult;
import ${packageName}.entity.$ClassName;
import ${packageName}.service.${ClassName}Service;

import lombok.RequiredArgsConstructor;

/**
* ${classComment}逻辑控制层
*
* @author $author
*/
@RequiredArgsConstructor
@RestController
@RequestMapping("/$moduleName/$businessName")
public class ${ClassName}Controller {

private final ${ClassName}Service ${className}Service;

public ${ClassName}Controller(${ClassName}Service ${className}Service) {
this.${className}Service = ${className}Service;
}

/**
* 查询${classComment}列表
*
Expand All @@ -40,16 +38,14 @@ public class ${ClassName}Controller {
@PreAuthorize("@auth.hasPermission('$moduleName.replace("/", ":"):$permissionName:list')")
@GetMapping("/list")
public PageResult list($ClassName $className){
PageUtils.startPage();
#foreach ($column in $columnList)
#set($javaField = $column.javaField)
#set($JavaField = $column.javaField.substring(0, 1).toUpperCase() + ${column.javaField.substring(1)})
#if ($column.htmlType == 'checkbox')
${className}.${javaField}ListTo${JavaField}();
#end
#end
var list = ${className}Service.select${ClassName}List($className);
return PageUtils.getDataTable(list);
return ${className}Service.select${ClassName}List($className);
}

/**
Expand All @@ -61,7 +57,7 @@ public class ${ClassName}Controller {
@PreAuthorize("@auth.hasPermission('$moduleName.replace("/", ":"):$permissionName:list')")
@GetMapping("/{$primaryKey}")
public AjaxResult select${ClassName}By${PrimaryKey}(@PathVariable Long $primaryKey) {
var $className = ${className}Service.getById($primaryKey);
var $className = ${className}Service.select${ClassName}By${PrimaryKey}($primaryKey);
#foreach ($column in $columnList)
#set($javaField = $column.javaField)
#set($JavaField = $column.javaField.substring(0, 1).toUpperCase() + ${column.javaField.substring(1)})
Expand All @@ -88,7 +84,7 @@ public class ${ClassName}Controller {
${className}.${javaField}ListTo${JavaField}();
#end
#end
return AjaxResult.rows(${className}Service.save($className));
return AjaxResult.rows(${className}Service.insert${ClassName}($className));
}

/**
Expand All @@ -107,7 +103,7 @@ public class ${ClassName}Controller {
${className}.${javaField}ListTo${JavaField}();
#end
#end
return AjaxResult.rows(${className}Service.updateById($className));
return AjaxResult.rows(${className}Service.update${ClassName}By${PrimaryKey}($className));
}

/**
Expand All @@ -119,7 +115,7 @@ public class ${ClassName}Controller {
@PreAuthorize("@auth.hasPermission('$moduleName.replace("/", ":"):$permissionName:delete')")
@DeleteMapping("/{$primaryKey}")
public AjaxResult delete(@PathVariable Long $primaryKey) {
return AjaxResult.rows(${className}Service.removeById($primaryKey));
return AjaxResult.rows(${className}Service.delete${ClassName}By${PrimaryKey}($primaryKey));
}

/**
Expand All @@ -131,7 +127,7 @@ public class ${ClassName}Controller {
@PreAuthorize("@auth.hasPermission('$moduleName.replace("/", ":"):$permissionName:delete')")
@DeleteMapping
public AjaxResult deletes(@RequestBody Long[] ${primaryKey}s) {
return AjaxResult.rows(${className}Service.removeByIds(${primaryKey}s));
return AjaxResult.rows(${className}Service.delete${ClassName}By${PrimaryKey}s(${primaryKey}s));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.tang.commons.base.entity.BaseEntity;

import lombok.Getter;
import lombok.Setter;

/**
* ${classComment}实体类 $tableName
*
* @author $author
*/
@Getter
@Setter
@TableName("$tableName")
public class $ClassName extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ${packageName}.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
Expand All @@ -14,53 +12,4 @@ import ${packageName}.entity.$ClassName;
*/
@Mapper
public interface ${ClassName}Mapper extends BaseMapper<$ClassName> {

/**
* 查询${classComment}列表
*
* @param $className ${classComment}对象
* @return ${classComment}列表
*/
List<$ClassName> select${ClassName}List($ClassName $className);

/**
* 通过${classComment}主键查询${classComment}信息
*
* @param $primaryKey ${classComment}主键
* @return ${classComment}信息
*/
$ClassName select${ClassName}By${PrimaryKey}(Long $primaryKey);

/**
* 新增${classComment}信息
*
* @param $className ${classComment}信息
* @return 影响行数
*/
int insert${ClassName}($ClassName $className);

/**
* 通过${classComment}主键修改${classComment}信息
*
* @param $className ${classComment}信息
* @return 影响行数
*/
int update${ClassName}By${PrimaryKey}($ClassName $className);

/**
* 通过${classComment}主键删除${classComment}信息
*
* @param $primaryKey ${classComment}主键
* @return 影响行数
*/
int delete${ClassName}By${PrimaryKey}(Long $primaryKey);

/**
* 通过${classComment}主键数组批量删除${classComment}信息
*
* @param ${primaryKey}s ${classComment}主键数组
* @return 影响行数
*/
int delete${ClassName}By${PrimaryKey}s(Long[] ${primaryKey}s);

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package ${packageName}.service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.IService;
import com.tang.commons.utils.page.PageResult;
import ${packageName}.entity.$ClassName;

/**
Expand All @@ -18,15 +17,15 @@ public interface ${ClassName}Service extends IService<$ClassName> {
* @param $className ${classComment}对象
* @return ${classComment}列表
*/
List<$ClassName> select${ClassName}List($ClassName $className);
PageResult select${ClassName}List($ClassName $className);

/**
* 通过${classComment}主键查询${classComment}信息
*
* @param $primaryKey ${classComment}主键
* @return ${classComment}信息
*/
$ClassName select${ClassName}By${PrimaryKey}(Long $primaryKey);
$ClassName select${ClassName}By${PrimaryKey}(Long $primaryKey);

/**
* 新增${classComment}信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,48 @@ import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tang.commons.utils.If;
import com.tang.extension.mybatisplus.utils.PageUtils;
import com.tang.commons.utils.page.PageResult;
import ${packageName}.entity.$ClassName;
import ${packageName}.mapper.${ClassName}Mapper;
import ${packageName}.service.${ClassName}Service;

import lombok.RequiredArgsConstructor;

/**
* ${classComment}业务逻辑层接口实现
*
* @author $author
*/
@RequiredArgsConstructor
@Service
public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, $ClassName> implements ${ClassName}Service {

private final ${ClassName}Mapper ${className}Mapper;

public ${ClassName}ServiceImpl(${ClassName}Mapper ${className}Mapper) {
this.${className}Mapper = ${className}Mapper;
}

/**
* 查询${classComment}列表
*
* @param $className ${classComment}对象
* @return ${classComment}列表
*/
@Override
public List<$ClassName> select${ClassName}List($ClassName $className) {
return ${className}Mapper.select${ClassName}List($className);
public PageResult select${ClassName}List($ClassName $className) {
var queryWrapper = new QueryWrapper<$ClassName>();
#foreach ($column in $columnList)
#if($column.queryType == "equal")
queryWrapper.lambda().eq(If.nonEmpty(${className}.get${column.columnNameUpperCamel}()), ${ClassName}::get${column.columnNameUpperCamel}, ${className}.get${column.columnNameUpperCamel}());
#end
#if($column.queryType == "fuzzy")
queryWrapper.lambda().like(If.nonEmpty(${className}.get${column.columnNameUpperCamel}()), ${ClassName}::get${column.columnNameUpperCamel}, ${className}.get${column.columnNameUpperCamel}());
#end
#if($column.queryType == "findInSet")
#end
#end
return PageUtils.page(${className}Mapper, queryWrapper);
}

/**
Expand All @@ -42,7 +56,7 @@ public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, $Cl
*/
@Override
public $ClassName select${ClassName}By${PrimaryKey}(Long $primaryKey) {
return ${className}Mapper.select${ClassName}By${PrimaryKey}($primaryKey);
return ${className}Mapper.selectById($primaryKey);
}

/**
Expand All @@ -53,7 +67,7 @@ public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, $Cl
*/
@Override
public int insert${ClassName}($ClassName $className) {
return ${className}Mapper.insert${ClassName}($className);
return ${className}Mapper.insert($className);
}

/**
Expand All @@ -64,7 +78,7 @@ public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, $Cl
*/
@Override
public int update${ClassName}By${PrimaryKey}($ClassName $className) {
return ${className}Mapper.update${ClassName}By${PrimaryKey}($className);
return ${className}Mapper.updateById($className);
}

/**
Expand All @@ -75,7 +89,7 @@ public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, $Cl
*/
@Override
public int delete${ClassName}By${PrimaryKey}(Long $primaryKey) {
return ${className}Mapper.delete${ClassName}By${PrimaryKey}($primaryKey);
return ${className}Mapper.deleteById($primaryKey);
}

/**
Expand All @@ -86,7 +100,7 @@ public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, $Cl
*/
@Override
public int delete${ClassName}By${PrimaryKey}s(Long[] ${primaryKey}s) {
return ${className}Mapper.delete${ClassName}By${PrimaryKey}s(${primaryKey}s);
return ${className}Mapper.deleteBatchIds(List.of(${primaryKey}s));
}

}
Loading

0 comments on commit 91cf284

Please sign in to comment.