Merge branch 'master' of gitee.com:leshalv/screw into master

This commit is contained in:
liuyueve
2020-08-25 09:55:07 +08:00
committed by Gitee
14 changed files with 98 additions and 57 deletions

View File

@@ -37,37 +37,37 @@ public class PojoConfiguration {
/**
* 包名
*/
private String packageName;
private String packageName;
/**
* 生成文件路径
*/
private String path;
private String path;
/**
* 是否使用lombok
*/
private boolean useLombok;
private boolean useLombok;
/**
* 数据源对象
*/
private DataSource dataSource;
private DataSource dataSource;
/**
* 生成配置
*/
private ProcessConfig processConfig;
private ProcessConfig processConfig;
/**
* 命名策略
*/
private NameStrategy nameStrategy;
private NameStrategy nameStrategy;
/**
* 自定义类型转换
*/
private Map<String, Class> customType;
private Map<String, Class<?>> customType;
/**
* builder
@@ -79,7 +79,7 @@ public class PojoConfiguration {
public static class Builder {
private PojoConfiguration configuration;
private final PojoConfiguration configuration;
public Builder() {
this.configuration = new PojoConfiguration();
@@ -115,7 +115,7 @@ public class PojoConfiguration {
return this;
}
public Builder customType(String type, Class clazz) {
public Builder customType(String type, Class<?> clazz) {
if (configuration.getCustomType() == null) {
configuration.setCustomType(new HashMap<>());
}

View File

@@ -26,8 +26,12 @@ import java.util.Map;
* Created by 15952866402@163.com on 2020-08-17
*/
public interface TypeDialect {
Class getClassTypeByFieldType(String type);
/**
* 通过字段类型获取类类型
* @param type {@link String}
* @return {@link Class}
*/
Class<?> getClassTypeByFieldType(String type);
/**
* 根据提供的map查询对应的java类型
@@ -36,43 +40,61 @@ public interface TypeDialect {
* @param type 提供的类型
* @return 查找类型可能null
*/
default Class getTypeByMap(Map<String, Class> map, String type) {
if (type == null || map == null || map.size() == 0)
default Class<?> getTypeByMap(Map<String, Class<?>> map, String type) {
if (type == null || map == null || map.size() == 0) {
return null;
if (type.startsWith("date"))
}
if (type.startsWith("date")) {
return map.get("date");
if (type.startsWith("mediumint"))
}
if (type.startsWith("mediumint")) {
return map.get("mediumint");
if (type.startsWith("double"))
}
if (type.startsWith("double")) {
return map.get("double");
if (type.startsWith("varchar"))
}
if (type.startsWith("varchar")) {
return map.get("varchar");
if (type.startsWith("tinyint"))
}
if (type.startsWith("tinyint")) {
return map.get("tinyint");
if (type.startsWith("bit"))
}
if (type.startsWith("bit")) {
return map.get("bit");
if (type.startsWith("float"))
}
if (type.startsWith("float")) {
return map.get("float");
if (type.startsWith("int"))
}
if (type.startsWith("int")) {
return map.get("int");
if (type.startsWith("smallint"))
}
if (type.startsWith("smallint")) {
return map.get("smallint");
if (type.startsWith("datetime"))
}
if (type.startsWith("datetime")) {
return map.get("datetime");
if (type.startsWith("blob"))
}
if (type.startsWith("blob")) {
return map.get("blob");
if (type.startsWith("char"))
}
if (type.startsWith("char")) {
return map.get("char");
if (type.startsWith("text"))
}
if (type.startsWith("text")) {
return map.get("text");
if (type.startsWith("time"))
}
if (type.startsWith("time")) {
return map.get("time");
if (type.startsWith("decimal"))
}
if (type.startsWith("decimal")) {
return map.get("decimal");
if (type.startsWith("bigint"))
}
if (type.startsWith("bigint")) {
return map.get("bigint");
if (type.startsWith("timestamp"))
}
if (type.startsWith("timestamp")) {
return map.get("timestamp");
}
return null;
}

View File

@@ -44,9 +44,9 @@ public class TypeDialectFactory {
* DataSource
*/
@Getter
private DataSource dataSource;
private final DataSource dataSource;
private Map<DatabaseType, Class<? extends TypeDialect>> dialectMap;
private final Map<DatabaseType, Class<? extends TypeDialect>> dialectMap;
{
dialectMap = new HashMap<>();

View File

@@ -32,7 +32,7 @@ import java.util.Map;
*/
public class MysqlTypeDialect implements TypeDialect {
private Map<String, Class> map = new HashMap<>();
private final Map<String, Class<?>> map = new HashMap<>();
public MysqlTypeDialect() {
map.put("varchar", String.class);
@@ -55,8 +55,8 @@ public class MysqlTypeDialect implements TypeDialect {
}
@Override
public Class getClassTypeByFieldType(String type) {
Class clazz = getTypeByMap(map, type);
public Class<?> getClassTypeByFieldType(String type) {
Class<?> clazz = getTypeByMap(map, type);
if (clazz == null) {
clazz = Object.class;
}

View File

@@ -21,11 +21,19 @@ import cn.smallbun.screw.core.exception.ProduceException;
import cn.smallbun.screw.extension.pojo.metadata.model.PojoModel;
/**
* POJO 引擎
*
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-14
*/
public interface PojoEngine {
void produce(PojoModel info, String docName) throws ProduceException;
/**
* 生成
*
* @param info {@link PojoModel}
* @param file {@link String}
* @throws ProduceException ProduceException
*/
void produce(PojoModel info, String file) throws ProduceException;
}

View File

@@ -43,9 +43,9 @@ public class PojoExecute implements Execute {
/**
* LOGGER
*/
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private PojoConfiguration configuration;
private final PojoConfiguration configuration;
public PojoExecute(PojoConfiguration config) {
String validate = validate(config);

View File

@@ -19,6 +19,7 @@ package cn.smallbun.screw.extension.pojo.metadata.model;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
@@ -29,7 +30,7 @@ import java.util.Set;
* Created by 15952866402@163.com on 2020-08-14
*/
@Data
public class PojoModel {
public class PojoModel implements Serializable {
/**
* 包名

View File

@@ -19,6 +19,8 @@ package cn.smallbun.screw.extension.pojo.metadata.model;
import lombok.Data;
import java.io.Serializable;
/**
* TypeModel
*
@@ -26,7 +28,7 @@ import lombok.Data;
* Created by 15952866402@163.com on 2020-08-14
*/
@Data
public class TypeModel {
public class TypeModel implements Serializable {
/**
* 数据库字段类型

View File

@@ -41,7 +41,7 @@ import java.util.*;
*/
public class PojoModelProcess implements PojoProcess {
private PojoConfiguration pojoConfiguration;
private final PojoConfiguration pojoConfiguration;
/**
* 构造方法
@@ -68,7 +68,7 @@ public class PojoModelProcess implements PojoProcess {
NameStrategy nameStrategy = pojoConfiguration.getNameStrategy();
//获取用户自定义的类型转换
Map<String, Class> customType = pojoConfiguration.getCustomType();
Map<String, Class<?>> customType = pojoConfiguration.getCustomType();
//进行数据转换
for (TableModel model : tableModels) {
@@ -91,7 +91,7 @@ public class PojoModelProcess implements PojoProcess {
typeModel.setRemarks(column.getRemarks());
//先判断用户是否自定义
Class classType = dialect.getTypeByMap(customType, column.getTypeName());
Class<?> classType = dialect.getTypeByMap(customType, column.getTypeName());
if (classType == null) {
classType = dialect.getClassTypeByFieldType(column.getTypeName());
}

View File

@@ -28,7 +28,11 @@ import java.util.List;
* Created by 15952866402@163.com on 2020-08-20
*/
public interface PojoProcess {
/**
* 获取pojo模型
*
* @return {@link PojoModel}
*/
List<PojoModel> getPojoModel();
}

View File

@@ -28,7 +28,7 @@ import java.util.regex.Pattern;
*/
public class HumpNameStrategy implements NameStrategy {
private Pattern linePattern = Pattern.compile("_(\\w)");
private final Pattern linePattern = Pattern.compile("_(\\w)");
@Override
public String transClassName(String name) {
@@ -36,17 +36,17 @@ public class HumpNameStrategy implements NameStrategy {
}
@Override
public String transFieldName(String name, Class type) {
public String transFieldName(String name, Class<?> type) {
return lineToHump(name);
}
@Override
public String transSetName(String name, Class type) {
public String transSetName(String name, Class<?> type) {
return "set" + upperCase(lineToHump(name));
}
@Override
public String transGetName(String name, Class type) {
public String transGetName(String name, Class<?> type) {
if (Boolean.class.isAssignableFrom(type)) {
return "is" + upperCase(lineToHump(name));
}
@@ -69,8 +69,8 @@ public class HumpNameStrategy implements NameStrategy {
/**
* 首字母转大写
*
* @param str
* @return
* @param str {@link String}
* @return {@link String}
*/
private String upperCase(String str) {
char[] ch = str.toCharArray();

View File

@@ -37,24 +37,27 @@ public interface NameStrategy {
* 转换字段名
*
* @param name 输入的字段名
* @param type {@link Class}
* @return 输出转换后的名字
*/
String transFieldName(String name, Class type);
String transFieldName(String name, Class<?> type);
/**
* 转换set方法名
*
* @param name 输入的字段名
* @param type {@link Class}
* @return 输出的set方法名
*/
String transSetName(String name, Class type);
String transSetName(String name, Class<?> type);
/**
* 转换get方法名
*
* @param name 输入的字段名
* @param type {@link Class}
* @return 输出的方法名
*/
String transGetName(String name, Class type);
String transGetName(String name, Class<?> type);
}

View File

@@ -31,17 +31,17 @@ public class NoOpNameStrategy implements NameStrategy {
}
@Override
public String transFieldName(String name, Class type) {
public String transFieldName(String name, Class<?> type) {
return name;
}
@Override
public String transSetName(String name, Class type) {
public String transSetName(String name, Class<?> type) {
return "set" + name;
}
@Override
public String transGetName(String name, Class type) {
public String transGetName(String name, Class<?> type) {
return "get" + name;
}

View File

@@ -47,3 +47,4 @@ public class ${className} {
</#list>
</#if>
}