Skip to content

Commit

Permalink
Merge branch 'master' of gitee.com:leshalv/screw into master
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueve authored and gitee-org committed Aug 25, 2020
2 parents 81a3c95 + 62f7ae1 commit c95c3fe
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -79,7 +79,7 @@ public static Builder builder() {

public static class Builder {

private PojoConfiguration configuration;
private final PojoConfiguration configuration;

public Builder() {
this.configuration = new PojoConfiguration();
Expand Down Expand Up @@ -115,7 +115,7 @@ public Builder nameStrategy(NameStrategy strategy) {
return this;
}

public Builder customType(String type, Class clazz) {
public Builder customType(String type, Class<?> clazz) {
if (configuration.getCustomType() == null) {
configuration.setCustomType(new HashMap<>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
* Created by [email protected] on 2020-08-17
*/
public interface TypeDialect {

Class getClassTypeByFieldType(String type);
/**
* 通过字段类型获取类类型
* @param type {@link String}
* @return {@link Class}
*/
Class<?> getClassTypeByFieldType(String type);

/**
* 根据提供的map查询对应的java类型
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
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);
Expand All @@ -55,8 +55,8 @@ public MysqlTypeDialect() {
}

@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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@
import cn.smallbun.screw.extension.pojo.metadata.model.PojoModel;

/**
* POJO 引擎
*
* @author liu·yu
* Created by [email protected] 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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

Expand All @@ -29,7 +30,7 @@
* Created by [email protected] on 2020-08-14
*/
@Data
public class PojoModel {
public class PojoModel implements Serializable {

/**
* 包名
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

import lombok.Data;

import java.io.Serializable;

/**
* TypeModel
*
* @author liu·yu
* Created by [email protected] on 2020-08-14
*/
@Data
public class TypeModel {
public class TypeModel implements Serializable {

/**
* 数据库字段类型
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*/
public class PojoModelProcess implements PojoProcess {

private PojoConfiguration pojoConfiguration;
private final PojoConfiguration pojoConfiguration;

/**
* 构造方法
Expand All @@ -68,7 +68,7 @@ public List<PojoModel> getPojoModel() {
NameStrategy nameStrategy = pojoConfiguration.getNameStrategy();

//获取用户自定义的类型转换
Map<String, Class> customType = pojoConfiguration.getCustomType();
Map<String, Class<?>> customType = pojoConfiguration.getCustomType();

//进行数据转换
for (TableModel model : tableModels) {
Expand All @@ -91,7 +91,7 @@ public List<PojoModel> getPojoModel() {
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
* Created by [email protected] on 2020-08-20
*/
public interface PojoProcess {

/**
* 获取pojo模型
*
* @return {@link PojoModel}
*/
List<PojoModel> getPojoModel();

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@
*/
public class HumpNameStrategy implements NameStrategy {

private Pattern linePattern = Pattern.compile("_(\\w)");
private final Pattern linePattern = Pattern.compile("_(\\w)");

@Override
public String transClassName(String name) {
return upperCase(lineToHump(name));
}

@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));
}
Expand All @@ -69,8 +69,8 @@ private String lineToHump(String str) {
/**
* 首字母转大写
*
* @param str
* @return
* @param str {@link String}
* @return {@link String}
*/
private String upperCase(String str) {
char[] ch = str.toCharArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public String transClassName(String name) {
}

@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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ public class ${className} {
</#list>
</#if>
}

0 comments on commit c95c3fe

Please sign in to comment.