【优化】将 Jdbc 的 SQL 构建放在 JdbcSql 中,后续可添加 SQL 的其它子类,用于构建 MyBatis 等不同的 SQL。

This commit is contained in:
2023-06-07 11:51:32 +08:00
parent 6164c1650b
commit 58e085277b
3 changed files with 70 additions and 61 deletions

View File

@@ -0,0 +1,62 @@
package xyz.zhouxy.plusone.commons.jdbc;
import java.util.Collection;
public class JdbcSql extends SQL<JdbcSql> {
JdbcSql() {
super();
}
@Override
public JdbcSql getSelf() {
return this;
}
public static String IN(String col, Collection<?> c) {
return IN(col, c.size());
}
public static <T> String IN(String col, T[] c) {
return IN(col, c.length);
}
private static String IN(String col, int length) {
return new StringBuilder()
.append(col)
.append(" IN (")
.append(buildQuestionsList(length))
.append(')')
.toString();
}
public static String NOT_IN(String col, Collection<?> c) {
return NOT_IN(col, c.size());
}
public static <T> String NOT_IN(String col, T[] c) {
return NOT_IN(col, c.length);
}
private static String NOT_IN(String col, int length) {
return new StringBuilder()
.append(col)
.append(" NOT IN (")
.append(buildQuestionsList(length))
.append(')')
.toString();
}
private static char[] buildQuestionsList(int times) {
char[] arr = new char[times * 3 - 2];
int i = 0;
for (int t = 1; t <= times; t++) {
arr[i++] = '?';
if (t < times) {
arr[i++] = ',';
arr[i++] = ' ';
}
}
return arr;
}
}