mirror of https://github.com/elunez/eladmin
修复代码生成器issues中的#60,优化#64/#IWT0F的前三条
parent
4d9cbfe08b
commit
90c2bf906b
|
@ -29,6 +29,9 @@ public class ColumnInfo {
|
|||
/** 数据库字段键类型 **/
|
||||
private Object columnKey;
|
||||
|
||||
/** 额外的参数 **/
|
||||
private Object extra;
|
||||
|
||||
/** 查询 1:模糊 2:精确 **/
|
||||
private String columnQuery;
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||
|
||||
@Override
|
||||
public Object getColumns(String name) {
|
||||
StringBuilder sql = new StringBuilder("select column_name, is_nullable, data_type, column_comment, column_key from information_schema.columns where ");
|
||||
StringBuilder sql = new StringBuilder("select column_name, is_nullable, data_type, column_comment, column_key, extra from information_schema.columns where ");
|
||||
if(!ObjectUtils.isEmpty(name)){
|
||||
sql.append("table_name = '"+name+"' ");
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||
List<Object[]> result = query.getResultList();
|
||||
List<ColumnInfo> columnInfos = new ArrayList<>();
|
||||
for (Object[] obj : result) {
|
||||
columnInfos.add(new ColumnInfo(obj[0],obj[1],obj[2],obj[3],obj[4],null,"true"));
|
||||
columnInfos.add(new ColumnInfo(obj[0],obj[1],obj[2],obj[3],obj[4],obj[5],null,"true"));
|
||||
}
|
||||
return PageUtil.toPage(columnInfos,columnInfos.size());
|
||||
}
|
||||
|
|
|
@ -29,6 +29,8 @@ public class GenUtil {
|
|||
|
||||
private static final String PK = "PRI";
|
||||
|
||||
private static final String EXTRA = "auto_increment";
|
||||
|
||||
/**
|
||||
* 获取后端代码模板名称
|
||||
* @return
|
||||
|
@ -74,10 +76,12 @@ public class GenUtil {
|
|||
map.put("tableName",tableName);
|
||||
String className = StringUtils.toCapitalizeCamelCase(tableName);
|
||||
map.put("className", className);
|
||||
map.put("upperCaseClassName", className.toUpperCase());
|
||||
map.put("changeClassName", StringUtils.toCamelCase(tableName));
|
||||
map.put("hasTimestamp",false);
|
||||
map.put("hasBigDecimal",false);
|
||||
map.put("hasQuery",false);
|
||||
map.put("auto",false);
|
||||
|
||||
List<Map<String,Object>> columns = new ArrayList<>();
|
||||
List<Map<String,Object>> queryColumns = new ArrayList<>();
|
||||
|
@ -87,8 +91,12 @@ public class GenUtil {
|
|||
listMap.put("columnKey",column.getColumnKey());
|
||||
|
||||
String colType = ColUtil.cloToJava(column.getColumnType().toString());
|
||||
String changeColumnName = StringUtils.toCamelCase(column.getColumnName().toString());
|
||||
String capitalColumnName = StringUtils.toCapitalizeCamelCase(column.getColumnName().toString());
|
||||
if(PK.equals(column.getColumnKey())){
|
||||
map.put("pkColumnType",colType);
|
||||
map.put("pkChangeColName",changeColumnName);
|
||||
map.put("pkCapitalColName",capitalColumnName);
|
||||
}
|
||||
if(TIMESTAMP.equals(colType)){
|
||||
map.put("hasTimestamp",true);
|
||||
|
@ -96,12 +104,15 @@ public class GenUtil {
|
|||
if(BIGDECIMAL.equals(colType)){
|
||||
map.put("hasBigDecimal",true);
|
||||
}
|
||||
if(EXTRA.equals(column.getExtra())){
|
||||
map.put("auto",true);
|
||||
}
|
||||
listMap.put("columnType",colType);
|
||||
listMap.put("columnName",column.getColumnName());
|
||||
listMap.put("isNullable",column.getIsNullable());
|
||||
listMap.put("columnShow",column.getColumnShow());
|
||||
listMap.put("changeColumnName",StringUtils.toCamelCase(column.getColumnName().toString()));
|
||||
listMap.put("capitalColumnName",StringUtils.toCapitalizeCamelCase(column.getColumnName().toString()));
|
||||
listMap.put("changeColumnName",changeColumnName);
|
||||
listMap.put("capitalColumnName",capitalColumnName);
|
||||
|
||||
if(!StringUtils.isBlank(column.getColumnQuery())){
|
||||
listMap.put("columnQuery",column.getColumnQuery());
|
||||
|
|
|
@ -49,9 +49,5 @@ public class Dept implements Serializable {
|
|||
@CreationTimestamp
|
||||
private Timestamp createTime;
|
||||
|
||||
@ManyToMany(mappedBy = "depts")
|
||||
@JsonIgnore
|
||||
private Set<Role> roles;
|
||||
|
||||
public @interface Update {}
|
||||
}
|
|
@ -41,7 +41,7 @@ public class DictDetail implements Serializable {
|
|||
/**
|
||||
* 字典id
|
||||
*/
|
||||
@ManyToOne
|
||||
@ManyToOne(fetch=FetchType.LAZY)
|
||||
@JoinColumn(name = "dict_id")
|
||||
private Dict dict;
|
||||
|
||||
|
|
|
@ -19,6 +19,4 @@ public interface DeptRepository extends JpaRepository<Dept, Long>, JpaSpecificat
|
|||
* @return
|
||||
*/
|
||||
List<Dept> findByPid(Long id);
|
||||
|
||||
Set<Dept> findByRoles_Id(Long id);
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
package ${package}.rest;
|
||||
|
||||
import me.zhengjie.aop.log.Log;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import ${package}.domain.${className};
|
||||
import ${package}.service.${className}Service;
|
||||
import ${package}.service.dto.${className}DTO;
|
||||
|
@ -28,41 +27,33 @@ public class ${className}Controller {
|
|||
@Autowired
|
||||
private ${className}QueryService ${changeClassName}QueryService;
|
||||
|
||||
private static final String ENTITY_NAME = "${changeClassName}";
|
||||
|
||||
@Log("查询${className}")
|
||||
@GetMapping(value = "/${changeClassName}")
|
||||
@PreAuthorize("hasAnyRole('ADMIN')")
|
||||
@PreAuthorize("hasAnyRole('ADMIN','${upperCaseClassName}_ALL','${upperCaseClassName}_SELECT')")
|
||||
public ResponseEntity get${className}s(${className}DTO resources, Pageable pageable){
|
||||
return new ResponseEntity(${changeClassName}QueryService.queryAll(resources,pageable),HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Log("新增${className}")
|
||||
@PostMapping(value = "/${changeClassName}")
|
||||
@PreAuthorize("hasAnyRole('ADMIN')")
|
||||
@PreAuthorize("hasAnyRole('ADMIN','${upperCaseClassName}_ALL','${upperCaseClassName}_CREATE')")
|
||||
public ResponseEntity create(@Validated @RequestBody ${className} resources){
|
||||
if (resources.getId() != null) {
|
||||
throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
|
||||
}
|
||||
return new ResponseEntity(${changeClassName}Service.create(resources),HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@Log("修改${className}")
|
||||
@PutMapping(value = "/${changeClassName}")
|
||||
@PreAuthorize("hasAnyRole('ADMIN')")
|
||||
@PreAuthorize("hasAnyRole('ADMIN','${upperCaseClassName}_ALL','${upperCaseClassName}_EDIT')")
|
||||
public ResponseEntity update(@Validated @RequestBody ${className} resources){
|
||||
if (resources.getId() == null) {
|
||||
throw new BadRequestException(ENTITY_NAME +" ID Can not be empty");
|
||||
}
|
||||
${changeClassName}Service.update(resources);
|
||||
return new ResponseEntity(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Log("删除${className}")
|
||||
@DeleteMapping(value = "/${changeClassName}/{id}")
|
||||
@PreAuthorize("hasAnyRole('ADMIN')")
|
||||
public ResponseEntity delete(@PathVariable Long id){
|
||||
${changeClassName}Service.delete(id);
|
||||
@DeleteMapping(value = "/${changeClassName}/{${pkChangeColName}}")
|
||||
@PreAuthorize("hasAnyRole('ADMIN','${upperCaseClassName}_ALL','${upperCaseClassName}_DELETE')")
|
||||
public ResponseEntity delete(@PathVariable ${pkColumnType} ${pkChangeColName}){
|
||||
${changeClassName}Service.delete(${pkChangeColName});
|
||||
return new ResponseEntity(HttpStatus.OK);
|
||||
}
|
||||
}
|
|
@ -8,6 +8,11 @@ import java.sql.Timestamp;
|
|||
import java.math.BigDecimal;
|
||||
</#if>
|
||||
import java.io.Serializable;
|
||||
<#if !auto && pkColumnType = 'Long'>
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
</#if>
|
||||
|
||||
|
||||
/**
|
||||
* @author ${author}
|
||||
|
@ -23,6 +28,12 @@ public class ${className}DTO implements Serializable {
|
|||
* ${column.columnComment}
|
||||
*/
|
||||
</#if>
|
||||
<#if column.columnKey = 'PRI'>
|
||||
<#if !auto && pkColumnType = 'Long'>
|
||||
// 处理精度丢失问题
|
||||
@JsonSerialize(using= ToStringSerializer.class)
|
||||
</#if>
|
||||
</#if>
|
||||
private ${column.columnType} ${column.changeColumnName};
|
||||
</#list>
|
||||
</#if>
|
||||
|
|
|
@ -28,8 +28,10 @@ public class ${className} implements Serializable {
|
|||
</#if>
|
||||
<#if column.columnKey = 'PRI'>
|
||||
@Id
|
||||
<#if auto>
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
</#if>
|
||||
</#if>
|
||||
@Column(name = "${column.columnName}"<#if column.columnKey = 'UNI'>,unique = true</#if><#if column.isNullable = 'NO' && column.columnKey != 'PRI'>,nullable = false</#if>)
|
||||
private ${column.columnType} ${column.changeColumnName};
|
||||
</#list>
|
||||
|
|
|
@ -85,8 +85,8 @@ public class ${className}QueryService {
|
|||
}
|
||||
</#list>
|
||||
</#if>
|
||||
Predicate[] p = new Predicate[list.size()];
|
||||
return cb.and(list.toArray(p));
|
||||
Predicate[] p = new Predicate[list.size()];
|
||||
return cb.and(list.toArray(p));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -15,11 +15,11 @@ public interface ${className}Service {
|
|||
|
||||
/**
|
||||
* findById
|
||||
* @param id
|
||||
* @param ${pkChangeColName}
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(key = "#p0")
|
||||
${className}DTO findById(${pkColumnType} id);
|
||||
${className}DTO findById(${pkColumnType} ${pkChangeColName});
|
||||
|
||||
/**
|
||||
* create
|
||||
|
@ -38,8 +38,8 @@ public interface ${className}Service {
|
|||
|
||||
/**
|
||||
* delete
|
||||
* @param id
|
||||
* @param ${pkChangeColName}
|
||||
*/
|
||||
@CacheEvict(allEntries = true)
|
||||
void delete(Long id);
|
||||
void delete(${pkColumnType} ${pkChangeColName});
|
||||
}
|
|
@ -20,6 +20,13 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import java.util.Optional;
|
||||
<#if !auto && pkColumnType = 'Long'>
|
||||
import cn.hutool.core.lang.Snowflake;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
</#if>
|
||||
<#if !auto && pkColumnType = 'String'>
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
</#if>
|
||||
|
||||
/**
|
||||
* @author ${author}
|
||||
|
@ -36,15 +43,22 @@ public class ${className}ServiceImpl implements ${className}Service {
|
|||
private ${className}Mapper ${changeClassName}Mapper;
|
||||
|
||||
@Override
|
||||
public ${className}DTO findById(${pkColumnType} id) {
|
||||
Optional<${className}> ${changeClassName} = ${changeClassName}Repository.findById(id);
|
||||
ValidationUtil.isNull(${changeClassName},"${className}","id",id);
|
||||
public ${className}DTO findById(${pkColumnType} ${pkChangeColName}) {
|
||||
Optional<${className}> ${changeClassName} = ${changeClassName}Repository.findById(${pkChangeColName});
|
||||
ValidationUtil.isNull(${changeClassName},"${className}","${pkChangeColName}",${pkChangeColName});
|
||||
return ${changeClassName}Mapper.toDto(${changeClassName}.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ${className}DTO create(${className} resources) {
|
||||
<#if !auto && pkColumnType = 'Long'>
|
||||
Snowflake snowflake = IdUtil.createSnowflake(1, 1);
|
||||
resources.set${pkCapitalColName}(snowflake.nextId());
|
||||
</#if>
|
||||
<#if !auto && pkColumnType = 'String'>
|
||||
resources.set${pkCapitalColName}(IdUtil.simpleUUID());
|
||||
</#if>
|
||||
<#if columns??>
|
||||
<#list columns as column>
|
||||
<#if column.columnKey = 'UNI'>
|
||||
|
@ -60,31 +74,31 @@ public class ${className}ServiceImpl implements ${className}Service {
|
|||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(${className} resources) {
|
||||
Optional<${className}> optional${className} = ${changeClassName}Repository.findById(resources.getId());
|
||||
ValidationUtil.isNull( optional${className},"${className}","id",resources.getId());
|
||||
Optional<${className}> optional${className} = ${changeClassName}Repository.findById(resources.get${pkCapitalColName}());
|
||||
ValidationUtil.isNull( optional${className},"${className}","id",resources.get${pkCapitalColName}());
|
||||
|
||||
${className} ${changeClassName} = optional${className}.get();
|
||||
<#if columns??>
|
||||
<#list columns as column>
|
||||
<#if column.columnKey = 'UNI'>
|
||||
<#if column_index = 1>
|
||||
${className} ${changeClassName}1 = null;
|
||||
${className} ${changeClassName}1 = null;
|
||||
</#if>
|
||||
${changeClassName}1 = ${changeClassName}Repository.findBy${column.capitalColumnName}(resources.get${column.capitalColumnName}());
|
||||
if(${changeClassName}1 != null && !${changeClassName}1.getId().equals(${changeClassName}.getId())){
|
||||
if(${changeClassName}1 != null && !${changeClassName}1.get${pkCapitalColName}().equals(${changeClassName}.get${pkCapitalColName}())){
|
||||
throw new EntityExistException(${className}.class,"${column.columnName}",resources.get${column.capitalColumnName}());
|
||||
}
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
// 此处需自己修改
|
||||
resources.setId(${changeClassName}.getId());
|
||||
resources.set${pkCapitalColName}(${changeClassName}.get${pkCapitalColName}());
|
||||
${changeClassName}Repository.save(resources);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(Long id) {
|
||||
${changeClassName}Repository.deleteById(id);
|
||||
public void delete(${pkColumnType} ${pkChangeColName}) {
|
||||
${changeClassName}Repository.deleteById(${pkChangeColName});
|
||||
}
|
||||
}
|
|
@ -8,9 +8,9 @@ export function add(data) {
|
|||
})
|
||||
}
|
||||
|
||||
export function del(id) {
|
||||
export function del(${pkChangeColName}) {
|
||||
return request({
|
||||
url: 'api/${changeClassName}/' + id,
|
||||
url: 'api/${changeClassName}/' + ${pkChangeColName},
|
||||
method: 'delete'
|
||||
})
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
<el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
|
||||
<#if columns??>
|
||||
<#list columns as column>
|
||||
<#if column.changeColumnName != 'id'>
|
||||
<#if column.changeColumnName != '${pkChangeColName}'>
|
||||
<el-form-item label="<#if column.columnComment != ''>${column.columnComment}<#else>${column.changeColumnName}</#if>">
|
||||
<el-input v-model="form.${column.changeColumnName}" style="width: 370px;"/>
|
||||
</el-form-item>
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
<!-- 新增 -->
|
||||
<div style="display: inline-block;margin: 0px 2px;">
|
||||
<el-button
|
||||
v-if="checkPermission(['ADMIN'])"
|
||||
v-permission="['ADMIN','${upperCaseClassName}_ALL','${upperCaseClassName}_CREATE']"
|
||||
class="filter-item"
|
||||
size="mini"
|
||||
type="primary"
|
||||
|
@ -23,7 +23,6 @@
|
|||
</template>
|
||||
|
||||
<script>
|
||||
import checkPermission from '@/utils/permission' // 权限判断函数
|
||||
import eForm from './form'
|
||||
export default {
|
||||
components: { eForm },
|
||||
|
@ -47,7 +46,7 @@ export default {
|
|||
}
|
||||
},
|
||||
methods: {
|
||||
checkPermission<#if hasQuery>,
|
||||
<#if hasQuery>
|
||||
toQuery() {
|
||||
this.$parent.page = 0
|
||||
this.$parent.init()
|
||||
|
|
|
@ -19,18 +19,18 @@
|
|||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
<el-table-column label="操作" width="150px" align="center">
|
||||
<el-table-column v-if="checkPermission(['ADMIN','${upperCaseClassName}_ALL','${upperCaseClassName}_EDIT','${upperCaseClassName}_DELETE'])" label="操作" width="150px" align="center">
|
||||
<template slot-scope="scope">
|
||||
<edit v-if="checkPermission(['ADMIN'])" :data="scope.row" :sup_this="sup_this"/>
|
||||
<edit v-permission="['ADMIN','${upperCaseClassName}_ALL','${upperCaseClassName}_EDIT']" :data="scope.row" :sup_this="sup_this"/>
|
||||
<el-popover
|
||||
v-if="checkPermission(['ADMIN'])"
|
||||
:ref="scope.row.id"
|
||||
v-permission="['ADMIN','${upperCaseClassName}_ALL','${upperCaseClassName}_DELETE']"
|
||||
:ref="scope.row.${pkChangeColName}"
|
||||
placement="top"
|
||||
width="180">
|
||||
<p>确定删除本条数据吗?</p>
|
||||
<div style="text-align: right; margin: 0">
|
||||
<el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
|
||||
<el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
|
||||
<el-button size="mini" type="text" @click="$refs[scope.row.${pkChangeColName}].doClose()">取消</el-button>
|
||||
<el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.${pkChangeColName})">确定</el-button>
|
||||
</div>
|
||||
<el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
|
||||
</el-popover>
|
||||
|
@ -76,7 +76,7 @@ export default {
|
|||
checkPermission,
|
||||
beforeInit() {
|
||||
this.url = 'api/${changeClassName}'
|
||||
const sort = 'id,desc'
|
||||
const sort = '${pkChangeColName},desc'
|
||||
this.params = { page: this.page, size: this.size, sort: sort }
|
||||
<#if hasQuery>
|
||||
const query = this.query
|
||||
|
@ -86,11 +86,11 @@ export default {
|
|||
</#if>
|
||||
return true
|
||||
},
|
||||
subDelete(id) {
|
||||
subDelete(${pkChangeColName}) {
|
||||
this.delLoading = true
|
||||
del(id).then(res => {
|
||||
del(${pkChangeColName}).then(res => {
|
||||
this.delLoading = false
|
||||
this.$refs[id].doClose()
|
||||
this.$refs[${pkChangeColName}].doClose()
|
||||
this.init()
|
||||
this.$notify({
|
||||
title: '删除成功',
|
||||
|
@ -99,7 +99,7 @@ export default {
|
|||
})
|
||||
}).catch(err => {
|
||||
this.delLoading = false
|
||||
this.$refs[id].doClose()
|
||||
this.$refs[${pkChangeColName}].doClose()
|
||||
console.log(err.response.data.message)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue