处理生成mapper出现的问题

处理中文乱码问题
This commit is contained in:
WenG 2021-04-04 11:35:32 +08:00
parent 4bc34ef94c
commit 0b79f47cb2
6 changed files with 109 additions and 87 deletions

View File

@ -19,7 +19,6 @@ import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;
@ -103,6 +102,7 @@ public class GenerateController implements Initializable {
this.generateService = new GenerateService(fieldTableView);
this.fieldService.setTableView(fieldTableView);
this.fieldService.setTableName(tableVO.getTableName());
this.fieldService.setTableNamePrefix(this.tablePrefixTextField.getText());
this.fieldService.showField();
}

View File

@ -1,12 +1,9 @@
package ink.wgink.code.factory.factory;
import com.sun.deploy.uitoolkit.impl.fx.ui.FXUIFactory;
import ink.wgink.code.factory.service.FieldService;
import ink.wgink.code.factory.vos.FieldVO;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;
@ -37,9 +34,32 @@ public class CheckBoxTableCellFactory implements Callback<TableColumn.CellDataFe
public ObservableValue<CheckBox> call(TableColumn.CellDataFeatures<FieldVO, String> param) {
FieldVO fieldVO = param.getValue();
CheckBox checkBox = new CheckBox();
String tableName = param.getValue().getTableName();
String tableNamePrefix = param.getValue().getTableNamePrefix();
if (StringUtils.equals(IS_FORM_SHOW, property)) {
Boolean isFormShow = param.getValue().getFormShow();
checkBox.setSelected(isFormShow == null || !isFormShow ? false : true);
// 如果是主键
if (StringUtils.equals(String.format("%s_id", tableName.replace(tableNamePrefix, "")), param.getValue().getColumnName())) {
param.getValue().setFormShow(false);
checkBox.setSelected(false);
} else if(StringUtils.equals(FieldService.DEFAULT_COLUMN_CREATOR, param.getValue().getColumnName())) {
param.getValue().setFormShow(false);
checkBox.setSelected(false);
} else if(StringUtils.equals(FieldService.DEFAULT_COLUMN_GMT_CREATE, param.getValue().getColumnName())) {
param.getValue().setFormShow(false);
checkBox.setSelected(false);
} else if(StringUtils.equals(FieldService.DEFAULT_COLUMN_MODIFIER, param.getValue().getColumnName())) {
param.getValue().setFormShow(false);
checkBox.setSelected(false);
} else if(StringUtils.equals(FieldService.DEFAULT_COLUMN_GMT_MODIFIED, param.getValue().getColumnName())) {
param.getValue().setFormShow(false);
checkBox.setSelected(false);
} else if(StringUtils.equals(FieldService.DEFAULT_COLUMN_IS_DELETE, param.getValue().getColumnName())) {
param.getValue().setFormShow(false);
checkBox.setSelected(false);
} else {
checkBox.setSelected(isFormShow == null || !isFormShow ? false : true);
}
}
if (StringUtils.equals(IS_LIST_SHOW, property)) {
Boolean isListShow = param.getValue().getListShow();

View File

@ -47,6 +47,7 @@ public class FieldService {
private ObservableList<FieldVO> fields = FXCollections.observableArrayList();
private TableView tableView;
private String tableName;
private String tableNamePrefix;
private Stage fieldStage;
/**
@ -114,6 +115,8 @@ public class FieldService {
continue;
}
FieldVO fieldVO = new FieldVO();
fieldVO.setTableName(tableName);
fieldVO.setTableNamePrefix(tableNamePrefix);
fieldVO.setRowNumber(fieldIndex);
fieldVO.setColumnName(columnName);
fieldVO.setColumnType(field.get("COLUMN_TYPE").toString());
@ -170,6 +173,14 @@ public class FieldService {
this.tableName = tableName;
}
public String getTableNamePrefix() {
return tableNamePrefix == null ? "" : tableNamePrefix.trim();
}
public void setTableNamePrefix(String tableNamePrefix) {
this.tableNamePrefix = tableNamePrefix;
}
public void removeField(String columnName) {
new Service<Integer>() {

View File

@ -1,6 +1,5 @@
package ink.wgink.code.factory.service;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
@ -18,10 +17,7 @@ import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.io.*;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
@ -41,6 +37,7 @@ import java.util.Map;
@Slf4j
public class GenerateService {
public static final String UTF_8 = "UTF-8";
private Stage fieldStage;
private TableView tableView;
Configuration freemarkerConfiguration = new Configuration(Configuration.getVersion());
@ -53,7 +50,7 @@ public class GenerateService {
// this.freemarkerConfiguration.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "/template"));
// 3.加载 jar中的模板
this.freemarkerConfiguration.setClassForTemplateLoading(this.getClass(), "/template");
this.freemarkerConfiguration.setDefaultEncoding("utf-8");
this.freemarkerConfiguration.setDefaultEncoding(UTF_8);
}
public void generateCode(TableVO tableVO, GenerateVO generateVO) throws IOException, TemplateException {
@ -68,6 +65,7 @@ public class GenerateService {
String tableFullName = tableVO.getTableName();
String tableNameWithoutPrefix = tableFullName.replaceFirst(generateVO.getTablePrefix(), "");
String tableName = WStringUtil.underLine2LowerUpper(tableNameWithoutPrefix);
String underlineTableName = WStringUtil.lowerUpper2UnderLine(tableName);
// 首字母大写驼峰表名
String firstUpperTableName = WStringUtil.firstToUpper(tableName);
// 首字母小写驼峰表明
@ -82,6 +80,7 @@ public class GenerateService {
dataModel.put("tableFullName", tableFullName);
dataModel.put("firstUpperTableName", firstUpperTableName);
dataModel.put("firstLowerTableName", firstLowerTableName);
dataModel.put("underlineTableName", underlineTableName);
dataModel.put("tableExplain", tableVO.getTableComment());
dataModel.put("author", "CodeFactory");
dataModel.put("date", DateUtil.getTime());
@ -132,16 +131,22 @@ public class GenerateService {
*/
private void initFieldList(String tableNameWithoutPrefix, Map<String, Object> dataModel, ObservableList<FieldVO> fields) {
List<Map<String, Object>> fieldList = new ArrayList<>();
String idColumn = null;
boolean hasId = false;
boolean idFormShow = false;
boolean hasGmtCreate = false;
boolean hasCreator = false;
boolean hasGmtModified = false;
boolean gmtModifiedFormShow = false;
boolean hasModifier = false;
boolean modifierFormShow = false;
boolean hasIsDelete = false;
for (int i = 0; i < fields.size(); i++) {
FieldVO field = fields.get(i);
if (!hasId && StringUtils.equals(String.format("%s_id", tableNameWithoutPrefix), field.getColumnName())) {
hasId = true;
idColumn = field.getColumnName();
idFormShow = field.getFormShow();
}
if (!hasGmtCreate && StringUtils.equals(FieldService.DEFAULT_FIELD_GMT_CREATE, field.getPropertyName())) {
hasGmtCreate = true;
@ -151,9 +156,11 @@ public class GenerateService {
}
if (!hasGmtModified && StringUtils.equals(FieldService.DEFAULT_FIELD_GMT_MODIFIED, field.getPropertyName())) {
hasGmtModified = true;
gmtModifiedFormShow = field.getFormShow();
}
if (!hasModifier && StringUtils.equals(FieldService.DEFAULT_FIELD_MODIFIER, field.getPropertyName())) {
hasModifier = true;
modifierFormShow = field.getFormShow();
}
if (!hasIsDelete && StringUtils.equals(FieldService.DEFAULT_FIELD_IS_DELETE, field.getPropertyName())) {
hasIsDelete = true;
@ -176,11 +183,15 @@ public class GenerateService {
fieldMap.put("fieldSplit", fieldSplit);
fieldList.add(fieldMap);
}
dataModel.put("idColumn", idColumn);
dataModel.put("hasId", hasId);
dataModel.put("idFormShow", idFormShow);
dataModel.put("hasGmtCreate", hasGmtCreate);
dataModel.put("hasCreator", hasCreator);
dataModel.put("hasGmtModified", hasGmtModified);
dataModel.put("gmtModifiedFormShow", gmtModifiedFormShow);
dataModel.put("hasModifier", hasModifier);
dataModel.put("modifierFormShow", modifierFormShow);
dataModel.put("hasIsDelete", hasIsDelete);
dataModel.put("fieldList", fieldList);
}
@ -308,7 +319,7 @@ public class GenerateService {
if (!folder.exists()) {
folder.mkdirs();
}
code(templateFtl, String.format("%s/%s-mapper.xml", outFolder, tableName), dataModel);
code(templateFtl, String.format("%s/%s-mapper.xml", outFolder, WStringUtil.lowerUpper2UnderLine(tableName).replaceAll("\\_", "-")), dataModel);
}
/**
@ -455,8 +466,8 @@ public class GenerateService {
* @throws TemplateException
*/
private void code(String templateFtl, String outFile, Map<String, Object> dataModel) throws IOException, TemplateException {
Template template = freemarkerConfiguration.getTemplate(templateFtl);
Writer out = new FileWriter(outFile);
Template template = freemarkerConfiguration.getTemplate(templateFtl, UTF_8);
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), UTF_8));
template.process(dataModel, out);
out.close();
}

View File

@ -32,6 +32,8 @@ public class FieldVO {
private Boolean isFormShow;
private Boolean isListShow;
private Boolean isNotNull;
private String tableName;
private String tableNamePrefix;
public FieldVO() {
this.rowNumber = new SimpleIntegerProperty(0);
@ -212,44 +214,20 @@ public class FieldVO {
isNotNull = notNull;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"rowNumber\":")
.append(rowNumber);
sb.append(",\"columnName\":")
.append(columnName);
sb.append(",\"columnComment\":")
.append(columnComment);
sb.append(",\"columnDefault\":")
.append(columnDefault);
sb.append(",\"dataType\":\"")
.append(dataType).append('\"');
sb.append(",\"columnType\":")
.append(columnType);
sb.append(",\"characterMaximum\":")
.append(characterMaximum);
sb.append(",\"numericPrecision\":")
.append(numericPrecision);
sb.append(",\"numericScale\":")
.append(numericScale);
sb.append(",\"propertyName\":")
.append(propertyName);
sb.append(",\"propertyType\":")
.append(propertyType);
sb.append(",\"propertyLength\":")
.append(propertyLength);
sb.append(",\"formFieldType\":\"")
.append(formFieldType).append('\"');
sb.append(",\"formFieldValue\":\"")
.append(formFieldValue).append('\"');
sb.append(",\"isFormShow\":")
.append(isFormShow);
sb.append(",\"isListShow\":")
.append(isListShow);
sb.append(",\"isNotNull\":")
.append(isNotNull);
sb.append('}');
return sb.toString();
public String getTableName() {
return tableName == null ? "" : tableName.trim();
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getTableNamePrefix() {
return tableNamePrefix == null ? "" : tableNamePrefix.trim();
}
public void setTableNamePrefix(String tableNamePrefix) {
this.tableNamePrefix = tableNamePrefix;
}
}

View File

@ -50,7 +50,7 @@
is_delete = 1
WHERE
<#if hasId>
${tableName}_id IN
${underlineTableName}_id IN
<foreach collection="${firstLowerTableName}Ids" index="index" open="(" separator="," close=")">
${r"#{"}${firstLowerTableName}${r"Ids[${index}]}"}
</foreach>
@ -66,7 +66,7 @@
${tableFullName}
WHERE
<#if hasId>
${tableName}_id IN
${idColumn} IN
<foreach collection="${firstLowerTableName}Ids" index="index" open="(" separator="," close=")">
${r"#{"}${firstLowerTableName}${r"Ids[${index}]}"}
</foreach>
@ -81,36 +81,38 @@
${tableFullName}
SET
<#list fieldList! as field>
<#if field.listShow>
<#if field.formFieldValue == "number" || field.formFieldValue == "double">
<if test="${field.propertyName} != null">
<#if field.formShow>
<#if field.formFieldValue == "number" || field.formFieldValue == "double">
<if test="${field.propertyName} != null">
${field.columnName} = ${r"#{"}${field.propertyName}${r"}"},
</if>
<#elseif field.formFieldValue == "radio" || field.formFieldValue == "checkbox" || field.formFieldValue == "select">
<if test="${field.propertyName} != null">
</if>
<#elseif field.formFieldValue == "radio" || field.formFieldValue == "checkbox" || field.formFieldValue == "select">
<if test="${field.propertyName} != null">
${field.columnName} = ${r"#{"}${field.propertyName}${r"}"},w
</if>
<#else>
<#if field.columnName != idColumn>
<if test="${field.propertyName} != null and ${field.propertyName} != ''">
${field.columnName} = ${r"#{"}${field.propertyName}${r"}"},
</if>
<#else>
<if test="${field.propertyName} != null and ${field.propertyName} != ''">
${field.columnName} = ${r"#{"}${field.propertyName}${r"}"},
</if>
</#if>
<#if hasGmtModified>
gmt_modified = ${r"#{gmtModified}"},
</#if>
<#if hasModifier>
modifier = ${r"#{modifier}"},
</#if>
</#if>
<#if hasId>
${tableName}_id = ${tableName}_id
<#else>
<!-- 填充条件 -->
</if>
</#if>
</#if>
</#if>
</#list>
<#if hasGmtModified && !gmtModifiedFormShow>
gmt_modified = ${r"#{gmtModified}"},
</#if>
<#if hasModifier && !modifierFormShow>
modifier = ${r"#{modifier}"},
</#if>
<#if hasId>
${idColumn} = ${idColumn}
<#else>
<!-- 填充条件 -->
</#if>
WHERE
<#if hasId>
${tableName}_id = ${r"#{"}${firstLowerTableName}${r"Id}"}
${idColumn} = ${r"#{"}${firstLowerTableName}${r"Id}"}
<#else>
<!-- 添加条件 -->
</#if>
@ -125,7 +127,7 @@
</#if>
</#list>
<#if hasId>
t1.${tableName}_id
t1.${idColumn}
<#else>
1
</#if>
@ -140,7 +142,7 @@
<#if hasId>
<if test="${firstLowerTableName}Id != null and ${firstLowerTableName}Id != ''">
AND
t1.${tableName}_id = ${r"#{"}${firstLowerTableName}${r"Id}"}
t1.${idColumn} = ${r"#{"}${firstLowerTableName}${r"Id}"}
</if>
<#else>
<!-- 添加条件 -->
@ -164,7 +166,7 @@
<#if hasId>
<if test="${firstLowerTableName}Id != null and ${firstLowerTableName}Id != ''">
AND
t1.${tableName}_id = ${r"#{"}${firstLowerTableName}${r"Id}"}
t1.${idColumn} = ${r"#{"}${firstLowerTableName}${r"Id}"}
</if>
<#else>
<!-- 添加条件 -->
@ -188,7 +190,7 @@
<#if hasId>
<if test="${firstLowerTableName}Id != null and ${firstLowerTableName}Id != ''">
AND
t1.${tableName}_id = ${r"#{"}${firstLowerTableName}${r"Id}"}
t1.${idColumn} = ${r"#{"}${firstLowerTableName}${r"Id}"}
</if>
<#else>
<!-- 添加条件 -->
@ -231,7 +233,7 @@
<#if hasId>
<if test="${firstLowerTableName}Ids != null and ${firstLowerTableName}Ids.size > 0">
AND
t1.${tableName}_id IN
t1.${idColumn} IN
<foreach collection="${firstLowerTableName}Ids" index="index" open="(" separator="," close=")">
${r"#{"}${firstLowerTableName}${r"Ids[${index}]}"}
</foreach>
@ -272,7 +274,7 @@
<#if hasId>
<if test="${firstLowerTableName}Ids != null and ${firstLowerTableName}Ids.size > 0">
AND
t1.${tableName}_id IN
t1.${idColumn} IN
<foreach collection="${firstLowerTableName}Ids" index="index" open="(" separator="," close=")">
${r"#{"}${firstLowerTableName}${r"Ids[${index}]}"}
</foreach>
@ -313,7 +315,7 @@
<#if hasId>
<if test="${firstLowerTableName}Ids != null and ${firstLowerTableName}Ids.size > 0">
AND
t1.${tableName}_id IN
t1.${idColumn} IN
<foreach collection="${firstLowerTableName}Ids" index="index" open="(" separator="," close=")">
${r"#{"}${firstLowerTableName}${r"Ids[${index}]}"}
</foreach>