Commit 6ac9cd0c authored by hewei's avatar hewei

fixed:[issues#2]

1.upsertByExampleSelective 实现 ;
2.代码精简;
parent 12bd2b18
...@@ -19,18 +19,18 @@ package com.itfsw.mybatis.generator.plugins; ...@@ -19,18 +19,18 @@ package com.itfsw.mybatis.generator.plugins;
import com.itfsw.mybatis.generator.plugins.utils.CommTools; import com.itfsw.mybatis.generator.plugins.utils.CommTools;
import com.itfsw.mybatis.generator.plugins.utils.CommentTools; import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools; 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.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*; import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.*; 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.codegen.mybatis3.ListUtilities; import org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.internal.util.StringUtility; import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
...@@ -47,6 +47,7 @@ public class UpsertPlugin extends PluginAdapter { ...@@ -47,6 +47,7 @@ public class UpsertPlugin extends PluginAdapter {
public static final String METHOD_UPSERT = "upsert"; // 方法名 public static final String METHOD_UPSERT = "upsert"; // 方法名
public static final String METHOD_UPSERT_SELECTIVE = "upsertSelective"; // 方法名 public static final String METHOD_UPSERT_SELECTIVE = "upsertSelective"; // 方法名
public static final String METHOD_UPSERT_BY_EXAMPLE = "upsertByExample"; // 方法名 public static final String METHOD_UPSERT_BY_EXAMPLE = "upsertByExample"; // 方法名
public static final String METHOD_UPSERT_BY_EXAMPLE_SELECTIVE = "upsertByExampleSelective"; // 方法名
public static final String PRE_ALLOW_MULTI_QUERIES = "allowMultiQueries"; // property allowMultiQueries public static final String PRE_ALLOW_MULTI_QUERIES = "allowMultiQueries"; // property allowMultiQueries
private boolean allowMultiQueries = false; // 是否允许多sql提交 private boolean allowMultiQueries = false; // 是否允许多sql提交
...@@ -128,6 +129,19 @@ public class UpsertPlugin extends PluginAdapter { ...@@ -128,6 +129,19 @@ public class UpsertPlugin extends PluginAdapter {
// interface 增加方法 // interface 增加方法
interfaze.addMethod(mUpsertByExample); interfaze.addMethod(mUpsertByExample);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExample方法。"); logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExample方法。");
// ====================================== 4. upsertByExampleSelective ======================================
Method mUpsertByExampleSelective = new Method(METHOD_UPSERT_BY_EXAMPLE_SELECTIVE);
// 返回值类型
mUpsertByExampleSelective.setReturnType(null);
// 添加参数
mUpsertByExampleSelective.addParameter(new Parameter(introspectedTable.getRules().calculateAllFieldsClass(), "record", "@Param(\"record\")"));
mUpsertByExampleSelective.addParameter(new Parameter(new FullyQualifiedJavaType(introspectedTable.getExampleType()), "example", "@Param(\"example\")"));
// 添加方法说明
CommentTools.addGeneralMethodComment(mUpsertByExampleSelective, introspectedTable);
// interface 增加方法
interfaze.addMethod(mUpsertByExampleSelective);
logger.debug("itfsw(存在即更新插件):" + interfaze.getType().getShortName() + "增加upsertByExampleSelective方法。");
} }
return true; return true;
...@@ -158,13 +172,11 @@ public class UpsertPlugin extends PluginAdapter { ...@@ -158,13 +172,11 @@ public class UpsertPlugin extends PluginAdapter {
// insert // insert
eleUpsert.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime())); eleUpsert.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
eleUpsert.addElement(this.generateInsertClause(introspectedTable)); eleUpsert.addElement(XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns()));
eleUpsert.addElement(new TextElement("values")); eleUpsert.addElement(new TextElement("values"));
eleUpsert.addElement(new TextElement("(")); eleUpsert.addElement(XmlElementGeneratorTools.generateValues(introspectedTable.getAllColumns()));
eleUpsert.addElement(this.generateValuesClause(introspectedTable));
eleUpsert.addElement(new TextElement(")"));
eleUpsert.addElement(new TextElement("on duplicate key update ")); eleUpsert.addElement(new TextElement("on duplicate key update "));
eleUpsert.addElement(this.generateDuplicateClause(introspectedTable)); eleUpsert.addElement(XmlElementGeneratorTools.generateSets(introspectedTable.getAllColumns()));
document.getRootElement().addElement(eleUpsert); document.getRootElement().addElement(eleUpsert);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsert实现方法。"); logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsert实现方法。");
...@@ -183,11 +195,11 @@ public class UpsertPlugin extends PluginAdapter { ...@@ -183,11 +195,11 @@ public class UpsertPlugin extends PluginAdapter {
// insert // insert
eleUpsertSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime())); eleUpsertSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
eleUpsertSelective.addElement(this.generateInsertSelectiveClause(introspectedTable)); eleUpsertSelective.addElement(XmlElementGeneratorTools.generateKeysSelective(introspectedTable.getAllColumns()));
eleUpsertSelective.addElement(new TextElement("values")); eleUpsertSelective.addElement(new TextElement("values"));
eleUpsertSelective.addElement(this.generateValuesSelectiveClause(introspectedTable)); eleUpsertSelective.addElement(XmlElementGeneratorTools.generateValuesSelective(introspectedTable.getAllColumns()));
eleUpsertSelective.addElement(new TextElement("on duplicate key update ")); eleUpsertSelective.addElement(new TextElement("on duplicate key update "));
eleUpsertSelective.addElement(this.generateDuplicateSelectiveClause(introspectedTable)); eleUpsertSelective.addElement(XmlElementGeneratorTools.generateSetsSelective(introspectedTable.getAllColumns(), null, false));
document.getRootElement().addElement(eleUpsertSelective); document.getRootElement().addElement(eleUpsertSelective);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。"); logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
...@@ -206,8 +218,8 @@ public class UpsertPlugin extends PluginAdapter { ...@@ -206,8 +218,8 @@ public class UpsertPlugin extends PluginAdapter {
// insert // insert
eleUpsertByExample.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime())); eleUpsertByExample.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
eleUpsertByExample.addElement(this.generateInsertClause(introspectedTable)); eleUpsertByExample.addElement(XmlElementGeneratorTools.generateKeys(introspectedTable.getAllColumns()));
this.generateExistsClause(introspectedTable, eleUpsertByExample); this.generateExistsClause(introspectedTable, eleUpsertByExample, false);
// multiQueries // multiQueries
eleUpsertByExample.addElement(new TextElement(";")); eleUpsertByExample.addElement(new TextElement(";"));
...@@ -215,24 +227,42 @@ public class UpsertPlugin extends PluginAdapter { ...@@ -215,24 +227,42 @@ public class UpsertPlugin extends PluginAdapter {
// update // update
eleUpsertByExample.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())); eleUpsertByExample.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
eleUpsertByExample.addElement(new TextElement("set")); eleUpsertByExample.addElement(new TextElement("set"));
Iterator<IntrospectedColumn> iterator = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()).iterator(); eleUpsertByExample.addElement(XmlElementGeneratorTools.generateSets(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record."));
StringBuffer sb = new StringBuffer();
while (iterator.hasNext()){
IntrospectedColumn column = iterator.next();
sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(column));
sb.append(" = ");
sb.append(MyBatis3FormattingUtilities.getParameterClause(column, "record."));
if (iterator.hasNext()){
sb.append(",");
}
}
eleUpsertByExample.addElement(new TextElement(sb.toString()));
// update where // update where
eleUpsertByExample.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable)); eleUpsertByExample.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
document.getRootElement().addElement(eleUpsertByExample); document.getRootElement().addElement(eleUpsertByExample);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。"); logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
// ====================================== 4. upsertByExampleSelective ======================================
XmlElement eleUpsertByExampleSelective = new XmlElement("insert");
eleUpsertByExampleSelective.addAttribute(new Attribute("id", METHOD_UPSERT_BY_EXAMPLE_SELECTIVE));
// 参数类型
eleUpsertByExampleSelective.addAttribute(new Attribute("parameterType", "map"));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools.addComment(eleUpsertByExampleSelective);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
CommTools.useGeneratedKeys(eleUpsertByExampleSelective, introspectedTable, "record.");
// insert
eleUpsertByExampleSelective.addElement(new TextElement("insert into " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateKeysSelective(introspectedTable.getAllColumns(), "record."));
this.generateExistsClause(introspectedTable, eleUpsertByExampleSelective, true);
// multiQueries
eleUpsertByExampleSelective.addElement(new TextElement(";"));
// update
eleUpsertByExampleSelective.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
eleUpsertByExampleSelective.addElement(new TextElement("set"));
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.generateSets(ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns()), "record."));
// update where
eleUpsertByExampleSelective.addElement(XmlElementGeneratorTools.getUpdateByExampleIncludeElement(introspectedTable));
document.getRootElement().addElement(eleUpsertByExampleSelective);
logger.debug("itfsw(存在即更新插件):" + introspectedTable.getMyBatis3XmlMapperFileName() + "增加upsertSelective实现方法。");
} }
return true; return true;
...@@ -242,11 +272,16 @@ public class UpsertPlugin extends PluginAdapter { ...@@ -242,11 +272,16 @@ public class UpsertPlugin extends PluginAdapter {
* exists 语句 * exists 语句
* *
* @param introspectedTable * @param introspectedTable
* @return * @param element
* @param selective
*/ */
private void generateExistsClause(IntrospectedTable introspectedTable, XmlElement element){ private void generateExistsClause(IntrospectedTable introspectedTable, XmlElement element, boolean selective){
element.addElement(new TextElement("select")); element.addElement(new TextElement("select"));
element.addElement(this.generateValuesClause(introspectedTable, "record.")); if (selective){
element.addElement(XmlElementGeneratorTools.generateValuesSelective(introspectedTable.getAllColumns(), "record.", false));
} else {
element.addElement(XmlElementGeneratorTools.generateValues(introspectedTable.getAllColumns(), "record.", false));
}
element.addElement(new TextElement("from dual where not exists")); element.addElement(new TextElement("from dual where not exists"));
element.addElement(new TextElement("(")); element.addElement(new TextElement("("));
element.addElement(new TextElement("select 1 from " + introspectedTable.getFullyQualifiedTableNameAtRuntime())); element.addElement(new TextElement("select 1 from " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
...@@ -257,181 +292,4 @@ public class UpsertPlugin extends PluginAdapter { ...@@ -257,181 +292,4 @@ public class UpsertPlugin extends PluginAdapter {
element.addElement(new TextElement(")")); element.addElement(new TextElement(")"));
} }
/**
* 普通insert
*
* @param introspectedTable
* @return
*/
private Element generateInsertClause(IntrospectedTable introspectedTable){
StringBuilder insertClause = new StringBuilder();
insertClause.append(" (");
List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
for (int i = 0; i < columns.size(); i++) {
IntrospectedColumn introspectedColumn = columns.get(i);
insertClause.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
if (i + 1 < columns.size()) {
insertClause.append(", ");
}
}
insertClause.append(") ");
return new TextElement(insertClause.toString());
}
/**
* 普通 values
*
* @param introspectedTable
* @return
*/
private Element generateValuesClause(IntrospectedTable introspectedTable){
return this.generateValuesClause(introspectedTable, null);
}
/**
* 普通 values
*
* @param introspectedTable
* @param prefix
* @return
*/
private Element generateValuesClause(IntrospectedTable introspectedTable, String prefix){
StringBuilder valuesClause = new StringBuilder();
List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
for (int i = 0; i < columns.size(); i++) {
IntrospectedColumn introspectedColumn = columns.get(i);
valuesClause.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
if (i + 1 < columns.size()) {
valuesClause.append(", ");
}
}
return new TextElement(valuesClause.toString());
}
/**
* 普通duplicate
*
* @param introspectedTable
* @return
*/
private Element generateDuplicateClause(IntrospectedTable introspectedTable){
StringBuilder duplicateClause = new StringBuilder();
List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
for (int i = 0; i < columns.size(); i++) {
IntrospectedColumn introspectedColumn = columns.get(i);
duplicateClause.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
duplicateClause.append(" = ");
duplicateClause.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
if (i + 1 < columns.size()) {
duplicateClause.append(", ");
}
}
return new TextElement(duplicateClause.toString());
}
/**
* 普通insert
*
* @param introspectedTable
* @return
*/
private Element generateInsertSelectiveClause(IntrospectedTable introspectedTable){
XmlElement insertTrimEle = new XmlElement("trim");
insertTrimEle.addAttribute(new Attribute("prefix", "("));
insertTrimEle.addAttribute(new Attribute("suffix", ")"));
insertTrimEle.addAttribute(new Attribute("suffixOverrides", ","));
StringBuffer sb = new StringBuffer();
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
XmlElement insertNotNullElement = new XmlElement("if"); //$NON-NLS-1$
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
sb.append(" != null");
insertNotNullElement.addAttribute(new Attribute("test", sb.toString()));
sb.setLength(0);
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
sb.append(',');
insertNotNullElement.addElement(new TextElement(sb.toString()));
insertTrimEle.addElement(insertNotNullElement);
}
return insertTrimEle;
}
/**
* 普通 values
*
* @param introspectedTable
* @return
*/
private Element generateValuesSelectiveClause(IntrospectedTable introspectedTable){
XmlElement valuesTrimEle = new XmlElement("trim");
valuesTrimEle.addAttribute(new Attribute("prefix", "("));
valuesTrimEle.addAttribute(new Attribute("suffix", ")"));
valuesTrimEle.addAttribute(new Attribute("suffixOverrides", ","));
StringBuffer sb = new StringBuffer();
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
XmlElement valuesNotNullElement = new XmlElement("if"); //$NON-NLS-1$
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
sb.append(" != null");
valuesNotNullElement.addAttribute(new Attribute("test", sb.toString()));
sb.setLength(0);
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
sb.append(',');
valuesNotNullElement.addElement(new TextElement(sb.toString()));
valuesTrimEle.addElement(valuesNotNullElement);
}
return valuesTrimEle;
}
/**
* 普通duplicate
*
* @param introspectedTable
* @return
*/
private Element generateDuplicateSelectiveClause(IntrospectedTable introspectedTable){
XmlElement duplicateTrimEle = new XmlElement("trim");
duplicateTrimEle.addAttribute(new Attribute("suffixOverrides", ","));
StringBuffer sb = new StringBuffer();
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
XmlElement duplicateNotNullElement = new XmlElement("if"); //$NON-NLS-1$
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
sb.append(" != null");
duplicateNotNullElement.addAttribute(new Attribute("test", sb.toString()));
sb.setLength(0);
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
sb.append(" = ");
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
sb.append(",");
duplicateNotNullElement.addElement(new TextElement(sb.toString()));
duplicateTrimEle.addElement(duplicateNotNullElement);
}
return duplicateTrimEle;
}
} }
\ No newline at end of file
...@@ -19,10 +19,15 @@ package com.itfsw.mybatis.generator.plugins.utils; ...@@ -19,10 +19,15 @@ package com.itfsw.mybatis.generator.plugins.utils;
import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.xml.Attribute; import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.api.dom.xml.TextElement; import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.config.GeneratedKey; import org.mybatis.generator.config.GeneratedKey;
import java.util.Iterator;
import java.util.List;
/** /**
* --------------------------------------------------------------------------- * ---------------------------------------------------------------------------
* Xml 节点生成工具 参考 org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator * Xml 节点生成工具 参考 org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator
...@@ -36,14 +41,11 @@ public class XmlElementGeneratorTools { ...@@ -36,14 +41,11 @@ public class XmlElementGeneratorTools {
/** /**
* This method should return an XmlElement for the select key used to * This method should return an XmlElement for the select key used to
* automatically generate keys. * automatically generate keys.
* * @param introspectedColumn the column related to the select key statement
* @param introspectedColumn * @param generatedKey the generated key for the current table
* the column related to the select key statement
* @param generatedKey
* the generated key for the current table
* @return the selectKey element * @return the selectKey element
*/ */
public static XmlElement getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) { public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) {
String identityColumnType = introspectedColumn String identityColumnType = introspectedColumn
.getFullyQualifiedJavaType().getFullyQualifiedName(); .getFullyQualifiedJavaType().getFullyQualifiedName();
...@@ -60,21 +62,21 @@ public class XmlElementGeneratorTools { ...@@ -60,21 +62,21 @@ public class XmlElementGeneratorTools {
return answer; return answer;
} }
public static XmlElement getBaseColumnListElement(IntrospectedTable introspectedTable) { public static Element getBaseColumnListElement(IntrospectedTable introspectedTable) {
XmlElement answer = new XmlElement("include"); //$NON-NLS-1$ XmlElement answer = new XmlElement("include"); //$NON-NLS-1$
answer.addAttribute(new Attribute("refid", //$NON-NLS-1$ answer.addAttribute(new Attribute("refid", //$NON-NLS-1$
introspectedTable.getBaseColumnListId())); introspectedTable.getBaseColumnListId()));
return answer; return answer;
} }
public static XmlElement getBlobColumnListElement(IntrospectedTable introspectedTable) { public static Element getBlobColumnListElement(IntrospectedTable introspectedTable) {
XmlElement answer = new XmlElement("include"); //$NON-NLS-1$ XmlElement answer = new XmlElement("include"); //$NON-NLS-1$
answer.addAttribute(new Attribute("refid", //$NON-NLS-1$ answer.addAttribute(new Attribute("refid", //$NON-NLS-1$
introspectedTable.getBlobColumnListId())); introspectedTable.getBlobColumnListId()));
return answer; return answer;
} }
public static XmlElement getExampleIncludeElement(IntrospectedTable introspectedTable) { public static Element getExampleIncludeElement(IntrospectedTable introspectedTable) {
XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$ XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$ ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$
...@@ -86,7 +88,7 @@ public class XmlElementGeneratorTools { ...@@ -86,7 +88,7 @@ public class XmlElementGeneratorTools {
return ifElement; return ifElement;
} }
public static XmlElement getUpdateByExampleIncludeElement(IntrospectedTable introspectedTable) { public static Element getUpdateByExampleIncludeElement(IntrospectedTable introspectedTable) {
XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$ XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$ ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$
...@@ -97,4 +99,250 @@ public class XmlElementGeneratorTools { ...@@ -97,4 +99,250 @@ public class XmlElementGeneratorTools {
return ifElement; return ifElement;
} }
/**
* 生成keys Ele
* @param columns
* @return
*/
public static Element generateKeys(List<IntrospectedColumn> columns) {
return generateKeys(columns, true);
}
/**
* 生成keys Ele
* @param columns
* @param bracket
* @return
*/
public static Element generateKeys(List<IntrospectedColumn> columns, boolean bracket) {
return generateCommColumns(columns, null, bracket, 1);
}
/**
* 生成keys Selective Ele
* @param columns
* @return
*/
public static Element generateKeysSelective(List<IntrospectedColumn> columns) {
return generateKeysSelective(columns, null);
}
/**
* 生成keys Selective Ele
* @param columns
* @param prefix
* @return
*/
public static Element generateKeysSelective(List<IntrospectedColumn> columns, String prefix) {
return generateKeysSelective(columns, prefix, true);
}
/**
* 生成keys Selective Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public static Element generateKeysSelective(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
return generateCommColumnsSelective(columns, prefix, bracket, 1);
}
/**
* 生成values Ele
* @param columns
* @return
*/
public static Element generateValues(List<IntrospectedColumn> columns) {
return generateValues(columns, null);
}
/**
* 生成values Ele
* @param columns
* @param prefix
* @return
*/
public static Element generateValues(List<IntrospectedColumn> columns, String prefix) {
return generateValues(columns, prefix, true);
}
/**
* 生成values Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public static Element generateValues(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
return generateCommColumns(columns, prefix, bracket, 2);
}
/**
* 生成values Selective Ele
* @param columns
* @return
*/
public static Element generateValuesSelective(List<IntrospectedColumn> columns) {
return generateValuesSelective(columns, null);
}
/**
* 生成values Selective Ele
* @param columns
* @param prefix
* @return
*/
public static Element generateValuesSelective(List<IntrospectedColumn> columns, String prefix) {
return generateValuesSelective(columns, prefix, true);
}
/**
* 生成values Selective Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public static Element generateValuesSelective(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
return generateCommColumnsSelective(columns, prefix, bracket, 2);
}
/**
* 生成sets Ele
* @param columns
* @return
*/
public static Element generateSets(List<IntrospectedColumn> columns) {
return generateSets(columns, null, false);
}
/**
* 生成sets Ele
* @param columns
* @param prefix
* @return
*/
public static Element generateSets(List<IntrospectedColumn> columns, String prefix) {
return generateSets(columns, prefix, false);
}
/**
* 生成sets Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public static Element generateSets(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
return generateCommColumns(columns, prefix, bracket, 3);
}
/**
* 生成sets Selective Ele
* @param columns
* @return
*/
public static Element generateSetsSelective(List<IntrospectedColumn> columns) {
return generateSetsSelective(columns, null, false);
}
/**
* 生成sets Selective Ele
* @param columns
* @param prefix
* @return
*/
public static Element generateSetsSelective(List<IntrospectedColumn> columns, String prefix) {
return generateSetsSelective(columns, prefix, false);
}
/**
* 生成sets Selective Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public static Element generateSetsSelective(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
return generateCommColumnsSelective(columns, prefix, bracket, 3);
}
/**
* 通用遍历columns
* @param columns
* @param prefix
* @param bracket
* @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 ? "(" : "");
Iterator<IntrospectedColumn> columnIterator = columns.iterator();
while (columnIterator.hasNext()) {
IntrospectedColumn introspectedColumn = columnIterator.next();
switch (type) {
case 3:
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
sb.append(" = ");
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
break;
case 2:
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
break;
case 1:
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
break;
}
if (columnIterator.hasNext()) {
sb.append(", ");
}
}
return new TextElement(sb.append(bracket ? ")" : "").toString());
}
/**
* 通用遍历columns
* @param columns
* @param prefix
* @param bracket
* @param type 1:key,2:value,3:set
* @return
*/
private static Element generateCommColumnsSelective(List<IntrospectedColumn> columns, String prefix, boolean bracket, int type) {
XmlElement eleTrim = new XmlElement("trim");
if (bracket) {
eleTrim.addAttribute(new Attribute("prefix", "("));
eleTrim.addAttribute(new Attribute("suffix", ")"));
eleTrim.addAttribute(new Attribute("suffixOverrides", ","));
} else {
eleTrim.addAttribute(new Attribute("suffixOverrides", ","));
}
for (IntrospectedColumn introspectedColumn : columns) {
XmlElement eleIf = new XmlElement("if");
eleIf.addAttribute(new Attribute("test", introspectedColumn.getJavaProperty(prefix) + " != null"));
switch (type) {
case 3:
eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
break;
case 2:
eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
break;
case 1:
eleIf.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ","));
break;
}
eleTrim.addElement(eleIf);
}
return eleTrim;
}
} }
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