mirror of
https://github.com/moshowgame/SpringBootCodeGenerator.git
synced 2025-12-26 05:48:33 +08:00
| 2025.12.09 | 优化Mybatis模板
This commit is contained in:
parent
2a354f7aba
commit
67185ad7af
@ -57,9 +57,9 @@ public class ${classInfo.className}Controller {
|
||||
* @author ${authorName}
|
||||
* @date ${.now?string('yyyy/MM/dd')}
|
||||
**/
|
||||
@RequestMapping("/load")
|
||||
public Object load(int id){
|
||||
return ${classInfo.className?uncap_first}Service.load(id);
|
||||
@RequestMapping("/find")
|
||||
public Object find(int id){
|
||||
return ${classInfo.className?uncap_first}Service.find(id);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -73,4 +73,34 @@ public class ${classInfo.className}Controller {
|
||||
return ${classInfo.className?uncap_first}Service.pageList(offset, pagesize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 动态条件分页查询 - 根据对象属性自动构建条件
|
||||
* 如果字段有值则进行分页+指定条件查询,否则仅进行分页查询
|
||||
* @author ${authorName}
|
||||
* @date ${.now?string('yyyy/MM/dd')}
|
||||
**/
|
||||
@RequestMapping("/pageByCondition")
|
||||
public Map<String, Object> pageByCondition(${classInfo.className} queryParamDTO,
|
||||
@RequestParam(required = false, defaultValue = "0") int offset,
|
||||
@RequestParam(required = false, defaultValue = "10") int pagesize) {
|
||||
return ${classInfo.className?uncap_first}Service.pageByCondition(queryParamDTO, offset, pagesize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 激活/停用
|
||||
* @author ${authorName}
|
||||
* @date ${.now?string('yyyy/MM/dd')}
|
||||
**/
|
||||
@RequestMapping("/active")
|
||||
public Object active(int id, Integer status) {
|
||||
${classInfo.className} ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}Service.find(id);
|
||||
if(${classInfo.className?uncap_first} != null) {
|
||||
${classInfo.className?uncap_first}.setStatus(status);
|
||||
Object result = ${classInfo.className?uncap_first}Service.update(${classInfo.className?uncap_first});
|
||||
return result;
|
||||
} else {
|
||||
return ${returnUtilFailure}("未找到记录");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ public interface ${classInfo.className}Mapper {
|
||||
* @author ${authorName}
|
||||
* @date ${.now?string('yyyy/MM/dd')}
|
||||
**/
|
||||
${classInfo.className} load(int id);
|
||||
${classInfo.className} find(int id);
|
||||
|
||||
/**
|
||||
* 查询 分页查询
|
||||
|
||||
@ -13,23 +13,33 @@ import java.util.List;
|
||||
@Repository
|
||||
public interface ${classInfo.className}Mapper {
|
||||
|
||||
@Select("select * from ${classInfo.tableName} where ${classInfo.tableName}_id=井{id}")
|
||||
public ${classInfo.className} getById(Integer id);
|
||||
@Select("""
|
||||
select * from ${classInfo.tableName} where ${classInfo.tableName}_id=井{id}
|
||||
""")
|
||||
public ${classInfo.className} find(Integer id);
|
||||
|
||||
@Options(useGeneratedKeys=true,keyProperty="${classInfo.className?uncap_first}Id")
|
||||
@Insert("insert into ${classInfo.tableName}" +
|
||||
" (<#list classInfo.fieldList as fieldItem >${fieldItem.columnName}<#if fieldItem_has_next>,</#if></#list>)" +
|
||||
" values(<#list classInfo.fieldList as fieldItem >${fieldItem.fieldName}<#if fieldItem_has_next>,<#else>)</#if></#list>")
|
||||
@Insert("""
|
||||
insert into ${classInfo.tableName} (
|
||||
<#list classInfo.fieldList as fieldItem >${fieldItem.columnName}<#if fieldItem_has_next>,</#if></#list>
|
||||
) values (
|
||||
<#list classInfo.fieldList as fieldItem >井{${fieldItem.fieldName}}<#if fieldItem_has_next>,<#else>)</#if></#list>
|
||||
)
|
||||
""")
|
||||
public Integer insert(${classInfo.className} ${classInfo.className?uncap_first});
|
||||
|
||||
@Delete(value = "delete from ${classInfo.tableName} where ${classInfo.tableName}_id=井{${classInfo.className?uncap_first}Id}")
|
||||
@Delete("""
|
||||
delete from ${classInfo.tableName} where ${classInfo.tableName}_id=井{id}
|
||||
""")
|
||||
boolean delete(Integer id);
|
||||
|
||||
@Update(value = "update ${classInfo.tableName} set "
|
||||
<#list classInfo.fieldList as fieldItem >
|
||||
<#if fieldItem.columnName != "id">+" ${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next>,</#if>"</#if>
|
||||
</#list>
|
||||
+" where ${classInfo.tableName}_id=井{${classInfo.className?uncap_first}Id} ")
|
||||
@Update("""
|
||||
update ${classInfo.tableName} set
|
||||
<#list classInfo.fieldList as fieldItem >
|
||||
<#if fieldItem.columnName != "id">${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next>,</#if></#if>
|
||||
</#list>
|
||||
where ${classInfo.tableName}_id=井{id}
|
||||
""")
|
||||
boolean update(${classInfo.className} ${classInfo.className?uncap_first});
|
||||
|
||||
|
||||
@ -38,19 +48,73 @@ public interface ${classInfo.className}Mapper {
|
||||
@Result(property = "${fieldItem.fieldName}", column = "${fieldItem.columnName}")<#if fieldItem_has_next>,</#if>
|
||||
</#list>
|
||||
})
|
||||
@Select(value = "select * from ${classInfo.tableName} where ${classInfo.tableName}_id=井{queryParam}")
|
||||
${classInfo.className} selectOne(String queryParam);
|
||||
@Select("""
|
||||
select * from ${classInfo.tableName} where ${classInfo.tableName}_id=井{id}
|
||||
""")
|
||||
${classInfo.className} selectOne(Integer id);
|
||||
|
||||
@Results(value = {
|
||||
<#list classInfo.fieldList as fieldItem >
|
||||
@Result(property = "${fieldItem.fieldName}", column = "${fieldItem.columnName}")<#if fieldItem_has_next>,</#if>
|
||||
</#list>
|
||||
})
|
||||
@Select(value = "select * from ${classInfo.tableName} where "
|
||||
<#list classInfo.fieldList as fieldItem >
|
||||
+" ${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next> or </#if>"
|
||||
</#list>
|
||||
)
|
||||
@Select("""
|
||||
select * from ${classInfo.tableName} where
|
||||
<#list classInfo.fieldList as fieldItem >
|
||||
${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next> or </#if>
|
||||
</#list>
|
||||
""")
|
||||
List<${classInfo.className}> selectList(${classInfo.className} ${classInfo.className?uncap_first});
|
||||
|
||||
/**
|
||||
* 动态条件分页查询 - 根据对象属性自动构建条件
|
||||
* 如果字段有值则进行分页+指定条件查询,否则仅进行分页查询
|
||||
*/
|
||||
@Select("""
|
||||
<script>
|
||||
SELECT * FROM ${classInfo.tableName}
|
||||
<where>
|
||||
<#list classInfo.fieldList as fieldItem>
|
||||
<#if fieldItem.fieldClass?contains("String")>
|
||||
<if test='queryParamDTO.${fieldItem.fieldName} != null and queryParamDTO.${fieldItem.fieldName} != ""'>
|
||||
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
|
||||
</if>
|
||||
<#else>
|
||||
<if test='queryParamDTO.${fieldItem.fieldName} != null'>
|
||||
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
|
||||
</if>
|
||||
</#if>
|
||||
</#list>
|
||||
</where>
|
||||
ORDER BY id DESC
|
||||
LIMIT 井{offset}, 井{limit}
|
||||
</script>
|
||||
""")
|
||||
List<${classInfo.className}> pageByCondition(@Param("queryParamDTO") ${classInfo.className} queryParamDTO,
|
||||
@Param("offset") int offset,
|
||||
@Param("limit") int limit);
|
||||
|
||||
/**
|
||||
* 动态条件分页查询总数
|
||||
*/
|
||||
@Select("""
|
||||
<script>
|
||||
SELECT COUNT(*) FROM ${classInfo.tableName}
|
||||
<where>
|
||||
<#list classInfo.fieldList as fieldItem>
|
||||
<#if fieldItem.fieldClass?contains("String")>
|
||||
<if test='queryParamDTO.${fieldItem.fieldName} != null and queryParamDTO.${fieldItem.fieldName} != ""'>
|
||||
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
|
||||
</if>
|
||||
<#else>
|
||||
<if test='queryParamDTO.${fieldItem.fieldName} != null'>
|
||||
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
|
||||
</if>
|
||||
</#if>
|
||||
</#list>
|
||||
</where>
|
||||
</script>
|
||||
""")
|
||||
int pageByConditionCount(@Param("queryParamDTO") ${classInfo.className} queryParamDTO);
|
||||
|
||||
}
|
||||
@ -26,11 +26,17 @@ public interface ${classInfo.className}Service {
|
||||
/**
|
||||
* 根据主键 id 查询
|
||||
*/
|
||||
public ${classInfo.className} load(int id);
|
||||
public ${classInfo.className} find(int id);
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
*/
|
||||
public Map<String,Object> pageList(int offset, int pagesize);
|
||||
|
||||
/**
|
||||
* 动态条件分页查询 - 根据对象属性自动构建条件
|
||||
* 如果字段有值则进行分页+指定条件查询,否则仅进行分页查询
|
||||
*/
|
||||
public Map<String,Object> pageByCondition(${classInfo.className} queryParamDTO, int offset, int pagesize);
|
||||
|
||||
}
|
||||
|
||||
@ -46,8 +46,8 @@ public class ${classInfo.className}ServiceImpl implements ${classInfo.className}
|
||||
|
||||
|
||||
@Override
|
||||
public ${classInfo.className} load(int id) {
|
||||
return ${classInfo.className?uncap_first}Mapper.load(id);
|
||||
public ${classInfo.className} find(int id) {
|
||||
return ${classInfo.className?uncap_first}Mapper.find(id);
|
||||
}
|
||||
|
||||
|
||||
@ -65,4 +65,18 @@ public class ${classInfo.className}ServiceImpl implements ${classInfo.className}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String,Object> pageByCondition(${classInfo.className} queryParamDTO, int offset, int pagesize) {
|
||||
|
||||
List<${classInfo.className}> pageList = ${classInfo.className?uncap_first}Mapper.pageByCondition(queryParamDTO, offset, pagesize);
|
||||
int totalCount = ${classInfo.className?uncap_first}Mapper.pageByConditionCount(queryParamDTO);
|
||||
|
||||
// result
|
||||
Map<String, Object> result = new HashMap<String, Object>();
|
||||
result.put("pageList", pageList);
|
||||
result.put("totalCount", totalCount);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user