利用数据库直接生成java pojo对象

利用数据库直接生成java pojo对象
This commit is contained in:
smallbun
2020-08-20 16:33:48 +08:00
committed by Gitee
23 changed files with 1299 additions and 0 deletions

View File

@@ -10,6 +10,32 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>screw-extension</artifactId>
<dependencies>
<!--项目核心包-->
<dependency>
<groupId>cn.smallbun.screw</groupId>
<artifactId>screw-core</artifactId>
<version>1.0.5</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<!--build-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,3 +1,20 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Screw extension
*

View File

@@ -0,0 +1,132 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo;
import cn.smallbun.screw.core.process.ProcessConfig;
import cn.smallbun.screw.extension.pojo.strategy.NameStrategy;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-14
*/
@Data
@NoArgsConstructor
public class PojoConfiguration {
/**
* 包名
*/
private String packageName;
/**
* 生成文件路径
*/
private String path;
/**
* 是否使用lombok
*/
private boolean useLombok;
/**
* 数据源对象
*/
private DataSource dataSource;
/**
* 生成配置
*/
private ProcessConfig processConfig;
/**
* 命名策略
*/
private NameStrategy nameStrategy;
/**
* 自定义类型转换
*/
private Map<String, Class> customType;
/**
* builder
* @return {@link Builder}
*/
public static Builder builder() {
return new Builder();
}
public static class Builder {
private PojoConfiguration configuration;
public Builder() {
this.configuration = new PojoConfiguration();
}
public Builder packageName(String packageName) {
this.configuration.setPackageName(packageName);
return this;
}
public Builder path(String path) {
this.configuration.setPath(path);
return this;
}
public Builder useLombok(boolean useLombok) {
this.configuration.setUseLombok(useLombok);
return this;
}
public Builder dataSource(DataSource dataSource) {
this.configuration.setDataSource(dataSource);
return this;
}
public Builder processConfig(ProcessConfig processConfig) {
this.configuration.setProcessConfig(processConfig);
return this;
}
public Builder nameStrategy(NameStrategy strategy) {
this.configuration.setNameStrategy(strategy);
return this;
}
public Builder customType(String type, Class clazz) {
if (configuration.getCustomType() == null) {
configuration.setCustomType(new HashMap<>());
}
configuration.getCustomType().put(type, clazz);
return this;
}
public PojoConfiguration build() {
return this.configuration;
}
}
}

View File

@@ -0,0 +1,79 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.dialect;
import java.util.Map;
/**
* 根据获取的数据库字段类型决定使用java对象类型
*
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-17
*/
public interface TypeDialect {
Class getClassTypeByFieldType(String type);
/**
* 根据提供的map查询对应的java类型
*
* @param map 提供的map不可为null
* @param type 提供的类型
* @return 查找类型可能null
*/
default Class getTypeByMap(Map<String, Class> map, String type) {
if (type == null || map == null || map.size() == 0)
return null;
if (type.startsWith("date"))
return map.get("date");
if (type.startsWith("mediumint"))
return map.get("mediumint");
if (type.startsWith("double"))
return map.get("double");
if (type.startsWith("varchar"))
return map.get("varchar");
if (type.startsWith("tinyint"))
return map.get("tinyint");
if (type.startsWith("bit"))
return map.get("bit");
if (type.startsWith("float"))
return map.get("float");
if (type.startsWith("int"))
return map.get("int");
if (type.startsWith("smallint"))
return map.get("smallint");
if (type.startsWith("datetime"))
return map.get("datetime");
if (type.startsWith("blob"))
return map.get("blob");
if (type.startsWith("char"))
return map.get("char");
if (type.startsWith("text"))
return map.get("text");
if (type.startsWith("time"))
return map.get("time");
if (type.startsWith("decimal"))
return map.get("decimal");
if (type.startsWith("bigint"))
return map.get("bigint");
if (type.startsWith("timestamp"))
return map.get("timestamp");
return null;
}
}

View File

@@ -0,0 +1,84 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.dialect;
import cn.smallbun.screw.core.exception.ScrewException;
import cn.smallbun.screw.core.query.DatabaseQuery;
import cn.smallbun.screw.core.query.DatabaseType;
import cn.smallbun.screw.core.util.ExceptionUtils;
import cn.smallbun.screw.core.util.JdbcUtils;
import cn.smallbun.screw.extension.pojo.dialect.mysql.MysqlTypeDialect;
import lombok.Getter;
import javax.sql.DataSource;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
* 类型转换工厂
*
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-17
*/
public class TypeDialectFactory {
/**
* DataSource
*/
@Getter
private DataSource dataSource;
private Map<DatabaseType, Class<? extends TypeDialect>> dialectMap;
{
dialectMap = new HashMap<>();
dialectMap.put(DatabaseType.MYSQL, MysqlTypeDialect.class);
}
public TypeDialectFactory(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* 获取配置的数据库类型实例
*
* @return {@link DatabaseQuery} 数据库查询对象
*/
public TypeDialect newInstance() {
try {
//获取数据库URL 用于判断数据库类型
String url = this.getDataSource().getConnection().getMetaData().getURL();
//获取实现类
Class<? extends TypeDialect> query = dialectMap.get(JdbcUtils.getDbType(url));
//判断是否已经实现实现
if (query == null)
throw new ScrewException("this database has not support url:" + url);
//获取有参构造
Constructor<? extends TypeDialect> constructor = query.getConstructor();
//实例化
return constructor.newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException
| InvocationTargetException | SQLException e) {
throw ExceptionUtils.mpe(e);
}
}
}

View File

@@ -0,0 +1,66 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.dialect.mysql;
import cn.smallbun.screw.extension.pojo.dialect.TypeDialect;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;
/**
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-17
*/
public class MysqlTypeDialect implements TypeDialect {
private Map<String, Class> map = new HashMap<>();
public MysqlTypeDialect() {
map.put("varchar", String.class);
map.put("char", String.class);
map.put("blob", Byte[].class);
map.put("text", String.class);
map.put("int", Integer.class);
map.put("tinyint", Integer.class);
map.put("smallint", Integer.class);
map.put("mediumint", Integer.class);
map.put("bit", Boolean.class);
map.put("bigint", Long.class);
map.put("float", Float.class);
map.put("double", Double.class);
map.put("decimal", BigDecimal.class);
map.put("date", LocalDate.class);
map.put("time", LocalTime.class);
map.put("datetime", LocalDateTime.class);
map.put("timestamp", LocalDateTime.class);
}
@Override
public Class getClassTypeByFieldType(String type) {
Class clazz = getTypeByMap(map, type);
if (clazz == null) {
clazz = Object.class;
}
return clazz;
}
}

View File

@@ -0,0 +1,23 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* MySQL Database query implement
* @author SanLi
* Created by qinggang.zuo@gmail.com / 2689170096@qq.com on 2020/6/19 16:02
*/
package cn.smallbun.screw.extension.pojo.dialect.mysql;

View File

@@ -0,0 +1,31 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.engine;
import cn.smallbun.screw.core.exception.ProduceException;
import cn.smallbun.screw.extension.pojo.metadata.model.PojoModel;
/**
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-14
*/
public interface PojoEngine {
void produce(PojoModel info, String docName) throws ProduceException;
}

View File

@@ -0,0 +1,72 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.engine.freemark;
import cn.smallbun.screw.core.exception.ProduceException;
import cn.smallbun.screw.core.exception.ScrewException;
import cn.smallbun.screw.extension.pojo.engine.PojoEngine;
import cn.smallbun.screw.extension.pojo.metadata.model.PojoModel;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.*;
import static cn.smallbun.screw.core.constant.DefaultConstants.DEFAULT_ENCODING;
/**
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-14
*/
public class FreeMarkerPojoEngine implements PojoEngine {
private final Configuration configuration = new Configuration(
Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
{
configuration
.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "/template/freemarker/"));
}
@Override
public void produce(PojoModel info, String docName) throws ProduceException {
try {
Template template = configuration.getTemplate("pojo_java.ftl");
// create file
File file = new File(docName);
if (file.exists()) {
throw new ScrewException("file need to generate has been exist! path:" + docName);
}
// writer freemarker
try (Writer out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file), DEFAULT_ENCODING))) {
// process
template.process(info, out);
// open the output directory
} catch (TemplateException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,23 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Document generation engine freemark implement
* @author SanLi
* Created by qinggang.zuo@gmail.com / 2689170096@qq.com on 2020/6/19 15:58
*/
package cn.smallbun.screw.extension.pojo.engine.freemark;

View File

@@ -0,0 +1,111 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.execute;
import cn.smallbun.screw.core.exception.ScrewException;
import cn.smallbun.screw.core.execute.Execute;
import cn.smallbun.screw.core.util.ExceptionUtils;
import cn.smallbun.screw.core.util.StringUtils;
import cn.smallbun.screw.extension.pojo.PojoConfiguration;
import cn.smallbun.screw.extension.pojo.engine.PojoEngine;
import cn.smallbun.screw.extension.pojo.engine.freemark.FreeMarkerPojoEngine;
import cn.smallbun.screw.extension.pojo.metadata.model.PojoModel;
import cn.smallbun.screw.extension.pojo.process.PojoModelProcess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* java bean生成
*
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-14
*/
public class PojoExecute implements Execute {
/**
* LOGGER
*/
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private PojoConfiguration configuration;
public PojoExecute(PojoConfiguration config) {
String validate = validate(config);
if (StringUtils.isNotBlank(validate)) {
throw new ScrewException(validate);
}
this.configuration = config;
}
@Override
public void execute() {
try {
long start = System.currentTimeMillis();
//获取数据
List<PojoModel> process = new PojoModelProcess(configuration).getPojoModel();
//新建模板引擎
PojoEngine pojoEngine = new FreeMarkerPojoEngine();
//确认路径
String path = configuration.getPath();
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.endsWith("/")) {
path = path + "/";
}
//逐个产生文档
for (PojoModel pojoModel : process) {
pojoEngine.produce(pojoModel, path + pojoModel.getClassName() + ".java");
}
logger.debug("pojo generation complete time consuming:{}ms",
System.currentTimeMillis() - start);
} catch (Exception e) {
throw ExceptionUtils.mpe(e);
}
}
private String validate(PojoConfiguration config) {
StringBuilder error = new StringBuilder();
String separator = System.lineSeparator();
if (config == null) {
error.append(separator);
error.append("config can't be null!");
return error.toString();
}
if (StringUtils.isBlank(config.getPackageName())) {
error.append(separator);
error.append("package can't be null!");
}
if (StringUtils.isBlank(config.getPath())) {
error.append(separator);
error.append("path can't be null!");
}
if (config.getDataSource() == null) {
error.append(separator);
error.append("datasource can't be null!");
}
if (config.getNameStrategy() == null) {
error.append(separator);
error.append("name strategy can't be null!");
}
return error.toString();
}
}

View File

@@ -0,0 +1,69 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.metadata.model;
import lombok.Data;
import java.util.List;
import java.util.Set;
/**
* PojoModel 定义一个pojo需要的一些属性
*
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-14
*/
@Data
public class PojoModel {
/**
* 包名
*/
private String packageName;
/**
* 类名
*/
private String className;
/**
* 数据库表名
*/
private String tableName;
/**
* 数据库注释
*/
private String remarks;
/**
* 是否使用lombok
*/
private boolean useLombok;
/**
* 需要import的类
*/
private Set<String> importList;
/**
* 对象的字段属性
*/
private List<TypeModel> fieldList;
}

View File

@@ -0,0 +1,66 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.metadata.model;
import lombok.Data;
/**
* TypeModel
*
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-14
*/
@Data
public class TypeModel {
/**
* 数据库字段类型
*/
private String fieldType;
/**
* java对象字段类型
*/
private String classType;
/**
* 数据库字段名
*/
private String fieldName;
/**
* java对象字段名
*/
private String className;
/**
* 数据库字段注释
*/
private String remarks;
/**
* get方法名
*/
private String getName;
/**
* set方法名
*/
private String setName;
}

View File

@@ -0,0 +1,23 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Metadata model
* @author SanLi
* Created by qinggang.zuo@gmail.com / 2689170096@qq.com on 2020/6/19 15:59
*/
package cn.smallbun.screw.extension.pojo.metadata.model;

View File

@@ -0,0 +1,23 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Metadata
* @author SanLi
* Created by qinggang.zuo@gmail.com / 2689170096@qq.com on 2020/6/19 15:28
*/
package cn.smallbun.screw.extension.pojo.metadata;

View File

@@ -0,0 +1,134 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.process;
import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.metadata.model.ColumnModel;
import cn.smallbun.screw.core.metadata.model.DataModel;
import cn.smallbun.screw.core.metadata.model.TableModel;
import cn.smallbun.screw.core.process.DataModelProcess;
import cn.smallbun.screw.extension.pojo.PojoConfiguration;
import cn.smallbun.screw.extension.pojo.dialect.TypeDialect;
import cn.smallbun.screw.extension.pojo.dialect.TypeDialectFactory;
import cn.smallbun.screw.extension.pojo.metadata.model.PojoModel;
import cn.smallbun.screw.extension.pojo.metadata.model.TypeModel;
import cn.smallbun.screw.extension.pojo.strategy.NameStrategy;
import java.util.*;
/**
* 用于生成pojo的数据
*
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-14
*/
public class PojoModelProcess implements PojoProcess {
private PojoConfiguration pojoConfiguration;
/**
* 构造方法
*
* @param configuration {@link Configuration}
*/
public PojoModelProcess(PojoConfiguration configuration) {
this.pojoConfiguration = configuration;
}
@Override
public List<PojoModel> getPojoModel() {
//获取数据库元数据
List<TableModel> tableModels = getTableModel(pojoConfiguration);
//新建list准备接收结果集
List<PojoModel> pojoModels = new ArrayList<>(tableModels.size());
//获取类型转换器
TypeDialect dialect = new TypeDialectFactory(pojoConfiguration.getDataSource())
.newInstance();
//获取命名策略,用于转换表至对象,列至字段的名称策略
NameStrategy nameStrategy = pojoConfiguration.getNameStrategy();
//获取用户自定义的类型转换
Map<String, Class> customType = pojoConfiguration.getCustomType();
//进行数据转换
for (TableModel model : tableModels) {
PojoModel pojoModel = new PojoModel();
pojoModel.setUseLombok(pojoConfiguration.isUseLombok());
pojoModel.setPackageName(pojoConfiguration.getPackageName());
pojoModel.setTableName(model.getTableName());
pojoModel.setRemarks(model.getRemarks());
pojoModel.setClassName(nameStrategy.transClassName(model.getTableName()));
//需要import的属性
Set<String> importList = new HashSet<>();
List<TypeModel> fieldList = new ArrayList<>();
for (ColumnModel column : model.getColumns()) {
TypeModel typeModel = new TypeModel();
typeModel.setFieldName(column.getColumnName());
typeModel.setFieldType(column.getTypeName());
typeModel.setRemarks(column.getRemarks());
//先判断用户是否自定义
Class classType = dialect.getTypeByMap(customType, column.getTypeName());
if (classType == null) {
classType = dialect.getClassTypeByFieldType(column.getTypeName());
}
//如果对象不在java.lang包下需要import
if (!classType.getTypeName().startsWith("java.lang")) {
importList.add(classType.getTypeName());
}
typeModel
.setClassName(nameStrategy.transFieldName(column.getColumnName(), classType));
typeModel.setClassType(classType.getSimpleName());
//如果不使用lombok需要生成getset方法
if (!pojoConfiguration.isUseLombok()) {
typeModel
.setGetName(nameStrategy.transGetName(column.getColumnName(), classType));
typeModel
.setSetName(nameStrategy.transSetName(column.getColumnName(), classType));
}
fieldList.add(typeModel);
}
pojoModel.setImportList(importList);
pojoModel.setFieldList(fieldList);
pojoModels.add(pojoModel);
}
return pojoModels;
}
private List<TableModel> getTableModel(PojoConfiguration pojoConfiguration) {
//配置
Configuration config = Configuration.builder().dataSource(pojoConfiguration.getDataSource())
.produceConfig(pojoConfiguration.getProcessConfig())
.engineConfig(EngineConfig.builder().fileType(EngineFileType.HTML).build()).build();
DataModel process = new DataModelProcess(config).process();
return process.getTables();
}
}

View File

@@ -0,0 +1,34 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.process;
import cn.smallbun.screw.extension.pojo.metadata.model.PojoModel;
import java.util.List;
/**
* 继承者自定义实现获取PojoModel
*
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-20
*/
public interface PojoProcess {
List<PojoModel> getPojoModel();
}

View File

@@ -0,0 +1,23 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Data processing
* @author SanLi
* Created by qinggang.zuo@gmail.com / 2689170096@qq.com on 2020/6/19 15:28
*/
package cn.smallbun.screw.extension.pojo.process;

View File

@@ -0,0 +1,83 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.strategy;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 采用驼峰命名策略(推荐数据库字段是下划线命名的情况下使用)
*
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-17
*/
public class HumpNameStrategy implements NameStrategy {
private Pattern linePattern = Pattern.compile("_(\\w)");
@Override
public String transClassName(String name) {
return upperCase(lineToHump(name));
}
@Override
public String transFieldName(String name, Class type) {
return lineToHump(name);
}
@Override
public String transSetName(String name, Class type) {
return "set" + upperCase(lineToHump(name));
}
@Override
public String transGetName(String name, Class type) {
if (Boolean.class.isAssignableFrom(type)) {
return "is" + upperCase(lineToHump(name));
}
return "get" + upperCase(lineToHump(name));
}
/**
* 下划线转驼峰
*/
private String lineToHump(String str) {
Matcher matcher = linePattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* 首字母转大写
*
* @param str
* @return
*/
private String upperCase(String str) {
char[] ch = str.toCharArray();
if (ch[0] >= 'a' && ch[0] <= 'z') {
ch[0] = (char) (ch[0] - 32);
}
return new String(ch);
}
}

View File

@@ -0,0 +1,60 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.strategy;
/**
* 命名策略,用于转换表名和字段名
*
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-17
*/
public interface NameStrategy {
/**
* 转换表名
*
* @param name 输入的表名
* @return 输出转换后的名字
*/
String transClassName(String name);
/**
* 转换字段名
*
* @param name 输入的字段名
* @return 输出转换后的名字
*/
String transFieldName(String name, Class type);
/**
* 转换set方法名
*
* @param name 输入的字段名
* @return 输出的set方法名
*/
String transSetName(String name, Class type);
/**
* 转换get方法名
*
* @param name 输入的字段名
* @return 输出的方法名
*/
String transGetName(String name, Class type);
}

View File

@@ -0,0 +1,48 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cn.smallbun.screw.extension.pojo.strategy;
/**
* 没有任何操作的命名策略
*
* @author liu·yu
* Created by 15952866402@163.com on 2020-08-17
*/
public class NoOpNameStrategy implements NameStrategy {
@Override
public String transClassName(String name) {
return name;
}
@Override
public String transFieldName(String name, Class type) {
return name;
}
@Override
public String transSetName(String name, Class type) {
return "set" + name;
}
@Override
public String transGetName(String name, Class type) {
return "get" + name;
}
}

View File

@@ -0,0 +1,23 @@
/*
* screw-extension - 简洁好用的数据库表结构文档生成工具
* Copyright © 2020 SanLi (qinggang.zuo@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* 命名策略
* @author SanLi
* Created by qinggang.zuo@gmail.com / 2689170096@qq.com on 2020/6/19 15:27
*/
package cn.smallbun.screw.extension.pojo.strategy;

View File

@@ -0,0 +1,49 @@
package ${packageName};
<#if importList?? && (importList?size > 0) >
<#list importList>
<#items as import>
import ${import};
</#items>
</#list>
</#if>
<#if useLombok>
import lombok.Data;
</#if>
/**
* 数据库表名: ${tableName}
<#if (remarks)??&&remarks?length gt 0>
* 数据库表注释: ${remarks}
</#if>
*/
<#if useLombok>
@Data
</#if>
public class ${className} {
<#list fieldList>
<#items as field>
<#if (field.remarks)??&&field.remarks?length gt 0>
//数据库字段注释: ${field.remarks}
</#if>
//数据库字段类型: ${field.fieldType},数据库字段名: ${field.fieldName}
private ${field.classType} ${field.className};
</#items>
</#list>
<#if !useLombok>
<#list fieldList>
<#items as field>
public ${field.classType} ${field.getName}(){
return this.${field.className};
}
public void ${field.setName}(${field.classType} ${field.className}){
this.${field.className} = ${field.className};
}
</#items>
</#list>
</#if>
}