🎨 改进结构和代码格式. add spring-javaformat plugin and format all code

This commit is contained in:
lengleng committed 2020-06-14 17:59:04 +08:00
1 parent 5414983f3d
commit bd47066299
651 files changed
+13034 -12462

No files matched your search

@@ -26,8 +26,7 @@ import org.springframework.cloud.client.SpringCloudApplication;
/**
* @author lengleng
* @date 2018/07/29
* 代码生成模块
* @date 2018/07/29 代码生成模块
*/
@EnableDynamicDataSource
@EnablePigxSwagger2
@@ -36,8 +35,8 @@ import org.springframework.cloud.client.SpringCloudApplication;
@EnablePigxResourceServer
public class PigxCodeGenApplication {
public static void main(String[] args) {
SpringApplication.run(PigxCodeGenApplication.class, args);
}
}
@@ -25,7 +25,6 @@ import com.pig4cloud.pigx.common.log.annotation.SysLog;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
/**
* 数据源管理
*
@@ -36,13 +35,12 @@ import org.springframework.web.bind.annotation.*;
@AllArgsConstructor
@RequestMapping("/dsconf")
public class GenDsConfController {
private final GenDatasourceConfService datasourceConfService;
private final GenDatasourceConfService datasourceConfService;
/**
* 分页查询
*
* @param page 分页对象
* @param page 分页对象
* @param datasourceConf 数据源表
* @return
*/
@@ -53,7 +51,6 @@ public class GenDsConfController {
/**
* 查询全部数据源
*
* @return
*/
@GetMapping("/list")
@@ -61,10 +58,8 @@ public class GenDsConfController {
return R.ok(datasourceConfService.list());
}
/**
* 通过id查询数据源表
*
* @param id id
* @return R
*/
@@ -75,7 +70,6 @@ public class GenDsConfController {
/**
* 新增数据源表
*
* @param datasourceConf 数据源表
* @return R
*/
@@ -87,7 +81,6 @@ public class GenDsConfController {
/**
* 修改数据源表
*
* @param conf 数据源表
* @return R
*/
@@ -99,7 +92,6 @@ public class GenDsConfController {
/**
* 通过id删除数据源表
*
* @param id id
* @return R
*/
@@ -108,4 +100,5 @@ public class GenDsConfController {
public R removeById(@PathVariable Integer id) {
return R.ok(datasourceConfService.removeByDsId(id));
}
}
@@ -29,7 +29,6 @@ import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
/**
* 表单管理
*
@@ -41,12 +40,12 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/form")
@Api(value = "form", tags = "表单管理")
public class GenFormConfController {
private final GenFormConfService genRecordService;
/**
* 分页查询
*
* @param page 分页对象
* @param page 分页对象
* @param formConf 生成记录
* @return
*/
@@ -56,10 +55,8 @@ public class GenFormConfController {
return R.ok(genRecordService.page(page, Wrappers.query(formConf)));
}
/**
* 通过id查询生成记录
*
* @param id id
* @return R
*/
@@ -71,8 +68,7 @@ public class GenFormConfController {
/**
* 通过id查询生成记录
*
* @param dsName 数据源ID
* @param dsName 数据源ID
* @param tableName tableName
* @return R
*/
@@ -84,7 +80,6 @@ public class GenFormConfController {
/**
* 新增生成记录
*
* @param formConf 生成记录
* @return R
*/
@@ -98,7 +93,6 @@ public class GenFormConfController {
/**
* 修改生成记录
*
* @param formConf 生成记录
* @return R
*/
@@ -112,7 +106,6 @@ public class GenFormConfController {
/**
* 通过id删除生成记录
*
* @param id id
* @return R
*/
@@ -40,14 +40,15 @@ import javax.servlet.http.HttpServletResponse;
@AllArgsConstructor
@RequestMapping("/generator")
public class GeneratorController {
private final GeneratorService generatorService;
private final GenTableColumnService columnService;
/**
* 列表
*
* @param tableName 参数集
* @param dsName 数据源编号
* @param dsName 数据源编号
* @return 数据库表
*/
@GetMapping("/page")
@@ -57,7 +58,6 @@ public class GeneratorController {
/**
* 预览代码
*
* @param genConfig 数据表配置
* @return
*/
@@ -68,8 +68,7 @@ public class GeneratorController {
/**
* 查询表的列信息
*
* @param page 分页
* @param page 分页
* @param genConfig 数据表配置
* @return
*/
@@ -78,10 +77,8 @@ public class GeneratorController {
return R.ok(columnService.listTable(page, genConfig));
}
/**
* 生成代码
*
* @param genConfig 数据表配置
*/
@SneakyThrows
@@ -89,10 +86,12 @@ public class GeneratorController {
public void generatorCode(@RequestBody GenConfig genConfig, HttpServletResponse response) {
byte[] data = generatorService.generatorCode(genConfig);
response.reset();
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=%s.zip", genConfig.getTableName()));
response.setHeader(HttpHeaders.CONTENT_DISPOSITION,
String.format("attachment; filename=%s.zip", genConfig.getTableName()));
response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length));
response.setContentType("application/octet-stream; charset=UTF-8");
IoUtil.write(response.getOutputStream(), Boolean.TRUE, data);
}
}
@@ -21,23 +21,26 @@ import lombok.Data;
/**
* @author lengleng
* @date 2018/07/29
* 列属性: https://blog.csdn.net/lkforce/article/details/79557482
* @date 2018/07/29 列属性: https://blog.csdn.net/lkforce/article/details/79557482
*/
@Data
public class ColumnEntity {
/**
* 列表
*/
private String columnName;
/**
* 数据类型
*/
private String dataType;
/**
* JAVA 数据类型
*/
private String javaType;
/**
* 备注
*/
@@ -47,28 +50,35 @@ public class ColumnEntity {
* 驼峰属性
*/
private String caseAttrName;
/**
* 普通属性
*/
private String lowerAttrName;
/**
* 属性类型
*/
private String attrType;
/**
* 其他信息
*/
private String extra;
/**
* 字段类型
*/
private String columnType;
/**
* 是否可以为空
*/
private Boolean nullable;
/**
* 是否隐藏
*/
private Boolean hidden;
}
@@ -21,27 +21,31 @@ import lombok.Data;
/**
* @author lengleng
* @date 2018/8/2
* 生成配置
* @date 2018/8/2 生成配置
*/
@Data
public class GenConfig {
/**
* 数据源name
*/
private String dsName;
/**
* 包名
*/
private String packageName;
/**
* 作者
*/
private String author;
/**
* 模块名称
*/
private String moduleName;
/**
* 表前缀
*/
@@ -58,9 +62,8 @@ public class GenConfig {
private String comments;
/**
* 代码风格
* 0 - avue
* 1 - element
* 代码风格 0 - avue 1 - element
*/
private String style;
}
@@ -35,6 +35,7 @@ import java.time.LocalDateTime;
@TableName("gen_datasource_conf")
@EqualsAndHashCode(callSuper = true)
public class GenDatasourceConf extends Model<GenDatasourceConf> {
private static final long serialVersionUID = 1L;
/**
@@ -42,35 +43,43 @@ public class GenDatasourceConf extends Model<GenDatasourceConf> {
*/
@TableId
private Integer id;
/**
* 名称
*/
private String name;
/**
* jdbcurl
*/
private String url;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 创建时间
*/
private LocalDateTime createDate;
/**
* 更新
*/
private LocalDateTime updateDate;
/**
* 删除标记
*/
@TableLogic
private String delFlag;
/**
* 租户ID
*/
@@ -38,6 +38,7 @@ import java.time.LocalDateTime;
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "生成记录")
public class GenFormConf extends Model<GenFormConf> {
private static final long serialVersionUID = 1L;
/**
@@ -46,34 +47,41 @@ public class GenFormConf extends Model<GenFormConf> {
@TableId
@ApiModelProperty(value = "ID")
private Integer id;
/**
* 表名称
*/
@ApiModelProperty(value = "表名称")
private String tableName;
/**
* 表单信息
*/
@ApiModelProperty(value = "表单信息")
private String formInfo;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间")
private LocalDateTime createTime;
/**
* 修改时间
*/
@ApiModelProperty(value = "修改时间")
private LocalDateTime updateTime;
/**
* 删除标记
*/
@ApiModelProperty(value = "删除标记")
private String delFlag;
/**
* 所属租户
*/
@ApiModelProperty(value = "所属租户", hidden = true)
private Integer tenantId;
}
@@ -23,33 +23,39 @@ import java.util.List;
/**
* @author lengleng
* @date 2018/07/29
* 表属性: https://blog.csdn.net/lkforce/article/details/79557482
* @date 2018/07/29 表属性: https://blog.csdn.net/lkforce/article/details/79557482
*/
@Data
public class TableEntity {
/**
* 名称
*/
private String tableName;
/**
* 备注
*/
private String comments;
/**
* 主键
*/
private ColumnEntity pk;
/**
* 列名
*/
private List<ColumnEntity> columns;
/**
* 驼峰类型
*/
private String caseClassName;
/**
* 普通类型
*/
private String lowerClassName;
}
@@ -20,18 +20,17 @@ public interface GenTableColumnMapper extends BaseMapper<ColumnEntity> {
/**
* 分页查询表分页信息
*
* @param page
* @param tableName
* @param dsName
* @return
*/
@DS("#last")
IPage<ColumnEntity> selectTableColumn(Page page, @Param("tableName") String tableName, @Param("dsName") String dsName);
IPage<ColumnEntity> selectTableColumn(Page page, @Param("tableName") String tableName,
@Param("dsName") String dsName);
/**
* 查询表全部列信息
*
* @param tableName
* @param dsName
* @return
@@ -39,14 +38,13 @@ public interface GenTableColumnMapper extends BaseMapper<ColumnEntity> {
@DS("#last")
List<ColumnEntity> selectTableColumn(@Param("tableName") String tableName, @Param("dsName") String dsName);
/**
* 查询表全部列信息
*
* @param tableName 表名称
* @param dsName 数据源名称
* @param dsName 数据源名称
* @return
*/
@DS("#last")
List<Map<String, String>> selectMapTableColumn(@Param("tableName") String tableName, String dsName);
}
@@ -37,8 +37,7 @@ public interface GeneratorMapper {
/**
* 分页查询表格
*
* @param page 分页信息
* @param page 分页信息
* @param tableName 表名称
* @return
*/
@@ -46,11 +45,11 @@ public interface GeneratorMapper {
/**
* 查询表信息
*
* @param tableName 表名称
* @param dsName 数据源名称
* @param dsName 数据源名称
* @return
*/
@DS("#last")
Map<String, String> queryTable(@Param("tableName") String tableName, String dsName);
}
@@ -26,9 +26,9 @@ import com.pig4cloud.pigx.codegen.entity.GenDatasourceConf;
* @date 2019-03-31 16:00:20
*/
public interface GenDatasourceConfService extends IService<GenDatasourceConf> {
/**
* 保存数据源并且加密
*
* @param genDatasourceConf
* @return
*/
@@ -36,7 +36,6 @@ public interface GenDatasourceConfService extends IService<GenDatasourceConf> {
/**
* 更新数据源
*
* @param genDatasourceConf
* @return
*/
@@ -44,16 +43,13 @@ public interface GenDatasourceConfService extends IService<GenDatasourceConf> {
/**
* 更新动态数据的数据源列表
*
* @param datasourceConf
* @return
*/
void addDynamicDataSource(GenDatasourceConf datasourceConf);
/**
* 校验数据源配置是否有效
*
* @param datasourceConf 数据源信息
* @return 有效/无效
*/
@@ -61,9 +57,9 @@ public interface GenDatasourceConfService extends IService<GenDatasourceConf> {
/**
* 通过数据源名称删除
*
* @param dsId 数据源ID
* @return
*/
Boolean removeByDsId(Integer dsId);
}
@@ -30,8 +30,7 @@ public interface GenFormConfService extends IService<GenFormConf> {
/**
* 获取表单信息
*
* @param dsName 数据源ID
* @param dsName 数据源ID
* @param tableName 表名称
* @return
*/
@@ -33,10 +33,10 @@ public interface GenTableColumnService extends IService<ColumnEntity> {
/**
* 查询表的字段信息
*
* @param page
* @param genConfig 查询条件
* @return
*/
IPage<ColumnEntity> listTable(Page page, GenConfig genConfig);
}
@@ -29,9 +29,9 @@ import java.util.Map;
* @date 2018/7/29
*/
public interface GeneratorService {
/**
* 生成代码
*
* @param genConfig 生成信息
* @return
*/
@@ -39,19 +39,18 @@ public interface GeneratorService {
/**
* 分页查询表
*
* @param page 分页信息
* @param page 分页信息
* @param tableName 表名
* @param name 数据源ID
* @param name 数据源ID
* @return
*/
IPage<List<Map<String, Object>>> getPage(Page page, String tableName, String name);
/**
* 预览代码
*
* @param genConfig 生成信息
* @return
*/
Map<String, String> previewCode(GenConfig genConfig);
}
@@ -44,13 +44,15 @@ import java.sql.SQLException;
@Slf4j
@Service
@AllArgsConstructor
public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfMapper, GenDatasourceConf> implements GenDatasourceConfService {
public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfMapper, GenDatasourceConf>
implements GenDatasourceConfService {
private final StringEncryptor stringEncryptor;
private final DataSourceCreator dataSourceCreator;
/**
* 保存数据源并且加密
*
* @param conf
* @return
*/
@@ -61,7 +63,7 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
return Boolean.FALSE;
}
//添加动态数据源
// 添加动态数据源
addDynamicDataSource(conf);
// 更新数据库配置
@@ -72,7 +74,6 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
/**
* 更新数据源
*
* @param conf 数据源信息
* @return
*/
@@ -81,12 +82,11 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
if (!checkDataSource(conf)) {
return Boolean.FALSE;
}
//先移除
// 先移除
DynamicRoutingDataSource dynamicRoutingDataSource = SpringContextHolder.getBean(DynamicRoutingDataSource.class);
dynamicRoutingDataSource.removeDataSource(
baseMapper.selectById(conf.getId()).getName());
dynamicRoutingDataSource.removeDataSource(baseMapper.selectById(conf.getId()).getName());
//再添加
// 再添加
addDynamicDataSource(conf);
// 更新数据库配置
@@ -97,25 +97,21 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
return Boolean.TRUE;
}
/**
* 通过数据源名称删除
*
* @param dsId 数据源ID
* @return
*/
@Override
public Boolean removeByDsId(Integer dsId) {
DynamicRoutingDataSource dynamicRoutingDataSource = SpringContextHolder.getBean(DynamicRoutingDataSource.class);
dynamicRoutingDataSource.removeDataSource(
baseMapper.selectById(dsId).getName());
dynamicRoutingDataSource.removeDataSource(baseMapper.selectById(dsId).getName());
this.baseMapper.deleteById(dsId);
return Boolean.TRUE;
}
/**
* 添加动态数据源
*
* @param conf 数据源信息
*/
@Override
@@ -133,18 +129,19 @@ public class GenDatasourceConfServiceImpl extends ServiceImpl<GenDatasourceConfM
/**
* 校验数据源配置是否有效
*
* @param conf 数据源信息
* @return 有效/无效
*/
@Override
public Boolean checkDataSource(GenDatasourceConf conf) {
try (Connection connection = DriverManager.getConnection(
conf.getUrl(), conf.getUsername(), conf.getPassword())) {
} catch (SQLException e) {
try (Connection connection = DriverManager.getConnection(conf.getUrl(), conf.getUsername(),
conf.getPassword())) {
}
catch (SQLException e) {
log.error("数据源配置 {} , 获取链接失败", conf.getName(), e);
return Boolean.FALSE;
}
return Boolean.TRUE;
}
}
@@ -49,21 +49,19 @@ import java.util.Properties;
@Service
@AllArgsConstructor
public class GenFormConfServiceImpl extends ServiceImpl<GenFormConfMapper, GenFormConf> implements GenFormConfService {
private final GenTableColumnMapper tableColumnMapper;
/**
* 1. 根据数据源、表名称,查询已配置表单信息
* 2. 不存在调用模板生成
*
* @param dsName 数据源ID
* 1. 根据数据源、表名称,查询已配置表单信息 2. 不存在调用模板生成
* @param dsName 数据源ID
* @param tableName 表名称
* @return
*/
@Override
@SneakyThrows
public String getForm(String dsName, String tableName) {
GenFormConf form = getOne(Wrappers.<GenFormConf>lambdaQuery()
.eq(GenFormConf::getTableName, tableName)
GenFormConf form = getOne(Wrappers.<GenFormConf>lambdaQuery().eq(GenFormConf::getTableName, tableName)
.orderByDesc(GenFormConf::getCreateTime), false);
if (form != null) {
@@ -71,7 +69,7 @@ public class GenFormConfServiceImpl extends ServiceImpl<GenFormConfMapper, GenFo
}
List<ColumnEntity> columns = tableColumnMapper.selectTableColumn(tableName, dsName);
//设置velocity资源加载器
// 设置velocity资源加载器
Properties prop = new Properties();
prop.put("file.resource.loader.class", ClasspathResourceLoader.class.getName());
Velocity.init(prop);
@@ -21,10 +21,13 @@ import org.springframework.stereotype.Service;
*/
@Service
@AllArgsConstructor
public class GenTableColumnServiceImpl extends ServiceImpl<GenTableColumnMapper, ColumnEntity> implements GenTableColumnService {
public class GenTableColumnServiceImpl extends ServiceImpl<GenTableColumnMapper, ColumnEntity>
implements GenTableColumnService {
@Override
public IPage<ColumnEntity> listTable(Page page, GenConfig genConfig) {
IPage<ColumnEntity> columnPage = baseMapper.selectTableColumn(page, genConfig.getTableName(), genConfig.getDsName());
IPage<ColumnEntity> columnPage = baseMapper.selectTableColumn(page, genConfig.getTableName(),
genConfig.getDsName());
// 处理 数据库类型和 Java 类型关系
Configuration config = GenUtils.getConfig();
@@ -35,4 +38,5 @@ public class GenTableColumnServiceImpl extends ServiceImpl<GenTableColumnMapper,
});
return columnPage;
}
}
@@ -50,13 +50,15 @@ import java.util.zip.ZipOutputStream;
@Service
@AllArgsConstructor
public class GeneratorServiceImpl implements GeneratorService {
private final GenTableColumnMapper tableColumnMapper;
private final GeneratorMapper generatorMapper;
private final GenFormConfMapper genFormConfMapper;
/**
* 分页查询表
*
* @param tableName 查询条件
* @param dsName
* @return
@@ -69,23 +71,24 @@ public class GeneratorServiceImpl implements GeneratorService {
@Override
public Map<String, String> previewCode(GenConfig genConfig) {
//根据tableName 查询最新的表单配置
// 根据tableName 查询最新的表单配置
List<GenFormConf> formConfList = genFormConfMapper.selectList(Wrappers.<GenFormConf>lambdaQuery()
.eq(GenFormConf::getTableName, genConfig.getTableName())
.orderByDesc(GenFormConf::getCreateTime));
.eq(GenFormConf::getTableName, genConfig.getTableName()).orderByDesc(GenFormConf::getCreateTime));
DynamicDataSourceContextHolder.push(genConfig.getDsName());
String tableNames = genConfig.getTableName();
for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) {
//查询表信息
// 查询表信息
Map<String, String> table = generatorMapper.queryTable(tableName, genConfig.getDsName());
//查询列信息
List<Map<String, String>> columns = tableColumnMapper.selectMapTableColumn(tableName, genConfig.getDsName());
//生成代码
// 查询列信息
List<Map<String, String>> columns = tableColumnMapper.selectMapTableColumn(tableName,
genConfig.getDsName());
// 生成代码
if (CollUtil.isNotEmpty(formConfList)) {
return GenUtils.generatorCode(genConfig, table, columns, null, formConfList.get(0));
} else {
}
else {
return GenUtils.generatorCode(genConfig, table, columns, null, null);
}
}
@@ -94,16 +97,14 @@ public class GeneratorServiceImpl implements GeneratorService {
/**
* 生成代码
*
* @param genConfig 生成配置
* @return
*/
@Override
public byte[] generatorCode(GenConfig genConfig) {
//根据tableName 查询最新的表单配置
// 根据tableName 查询最新的表单配置
List<GenFormConf> formConfList = genFormConfMapper.selectList(Wrappers.<GenFormConf>lambdaQuery()
.eq(GenFormConf::getTableName, genConfig.getTableName())
.orderByDesc(GenFormConf::getCreateTime));
.eq(GenFormConf::getTableName, genConfig.getTableName()).orderByDesc(GenFormConf::getCreateTime));
DynamicDataSourceContextHolder.push(genConfig.getDsName());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@@ -111,18 +112,21 @@ public class GeneratorServiceImpl implements GeneratorService {
String tableNames = genConfig.getTableName();
for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) {
//查询表信息
// 查询表信息
Map<String, String> table = generatorMapper.queryTable(tableName, genConfig.getDsName());
//查询列信息
List<Map<String, String>> columns = tableColumnMapper.selectMapTableColumn(tableName, genConfig.getDsName());
//生成代码
// 查询列信息
List<Map<String, String>> columns = tableColumnMapper.selectMapTableColumn(tableName,
genConfig.getDsName());
// 生成代码
if (CollUtil.isNotEmpty(formConfList)) {
GenUtils.generatorCode(genConfig, table, columns, zip, formConfList.get(0));
} else {
}
else {
GenUtils.generatorCode(genConfig, table, columns, zip, null);
}
}
IoUtil.close(zip);
return outputStream.toByteArray();
}
}
@@ -49,7 +49,7 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 代码生成器 工具类
* 代码生成器 工具类
*
* @author lengleng
* @date 2018-07-30
@@ -57,23 +57,35 @@ import java.util.zip.ZipOutputStream;
@Slf4j
@UtilityClass
public class GenUtils {
public final String CRUD_PREFIX = "export const tableOption =";
private final String ENTITY_JAVA_VM = "Entity.java.vm";
private final String MAPPER_JAVA_VM = "Mapper.java.vm";
private final String SERVICE_JAVA_VM = "Service.java.vm";
private final String SERVICE_IMPL_JAVA_VM = "ServiceImpl.java.vm";
private final String CONTROLLER_JAVA_VM = "Controller.java.vm";
private final String MAPPER_XML_VM = "Mapper.xml.vm";
private final String MENU_SQL_VM = "menu.sql.vm";
private final String AVUE_INDEX_VUE_VM = "avue/index.vue.vm";
private final String ELE_INDEX_VUE_VM = "element/index.vue.vm";
private final String ELE_ADD_UPDATE_VUE_VM = "element/form.vue.vm";
private final String AVUE_API_JS_VM = "avue/api.js.vm";
private final String AVUE_CRUD_JS_VM = "avue/crud.js.vm";
/**
* 配置
*
* @param config
* @return
*/
@@ -91,7 +103,8 @@ public class GenUtils {
if (StyleTypeEnum.ELEMENT.getStyle().equals(config.getStyle())) {
templates.add("template/element/index.vue.vm");
templates.add("template/element/form.vue.vm");
} else {
}
else {
templates.add("template/avue/index.vue.vm");
templates.add("template/avue/crud.js.vm");
}
@@ -101,39 +114,40 @@ public class GenUtils {
/**
* 生成代码
*
* @return
*/
@SneakyThrows
public Map<String, String> generatorCode(GenConfig genConfig, Map<String, String> table,
List<Map<String, String>> columns, ZipOutputStream zip, GenFormConf formConf) {
//配置信息
List<Map<String, String>> columns, ZipOutputStream zip, GenFormConf formConf) {
// 配置信息
Configuration config = getConfig();
boolean hasBigDecimal = false;
//表信息
// 表信息
TableEntity tableEntity = new TableEntity();
tableEntity.setTableName(table.get("tableName"));
if (StrUtil.isNotBlank(genConfig.getComments())) {
tableEntity.setComments(genConfig.getComments());
} else {
}
else {
tableEntity.setComments(table.get("tableComment"));
}
String tablePrefix;
if (StrUtil.isNotBlank(genConfig.getTablePrefix())) {
tablePrefix = genConfig.getTablePrefix();
} else {
}
else {
tablePrefix = config.getString("tablePrefix");
}
//表名转换成Java类名
// 表名转换成Java类名
String className = tableToJava(tableEntity.getTableName(), tablePrefix);
tableEntity.setCaseClassName(className);
tableEntity.setLowerClassName(StringUtils.uncapitalize(className));
//获取需要在swagger文档中隐藏的属性字段
// 获取需要在swagger文档中隐藏的属性字段
List<Object> hiddenColumns = config.getList("hiddenColumn");
//列信息
// 列信息
List<ColumnEntity> columnList = new ArrayList<>();
for (Map<String, String> column : columns) {
ColumnEntity columnEntity = new ColumnEntity();
@@ -143,24 +157,25 @@ public class GenUtils {
columnEntity.setExtra(column.get("extra"));
columnEntity.setNullable("NO".equals(column.get("isNullable")));
columnEntity.setColumnType(column.get("columnType"));
//隐藏不需要的在接口文档中展示的字段
// 隐藏不需要的在接口文档中展示的字段
if (hiddenColumns.contains(column.get("columnName"))) {
columnEntity.setHidden(Boolean.TRUE);
} else {
}
else {
columnEntity.setHidden(Boolean.FALSE);
}
//列名转换成Java属性名
// 列名转换成Java属性名
String attrName = columnToJava(columnEntity.getColumnName());
columnEntity.setCaseAttrName(attrName);
columnEntity.setLowerAttrName(StringUtils.uncapitalize(attrName));
//列的数据类型,转换成Java类型
// 列的数据类型,转换成Java类型
String attrType = config.getString(columnEntity.getDataType(), "unknowType");
columnEntity.setAttrType(attrType);
if (!hasBigDecimal && "BigDecimal".equals(attrType)) {
hasBigDecimal = true;
}
//是否主键
// 是否主键
if ("PRI".equalsIgnoreCase(column.get("columnKey")) && tableEntity.getPk() == null) {
tableEntity.setPk(columnEntity);
}
@@ -169,13 +184,12 @@ public class GenUtils {
}
tableEntity.setColumns(columnList);
//没主键,则第一个字段为主键
// 没主键,则第一个字段为主键
if (tableEntity.getPk() == null) {
tableEntity.setPk(tableEntity.getColumns().get(0));
}
//封装模板数据
// 封装模板数据
Map<String, Object> map = new HashMap<>(16);
map.put("tableName", tableEntity.getTableName());
map.put("pk", tableEntity.getPk());
@@ -188,26 +202,30 @@ public class GenUtils {
if (StrUtil.isNotBlank(genConfig.getComments())) {
map.put("comments", genConfig.getComments());
} else {
}
else {
map.put("comments", tableEntity.getComments());
}
if (StrUtil.isNotBlank(genConfig.getAuthor())) {
map.put("author", genConfig.getAuthor());
} else {
}
else {
map.put("author", config.getString("author"));
}
if (StrUtil.isNotBlank(genConfig.getModuleName())) {
map.put("moduleName", genConfig.getModuleName());
} else {
}
else {
map.put("moduleName", config.getString("moduleName"));
}
if (StrUtil.isNotBlank(genConfig.getPackageName())) {
map.put("package", genConfig.getPackageName());
map.put("mainPath", genConfig.getPackageName());
} else {
}
else {
map.put("package", config.getString("package"));
map.put("mainPath", config.getString("mainPath"));
}
@@ -218,23 +236,23 @@ public class GenUtils {
/**
* 渲染数据
*
* @param genConfig 配置信息
* @param zip 流 (为空,直接返回Map)
* @param formConf 表单信息
* @param genConfig 配置信息
* @param zip 流 (为空,直接返回Map)
* @param formConf 表单信息
* @param tableEntity 表基本信息
* @param map 模板参数
* @param map 模板参数
* @return map key-filename value-contents
* @throws IOException
*/
private Map<String, String> renderData(GenConfig genConfig, ZipOutputStream zip, GenFormConf formConf, TableEntity tableEntity, Map<String, Object> map) throws IOException {
//设置velocity资源加载器
private Map<String, String> renderData(GenConfig genConfig, ZipOutputStream zip, GenFormConf formConf,
TableEntity tableEntity, Map<String, Object> map) throws IOException {
// 设置velocity资源加载器
Properties prop = new Properties();
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(prop);
VelocityContext context = new VelocityContext(map);
//获取模板列表
// 获取模板列表
List<String> templates = getTemplates(genConfig);
Map<String, String> resultMap = new HashMap<>(8);
@@ -242,13 +260,12 @@ public class GenUtils {
// 如果是crud
if (template.contains(AVUE_CRUD_JS_VM) && formConf != null) {
String fileName = getFileName(template, tableEntity.getCaseClassName()
, map.get("package").toString(), map.get("moduleName").toString());
String fileName = getFileName(template, tableEntity.getCaseClassName(), map.get("package").toString(),
map.get("moduleName").toString());
String contents = CRUD_PREFIX + formConf.getFormInfo();
if (zip != null) {
zip.putNextEntry(new ZipEntry(Objects
.requireNonNull(fileName)));
zip.putNextEntry(new ZipEntry(Objects.requireNonNull(fileName)));
IoUtil.write(zip, StandardCharsets.UTF_8, false, contents);
zip.closeEntry();
}
@@ -257,19 +274,17 @@ public class GenUtils {
continue;
}
//渲染模板
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, CharsetUtil.UTF_8);
tpl.merge(context, sw);
//添加到zip
String fileName = getFileName(template, tableEntity.getCaseClassName()
, map.get("package").toString(), map.get("moduleName").toString());
// 添加到zip
String fileName = getFileName(template, tableEntity.getCaseClassName(), map.get("package").toString(),
map.get("moduleName").toString());
if (zip != null) {
zip.putNextEntry(new ZipEntry(Objects
.requireNonNull(fileName)));
zip.putNextEntry(new ZipEntry(Objects.requireNonNull(fileName)));
IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString());
IoUtil.close(sw);
zip.closeEntry();
@@ -280,12 +295,11 @@ public class GenUtils {
return resultMap;
}
/**
* 列名转换成Java属性名
*/
public String columnToJava(String columnName) {
return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "");
return WordUtils.capitalizeFully(columnName, new char[] { '_' }).replace("_", "");
}
/**
@@ -304,7 +318,8 @@ public class GenUtils {
public Configuration getConfig() {
try {
return new PropertiesConfiguration("generator.properties");
} catch (ConfigurationException e) {
}
catch (ConfigurationException e) {
throw new CheckedException("获取配置文件失败,", e);
}
}
@@ -313,8 +328,8 @@ public class GenUtils {
* 获取文件名
*/
private String getFileName(String template, String className, String packageName, String moduleName) {
String packagePath = CommonConstants.BACK_END_PROJECT + File.separator + "src"
+ File.separator + "main" + File.separator + "java" + File.separator;
String packagePath = CommonConstants.BACK_END_PROJECT + File.separator + "src" + File.separator + "main"
+ File.separator + "java" + File.separator;
if (StringUtils.isNotBlank(packageName)) {
packagePath += packageName.replace(".", File.separator) + File.separator + moduleName + File.separator;
@@ -341,8 +356,8 @@ public class GenUtils {
}
if (template.contains(MAPPER_XML_VM)) {
return CommonConstants.BACK_END_PROJECT + File.separator + "src" + File.separator + "main"
+ File.separator + "resources" + File.separator + "mapper" + File.separator + className + "Mapper.xml";
return CommonConstants.BACK_END_PROJECT + File.separator + "src" + File.separator + "main" + File.separator
+ "resources" + File.separator + "mapper" + File.separator + className + "Mapper.xml";
}
if (template.contains(MENU_SQL_VM)) {
@@ -350,13 +365,14 @@ public class GenUtils {
}
if (template.contains(AVUE_INDEX_VUE_VM) || template.contains(ELE_INDEX_VUE_VM)) {
return CommonConstants.FRONT_END_PROJECT + File.separator + "src" + File.separator + "views" +
File.separator + moduleName + File.separator + className.toLowerCase() + File.separator + "index.vue";
return CommonConstants.FRONT_END_PROJECT + File.separator + "src" + File.separator + "views"
+ File.separator + moduleName + File.separator + className.toLowerCase() + File.separator
+ "index.vue";
}
if (template.contains(AVUE_API_JS_VM)) {
return CommonConstants.FRONT_END_PROJECT + File.separator + "src" + File.separator + "api"
+ File.separator + className.toLowerCase() + ".js";
return CommonConstants.FRONT_END_PROJECT + File.separator + "src" + File.separator + "api" + File.separator
+ className.toLowerCase() + ".js";
}
if (template.contains(AVUE_CRUD_JS_VM)) {
@@ -366,10 +382,11 @@ public class GenUtils {
if (template.contains(ELE_ADD_UPDATE_VUE_VM)) {
return CommonConstants.FRONT_END_PROJECT + File.separator + "src" + File.separator + "views"
+ File.separator + moduleName + File.separator + className.toLowerCase()
+ File.separator + className.toLowerCase() + "-form.vue";
+ File.separator + moduleName + File.separator + className.toLowerCase() + File.separator
+ className.toLowerCase() + "-form.vue";
}
return null;
}
}