MyBatis学习-使用SqlBuilder生成SQL语句

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


import org.apache.ibatis.jdbc.SqlBuilder;




public class Record extends SqlBuilder implements Serializable {


private static final long serialVersionUID = -74709401356489160L;


private DbType dbType = DbType.INSERT;


public DbType getDbType() {
return dbType;
}


public void setDbType(DbType dbType) {
this.dbType = dbType;
}


public String getSchema() {
return schema;
}


public void setSchema(String schema) {
this.schema = schema;
}


private String schema;


private String tableName;


private List columns;


private List values;


private List keys;


private List keyValues;


private String recordKey;


public String getRecordKey() {
return recordKey;
}


public void setRecordKey(String recordKey) {
this.recordKey = recordKey;
}


public Record(String schema, String tableName) {
if (tableName == null)
throw new NullPointerException("record tableName is null");
if (schema == null || schema.length() == 0)
throw new NullPointerException(
"record schema is null , tableName : " + tableName);
this.columns = new ArrayList();
this.values = new ArrayList();
this.keys = new ArrayList();
this.keyValues = new ArrayList();
this.tableName = tableName;
this.schema = schema;


}


public List getColumns() {
return columns;
}


public void setColumns(List columns) {
this.columns = columns;
}


public List getValues() {
return values;
}


public String getTableFullName() {
return (schema == null ? tableName : schema + "." + tableName);
}


public String getTableName() {
return tableName;
}


public void setTableName(String tableName) {
this.tableName = tableName;
}


public void setValues(List values) {
this.values = values;
}


public List getKeys() {
return keys;
}


public void setKeys(List keys) {
this.keys = keys;
}


public List getKeyValues() {
return keyValues;
}


public void setKeyValues(List keyValues) {
this.keyValues = keyValues;
}


public void addFieldAndValue(String column, Object value, boolean isKey) {
this.columns.add(column);
this.values.add(value);
if (isKey)
addKeyAndValue(column, value);
}


private void addKeyAndValue(String column, Object value) {
this.keys.add(column);
this.keyValues.add(value);
this.recordKey += value.toString();
}


public String insertSqlForPreparedStatement() {
BEGIN();
INSERT_INTO(schema + "." + tableName);
for (String column : columns)
VALUES(column, "?");
return SQL().replaceAll("\\n", "");
}


public String updateSqlForPreparedStatement() {
BEGIN();
UPDATE(schema + "." + tableName);
for (String column : columns) {
if (keys.contains(column))
continue;
SET(column + "=?");
}
for (String keyString : keys)
WHERE(keyString + "=?");
return SQL().replaceAll("\\n", " ");
}


public String toString() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("[sql type:" + dbType + ";")
.append("SCHEMA:" + schema + ";")
.append("tableName:" + tableName + ";")
.append("columns:" + columns + ";")
.append("values:" + values + ";")
.append("keyValues:" + keyValues + ";").append("]");
return stringBuffer.toString();
}


public String insterOrUpdateSqlForPreparedStatement() {
StringBuffer sql = new StringBuffer("MERGE INTO ");
sql.append(schema + "." + tableName + " T1 ");
sql.append(" USING (SELECT ");
for (int i = 0; i < columns.size(); i++) {
if (i == 0) {
sql.append(" ? as " + columns.get(i));
} else {
sql.append(", ? as " + columns.get(i));
}


}
sql.append(" FROM dual) T2 ON ( ");
for (int i = 0; i < keys.size(); i++) {
if (i == 0) {
sql.append("T1." + keys.get(i) + "=T2." + keys.get(i));
} else {
sql.append(" and T1." + keys.get(i) + "=T2." + keys.get(i));
}
}
sql.append(") WHEN MATCHED THEN UPDATE SET ");
for (int i = 0; i < columns.size(); i++) {
if (keys.contains(columns.get(i)))
continue;
sql.append(" T1." + columns.get(i) + "=T2." + columns.get(i) + ",");
}
sql.delete(sql.length() - 1, sql.length());
sql.append(" WHEN NOT MATCHED THEN INSERT (");
for (int i = 0; i < columns.size(); i++) {
sql.append(columns.get(i) + ",");
}
sql.delete(sql.length() - 1, sql.length());
sql.append(") VALUES (");
for (int i = 0; i < columns.size(); i++) {
sql.append("T2." + columns.get(i) + ",");
}
sql.delete(sql.length() - 1, sql.length());
sql.append(")");
return sql.toString();
}
}

你可能感兴趣的:(MyBatis)