Commit b96db6b2 authored by yanzg's avatar yanzg

修改实例化关系

parent c1063d50
...@@ -27,11 +27,15 @@ public class DbPrintSql { ...@@ -27,11 +27,15 @@ public class DbPrintSql {
public String getStringSql(String sql, Object... paras) { public String getStringSql(String sql, Object... paras) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
// 进行SQL语句参数替换,后面增加一个空格,方便后续用正则表达式进行替换处理
String[] split = sql.split("\\?"); String[] split = sql.split("\\?");
int pos = 0; int pos = 0;
while (pos < split.length) {
// 进行SQL语句参数替换,后面增加一个空格,方便后续用正则表达式进行替换处理 sb.append(split[pos]);
for (Object item : paras) { if (pos == split.length - 1) {
break;
}
Object item = paras.length > pos ? paras[pos] : null;
String str = StringHelper.toString(item); String str = StringHelper.toString(item);
String strTo = str; String strTo = str;
if (str == null) { if (str == null) {
...@@ -41,15 +45,11 @@ public class DbPrintSql { ...@@ -41,15 +45,11 @@ public class DbPrintSql {
} else { } else {
strTo = "'" + str.replace("'", "''") + "'"; strTo = "'" + str.replace("'", "''") + "'";
} }
sb.append(split[pos]);
pos++;
sb.append(strTo); sb.append(strTo);
}
while (pos < split.length) {
sb.append(split[pos]);
pos++; pos++;
} }
return sb.toString(); return sb.toString();
} }
......
import com.yanzuoguang.db.impl.DbPrintSql;
import org.junit.Test;
public class TestDbPrintSql {
@Test
public void test(){
DbPrintSql printSql = new DbPrintSql();
String stringSql = printSql.getStringSql("select * from where id = ? AND name = ? and removeFlag = 0 ", "1");
System.out.println(stringSql);
}
}
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