自动生成代码工具 模板工具类
自动生成代码工具 模板工具类import java.io.BufferedWriter;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.util.ArrayList;i...
·
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ijwt.exception.JavaTypeException;
import com.ijwt.exception.MybatisTypeException;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
/**
* @author InJavaWeTrust
*/
public enum UtilFreemarker {
INSTANCE;
private static Logger log = LoggerFactory.getLogger(UtilFreemarker.class);
/**
* 获取java属性</br>
* @param tableName 表名
* @return java属性list
*/
public List<String> getJavaPropertyList(String tableName) {
List<String> javaPropertyList = new ArrayList<>();
List<String> columnNameList = UtilDB.getColumnNameByTableName(tableName);
columnNameList.forEach(columnName -> javaPropertyList.add(UtilString.INSTANCE.columnNameToJavaName(columnName)));
return javaPropertyList;
}
/**
* 获取表中列
* @param tableName 表名
* @return list
*/
public List<String> getColumnNameForMybatis(String tableName) {
return UtilDB.getColumnNameByTableName(tableName);
}
/**
* 得到表中列字段长度最长的列字段
* @param tableName 表名
* @return 最长的列字段
*/
public int getMaxColumnLength(String tableName){
List<String> columnList = getColumnNameForMybatis(tableName);
Optional<Integer> opt = columnList.stream().map(String::length).max(Integer::compareTo);
int maxLen = 0;
if (opt.isPresent()) {
maxLen = opt.get();
}
return maxLen;
}
/**
* 得到Java属性字段长度最长的字段长度
* @param tableName 表名
* @return Java属性最长字段长度
*/
public int getMaxJavaPropertyLength(String tableName){
List<String> propertyList = getJavaPropertyList(tableName);
Optional<Integer> opt = propertyList.stream().map(String::length).max(Integer::compareTo);
int maxLen = 0;
if (opt.isPresent()) {
maxLen = opt.get();
}
return maxLen;
}
/**
* 获取表中列注释
* @param tableName 表名
* @return list
*/
public List<String> getRemarks(String tableName) {
return UtilDB.getColumnCommentByTableName(tableName);
}
/**
* 获取表中列类型并转换成java类型
* @param tableName 表名
* @return list
*/
public List<String> getColumnType(String tableName) {
List<String> retTypes = new ArrayList<>();
List<String> typeList = UtilDB.getColumnTypeByTableName(tableName);
typeList.forEach(type -> {
try {
retTypes.add(UtilString.INSTANCE.columnTypeToJavaType(type));
} catch (JavaTypeException e) {
log.error(e.getMessage());
}
});
return retTypes;
}
/**
* 获取mybatis类型
* @param tableName 表名
* @return list
*/
public List<String> getMybatisType(String tableName) {
List<String> retTypes = new ArrayList<>();
List<String> typeList = UtilDB.getColumnTypeByTableName(tableName);
typeList.forEach(type -> {
try {
retTypes.add(UtilString.INSTANCE.mySqlTypeToJdbcType(type));
} catch (MybatisTypeException e) {
log.error(e.getMessage());
}
});
return retTypes;
}
/**
* 获取title
* @param tableName 表名
* @return
*/
public String getTitle(String tableName) {
return UtilDB.getTableCommentByTableName(tableName);
}
/**
* 获取dataModel
* @param tableName 表名
* @return Map
*/
public Map<Object, Object> getTableInfo(String tableName){
Map<Object, Object> map = new HashMap<>();
map.put("title", getTitle(tableName));
map.put("javaPropertyList", getJavaPropertyList(tableName));
map.put("mybatisColumns", getColumnNameForMybatis(tableName));
map.put("remarks", getRemarks(tableName));
map.put("columnTypes", getColumnType(tableName));
map.put("mybatisTypes", getMybatisType(tableName));
map.put("maxColumnLength", getMaxColumnLength(tableName));
map.put("maxJavaPropertyLength", getMaxJavaPropertyLength(tableName));
return map;
}
public void generateFile(String fileName, String templateName, Map<Object, Object> map){
Configuration config = new Configuration(Configuration.VERSION_2_3_23);
config.setDefaultEncoding("UTF-8");
config.setTemplateLoader(new ClassTemplateLoader(UtilFreemarker.class, "/"));
config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileName), "UTF-8"));) {
Template template = config.getTemplate(templateName);
template.process(map, out);
}catch(Exception e){
log.error(e.getMessage());
}
}
}
更多推荐
所有评论(0)