Commit fbecf4ce authored by hewei's avatar hewei

美化xml输出格式,实现和官方一致80换行,便于阅读

parent f62d78d1
......@@ -23,10 +23,7 @@ import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.api.dom.xml.*;
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
......@@ -53,7 +50,7 @@ public class BatchInsertPlugin extends BasePlugin {
// 插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
&& "com.microsoft.jdbc.sqlserver.SQLServer".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false
&& "com.microsoft.sqlserver.jdbc.SQLServerDriver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false){
&& "com.microsoft.sqlserver.jdbc.SQLServerDriver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false) {
logger.error("itfsw:插件" + this.getClass().getTypeName() + "插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键!");
return false;
}
......@@ -69,11 +66,9 @@ public class BatchInsertPlugin extends BasePlugin {
}
/**
* Java Client Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param interfaze
* @param topLevelClass
* @param introspectedTable
......@@ -97,7 +92,7 @@ public class BatchInsertPlugin extends BasePlugin {
logger.debug("itfsw(批量插入插件):" + interfaze.getType().getShortName() + "增加batchInsert方法。");
// 2. batchInsertSelective
FullyQualifiedJavaType selectiveType = new FullyQualifiedJavaType(introspectedTable.getRules().calculateAllFieldsClass().getShortName()+"."+ModelColumnPlugin.ENUM_NAME);
FullyQualifiedJavaType selectiveType = new FullyQualifiedJavaType(introspectedTable.getRules().calculateAllFieldsClass().getShortName() + "." + ModelColumnPlugin.ENUM_NAME);
Method mBatchInsertSelective = JavaElementGeneratorTools.generateMethod(
METHOD_BATCH_INSERT_SELECTIVE,
JavaVisibility.DEFAULT,
......@@ -116,7 +111,6 @@ public class BatchInsertPlugin extends BasePlugin {
/**
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param document
* @param introspectedTable
* @return
......@@ -135,7 +129,9 @@ public class BatchInsertPlugin extends BasePlugin {
XmlElementGeneratorTools.useGeneratedKeys(batchInsertEle, introspectedTable);
batchInsertEle.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
batchInsertEle.addElement(XmlElementGeneratorTools.generateKeys(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())));
for (Element element : XmlElementGeneratorTools.generateKeys(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()))) {
batchInsertEle.addElement(element);
}
// 添加foreach节点
XmlElement foreachElement = new XmlElement("foreach");
......@@ -143,8 +139,9 @@ public class BatchInsertPlugin extends BasePlugin {
foreachElement.addAttribute(new Attribute("item", "item"));
foreachElement.addAttribute(new Attribute("separator", ","));
foreachElement.addElement(XmlElementGeneratorTools.generateValues(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "item."));
for (Element element : XmlElementGeneratorTools.generateValues(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "item.")) {
foreachElement.addElement(element);
}
// values 构建
batchInsertEle.addElement(new TextElement("values"));
......@@ -165,7 +162,7 @@ public class BatchInsertPlugin extends BasePlugin {
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools.useGeneratedKeys(element, introspectedTable);
element.addElement(new TextElement("insert into "+introspectedTable.getFullyQualifiedTableNameAtRuntime()+" ("));
element.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime() + " ("));
XmlElement foreachInsertColumns = new XmlElement("foreach");
foreachInsertColumns.addAttribute(new Attribute("collection", "selective"));
......@@ -200,7 +197,7 @@ public class BatchInsertPlugin extends BasePlugin {
for (int i = 0; i < columns1.size(); i++) {
IntrospectedColumn introspectedColumn = columns.get(i);
XmlElement check = new XmlElement("if");
check.addAttribute(new Attribute("test", "'"+introspectedColumn.getActualColumnName()+"' == column.value"));
check.addAttribute(new Attribute("test", "'" + introspectedColumn.getActualColumnName() + "' == column.value"));
check.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "item.")));
foreachInsertColumnsCheck.addElement(check);
......
......@@ -22,10 +22,7 @@ import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.api.dom.xml.*;
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.internal.util.StringUtility;
......@@ -61,7 +58,7 @@ public class UpsertPlugin extends BasePlugin {
public boolean validate(List<String> warnings) {
// 插件使用前提是数据库为MySQL
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false){
if ("com.mysql.jdbc.Driver".equalsIgnoreCase(this.getContext().getJdbcConnectionConfiguration().getDriverClass()) == false) {
logger.error("itfsw:插件" + this.getClass().getTypeName() + "插件使用前提是数据库为MySQL!");
return false;
}
......@@ -70,7 +67,7 @@ public class UpsertPlugin extends BasePlugin {
Properties properties = this.getProperties();
String allowMultiQueries = properties.getProperty(PRE_ALLOW_MULTI_QUERIES);
this.allowMultiQueries = allowMultiQueries == null ? false : StringUtility.isTrue(allowMultiQueries);
if (this.allowMultiQueries){
if (this.allowMultiQueries) {
// 提示用户注意信息
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件您开启了allowMultiQueries支持,注意在jdbc url 配置中增加“allowMultiQueries=true”支持(不怎么建议使用该功能,开启多sql提交会增加sql注入的风险,请确保你所有sql都使用MyBatis书写,请不要使用statement进行sql提交)!");
}
......@@ -81,7 +78,6 @@ public class UpsertPlugin extends BasePlugin {
/**
* Java Client Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param interfaze
* @param topLevelClass
* @param introspectedTable
......@@ -113,7 +109,7 @@ public class UpsertPlugin extends BasePlugin {
interfaze.addMethod(mUpsertSelective);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertSelective方法。");
if (this.allowMultiQueries){
if (this.allowMultiQueries) {
// ====================================== 3. upsertByExample ======================================
Method mUpsertByExample = JavaElementGeneratorTools.generateMethod(
METHOD_UPSERT_BY_EXAMPLE,
......@@ -142,7 +138,7 @@ public class UpsertPlugin extends BasePlugin {
}
// !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
if (introspectedTable.hasBLOBColumns()){
if (introspectedTable.hasBLOBColumns()) {
// ====================================== 1. upsertWithBLOBs ======================================
Method mUpsertWithBLOBs = JavaElementGeneratorTools.generateMethod(
METHOD_UPSERT_WITH_BLOBS,
......@@ -167,7 +163,7 @@ public class UpsertPlugin extends BasePlugin {
interfaze.addMethod(mUpsertSelectiveWithBLOBs);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertSelective方法。");
if (this.allowMultiQueries){
if (this.allowMultiQueries) {
// ====================================== 3. upsertByExampleWithBLOBs ======================================
Method mUpsertByExampleWithBLOBs = JavaElementGeneratorTools.generateMethod(
METHOD_UPSERT_BY_EXAMPLE_WITH_BLOBS,
......@@ -202,7 +198,6 @@ public class UpsertPlugin extends BasePlugin {
/**
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param document
* @param introspectedTable
* @return
......@@ -213,7 +208,7 @@ public class UpsertPlugin extends BasePlugin {
this.generateXmlElementWithoutBLOBs(document, introspectedTable);
// !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
if (introspectedTable.hasBLOBColumns()){
if (introspectedTable.hasBLOBColumns()) {
this.generateXmlElementWithBLOBs(document, introspectedTable);
}
......@@ -222,11 +217,10 @@ public class UpsertPlugin extends BasePlugin {
/**
* 当Model有生成WithBLOBs类时的情况
*
* @param document
* @param introspectedTable
*/
private void generateXmlElementWithBLOBs(Document document, IntrospectedTable introspectedTable){
private void generateXmlElementWithBLOBs(Document document, IntrospectedTable introspectedTable) {
// ====================================== 1. upsert ======================================
XmlElement eleUpsertWithBLOBs = new XmlElement("insert");
eleUpsertWithBLOBs.addAttribute(new Attribute("id", METHOD_UPSERT_WITH_BLOBS));
......@@ -241,11 +235,17 @@ public class UpsertPlugin extends BasePlugin {
// insert
eleUpsertWithBLOBs.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
eleUpsertWithBLOBs.addElement(XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns()));
for (Element element : XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns())) {
eleUpsertWithBLOBs.addElement(element);
}
eleUpsertWithBLOBs.addElement(new TextElement("values"));
eleUpsertWithBLOBs.addElement(XmlElementGeneratorTools.generateValues(introspectedTable.getAllColumns()));
for (Element element : XmlElementGeneratorTools.generateValues(introspectedTable.getAllColumns())) {
eleUpsertWithBLOBs.addElement(element);
}
eleUpsertWithBLOBs.addElement(new TextElement("on duplicate key update "));
eleUpsertWithBLOBs.addElement(XmlElementGeneratorTools.generateSets(introspectedTable.getAllColumns()));
for (Element element : XmlElementGeneratorTools.generateSets(introspectedTable.getAllColumns())) {
eleUpsertWithBLOBs.addElement(element);
}
document.getRootElement().addElement(eleUpsertWithBLOBs);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsert实现方法。");
......@@ -273,7 +273,7 @@ public class UpsertPlugin extends BasePlugin {
document.getRootElement().addElement(eleUpsertSelectiveWithBLOBs);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
if (this.allowMultiQueries){
if (this.allowMultiQueries) {
// ====================================== 2. upsertByExample ======================================
XmlElement eleUpsertByExampleWithBLOBs = new XmlElement("insert");
eleUpsertByExampleWithBLOBs.addAttribute(new Attribute("id", METHOD_UPSERT_BY_EXAMPLE_WITH_BLOBS));
......@@ -287,7 +287,9 @@ public class UpsertPlugin extends BasePlugin {
// insert
eleUpsertByExampleWithBLOBs.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
eleUpsertByExampleWithBLOBs.addElement(XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns()));
for (Element element : XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns())) {
eleUpsertByExampleWithBLOBs.addElement(element);
}
this.generateExistsClause(introspectedTable, eleUpsertByExampleWithBLOBs, false, true);
// multiQueries
......@@ -296,7 +298,10 @@ public class UpsertPlugin extends BasePlugin {
// update
eleUpsertByExampleWithBLOBs.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
eleUpsertByExampleWithBLOBs.addElement(new TextElement("set"));
eleUpsertByExampleWithBLOBs.addElement(XmlElementGeneratorTools.generateSets(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record."));
for (Element element : XmlElementGeneratorTools.generateSets(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record.")) {
eleUpsertByExampleWithBLOBs.addElement(element);
}
// update where
eleUpsertByExampleWithBLOBs.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
......@@ -337,11 +342,10 @@ public class UpsertPlugin extends BasePlugin {
/**
* 当Model没有生成WithBLOBs类时的情况
*
* @param document
* @param introspectedTable
*/
private void generateXmlElementWithoutBLOBs(Document document, IntrospectedTable introspectedTable){
private void generateXmlElementWithoutBLOBs(Document document, IntrospectedTable introspectedTable) {
// WithoutBLOBs也会存在只有一个时,不生成WithBLOBs对象的情况
boolean flag = !introspectedTable.getRules().generateRecordWithBLOBsClass();
List<IntrospectedColumn> columns = flag ? introspectedTable.getAllColumns() : introspectedTable.getNonBLOBColumns();
......@@ -360,11 +364,17 @@ public class UpsertPlugin extends BasePlugin {
// insert
eleUpsert.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
eleUpsert.addElement(XmlElementGeneratorTools.generateKeys(columns));
for (Element element : XmlElementGeneratorTools.generateKeys(columns)) {
eleUpsert.addElement(element);
}
eleUpsert.addElement(new TextElement("values"));
eleUpsert.addElement(XmlElementGeneratorTools.generateValues(columns));
for (Element element : XmlElementGeneratorTools.generateValues(columns)) {
eleUpsert.addElement(element);
}
eleUpsert.addElement(new TextElement("on duplicate key update "));
eleUpsert.addElement(XmlElementGeneratorTools.generateSets(columns));
for (Element element : XmlElementGeneratorTools.generateSets(columns)) {
eleUpsert.addElement(element);
}
document.getRootElement().addElement(eleUpsert);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsert实现方法。");
......@@ -392,7 +402,7 @@ public class UpsertPlugin extends BasePlugin {
document.getRootElement().addElement(eleUpsertSelective);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
if (this.allowMultiQueries){
if (this.allowMultiQueries) {
// ====================================== 2. upsertByExample ======================================
XmlElement eleUpsertByExample = new XmlElement("insert");
eleUpsertByExample.addAttribute(new Attribute("id", METHOD_UPSERT_BY_EXAMPLE));
......@@ -406,7 +416,9 @@ public class UpsertPlugin extends BasePlugin {
// insert
eleUpsertByExample.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
eleUpsertByExample.addElement(XmlElementGeneratorTools.generateKeys(columns));
for (Element element : XmlElementGeneratorTools.generateKeys(columns)) {
eleUpsertByExample.addElement(element);
}
this.generateExistsClause(introspectedTable, eleUpsertByExample, false, flag);
// multiQueries
......@@ -415,7 +427,9 @@ public class UpsertPlugin extends BasePlugin {
// update
eleUpsertByExample.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
eleUpsertByExample.addElement(new TextElement("set"));
eleUpsertByExample.addElement(XmlElementGeneratorTools.generateSets(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(columns), "record."));
for (Element element : XmlElementGeneratorTools.generateSets(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(columns), "record.")) {
eleUpsertByExample.addElement(element);
}
// update where
eleUpsertByExample.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
......@@ -456,20 +470,21 @@ public class UpsertPlugin extends BasePlugin {
/**
* exists 语句
*
* @param introspectedTable
* @param element
* @param selective
* @param allColumns
*/
private void generateExistsClause(IntrospectedTable introspectedTable, XmlElement element, boolean selective, boolean allColumns){
private void generateExistsClause(IntrospectedTable introspectedTable, XmlElement element, boolean selective, boolean allColumns) {
List<IntrospectedColumn> columns = allColumns ? introspectedTable.getAllColumns() : introspectedTable.getNonBLOBColumns();
element.addElement(new TextElement("select"));
if (selective){
if (selective) {
element.addElement(XmlElementGeneratorTools.generateValuesSelective(columns, "record.", false));
} else {
element.addElement(XmlElementGeneratorTools.generateValues(columns, "record.", false));
for (Element element1 : XmlElementGeneratorTools.generateValues(columns, "record.", false)) {
element.addElement(element1);
}
}
element.addElement(new TextElement("from dual where not exists"));
element.addElement(new TextElement("("));
......
......@@ -18,6 +18,7 @@ package com.itfsw.mybatis.generator.plugins.utils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.OutputUtilities;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.api.dom.xml.TextElement;
......@@ -50,11 +51,11 @@ public class XmlElementGeneratorTools {
String identityColumnType = introspectedColumn
.getFullyQualifiedJavaType().getFullyQualifiedName();
XmlElement answer = new XmlElement("selectKey");
answer.addAttribute(new Attribute("resultType", identityColumnType));
XmlElement answer = new XmlElement("selectKey");
answer.addAttribute(new Attribute("resultType", identityColumnType));
answer.addAttribute(new Attribute(
"keyProperty", introspectedColumn.getJavaProperty()));
answer.addAttribute(new Attribute("order",
"keyProperty", introspectedColumn.getJavaProperty()));
answer.addAttribute(new Attribute("order",
generatedKey.getMyBatis3Order()));
answer.addElement(new TextElement(generatedKey
......@@ -64,25 +65,25 @@ public class XmlElementGeneratorTools {
}
public static Element getBaseColumnListElement(IntrospectedTable introspectedTable) {
XmlElement answer = new XmlElement("include");
answer.addAttribute(new Attribute("refid",
XmlElement answer = new XmlElement("include");
answer.addAttribute(new Attribute("refid",
introspectedTable.getBaseColumnListId()));
return answer;
}
public static Element getBlobColumnListElement(IntrospectedTable introspectedTable) {
XmlElement answer = new XmlElement("include");
answer.addAttribute(new Attribute("refid",
XmlElement answer = new XmlElement("include");
answer.addAttribute(new Attribute("refid",
introspectedTable.getBlobColumnListId()));
return answer;
}
public static Element getExampleIncludeElement(IntrospectedTable introspectedTable) {
XmlElement ifElement = new XmlElement("if");
ifElement.addAttribute(new Attribute("test", "_parameter != null"));
XmlElement ifElement = new XmlElement("if");
ifElement.addAttribute(new Attribute("test", "_parameter != null"));
XmlElement includeElement = new XmlElement("include");
includeElement.addAttribute(new Attribute("refid",
XmlElement includeElement = new XmlElement("include");
includeElement.addAttribute(new Attribute("refid",
introspectedTable.getExampleWhereClauseId()));
ifElement.addElement(includeElement);
......@@ -90,11 +91,11 @@ public class XmlElementGeneratorTools {
}
public static Element getUpdateByExampleIncludeElement(IntrospectedTable introspectedTable) {
XmlElement ifElement = new XmlElement("if");
ifElement.addAttribute(new Attribute("test", "_parameter != null"));
XmlElement ifElement = new XmlElement("if");
ifElement.addAttribute(new Attribute("test", "_parameter != null"));
XmlElement includeElement = new XmlElement("include");
includeElement.addAttribute(new Attribute("refid",
XmlElement includeElement = new XmlElement("include");
includeElement.addAttribute(new Attribute("refid",
introspectedTable.getMyBatis3UpdateByExampleWhereClauseId()));
ifElement.addElement(includeElement);
......@@ -103,22 +104,20 @@ public class XmlElementGeneratorTools {
/**
* 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
*
* @param element
* @param introspectedTable
*/
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable){
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable) {
useGeneratedKeys(element, introspectedTable, null);
}
/**
* 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
*
* @param element
* @param introspectedTable
* @param prefix
*/
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable, String prefix){
public static void useGeneratedKeys(XmlElement element, IntrospectedTable introspectedTable, String prefix) {
GeneratedKey gk = introspectedTable.getGeneratedKey();
if (gk != null) {
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
......@@ -126,9 +125,9 @@ public class XmlElementGeneratorTools {
// warning has already been reported
if (introspectedColumn != null) {
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
element.addAttribute(new Attribute("useGeneratedKeys", "true"));
element.addAttribute(new Attribute("keyProperty", (prefix == null ? "" : prefix) + introspectedColumn.getJavaProperty()));
element.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
element.addAttribute(new Attribute("useGeneratedKeys", "true"));
element.addAttribute(new Attribute("keyProperty", (prefix == null ? "" : prefix) + introspectedColumn.getJavaProperty()));
element.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
}
}
}
......@@ -138,7 +137,7 @@ public class XmlElementGeneratorTools {
* @param columns
* @return
*/
public static Element generateKeys(List<IntrospectedColumn> columns) {
public static List<Element> generateKeys(List<IntrospectedColumn> columns) {
return generateKeys(columns, true);
}
......@@ -148,7 +147,7 @@ public class XmlElementGeneratorTools {
* @param bracket
* @return
*/
public static Element generateKeys(List<IntrospectedColumn> columns, boolean bracket) {
public static List<Element> generateKeys(List<IntrospectedColumn> columns, boolean bracket) {
return generateCommColumns(columns, null, bracket, 1);
}
......@@ -187,7 +186,7 @@ public class XmlElementGeneratorTools {
* @param columns
* @return
*/
public static Element generateValues(List<IntrospectedColumn> columns) {
public static List<Element> generateValues(List<IntrospectedColumn> columns) {
return generateValues(columns, null);
}
......@@ -197,7 +196,7 @@ public class XmlElementGeneratorTools {
* @param prefix
* @return
*/
public static Element generateValues(List<IntrospectedColumn> columns, String prefix) {
public static List<Element> generateValues(List<IntrospectedColumn> columns, String prefix) {
return generateValues(columns, prefix, true);
}
......@@ -208,7 +207,7 @@ public class XmlElementGeneratorTools {
* @param bracket
* @return
*/
public static Element generateValues(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
public static List<Element> generateValues(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
return generateCommColumns(columns, prefix, bracket, 2);
}
......@@ -247,7 +246,7 @@ public class XmlElementGeneratorTools {
* @param columns
* @return
*/
public static Element generateSets(List<IntrospectedColumn> columns) {
public static List<Element> generateSets(List<IntrospectedColumn> columns) {
return generateSets(columns, null, false);
}
......@@ -257,7 +256,7 @@ public class XmlElementGeneratorTools {
* @param prefix
* @return
*/
public static Element generateSets(List<IntrospectedColumn> columns, String prefix) {
public static List<Element> generateSets(List<IntrospectedColumn> columns, String prefix) {
return generateSets(columns, prefix, false);
}
......@@ -268,7 +267,7 @@ public class XmlElementGeneratorTools {
* @param bracket
* @return
*/
public static Element generateSets(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
public static List<Element> generateSets(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
return generateCommColumns(columns, prefix, bracket, 3);
}
......@@ -310,8 +309,9 @@ public class XmlElementGeneratorTools {
* @param type 1:key,2:value,3:set
* @return
*/
private static Element generateCommColumns(List<IntrospectedColumn> columns, String prefix, boolean bracket, int type) {
StringBuffer sb = new StringBuffer(bracket ? "(" : "");
private static List<Element> generateCommColumns(List<IntrospectedColumn> columns, String prefix, boolean bracket, int type) {
List<Element> list = new ArrayList<>();
StringBuilder sb = new StringBuilder(bracket ? "(" : "");
Iterator<IntrospectedColumn> columnIterator = columns.iterator();
while (columnIterator.hasNext()) {
IntrospectedColumn introspectedColumn = columnIterator.next();
......@@ -333,9 +333,24 @@ public class XmlElementGeneratorTools {
if (columnIterator.hasNext()) {
sb.append(", ");
}
// 保持和官方一致 80 进行换行
if (type == 1 || type == 2) {
if (sb.length() > 80) {
list.add(new TextElement(sb.toString()));
sb.setLength(0);
OutputUtilities.xmlIndent(sb, 1);
}
} else {
list.add(new TextElement(sb.toString()));
sb.setLength(0);
}
}
if (sb.length() > 0 || bracket){
list.add(new TextElement(sb.append(bracket ? ")" : "").toString()));
}
return new TextElement(sb.append(bracket ? ")" : "").toString());
return list;
}
/**
......@@ -380,12 +395,11 @@ public class XmlElementGeneratorTools {
/**
* 查找指定xml节点下指定节点名称的元素
*
* @param xmlElement
* @param name
* @return
*/
public static List<XmlElement> findXmlElements(XmlElement xmlElement, String name){
public static List<XmlElement> findXmlElements(XmlElement xmlElement, String name) {
List<XmlElement> list = new ArrayList<>();
List<Element> elements = xmlElement.getElements();
for (Element ele : elements) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment