forked from plusone/plusone-commons
【优化】将 Jdbc 的 SQL 构建放在 JdbcSql 中,后续可添加 SQL 的其它子类,用于构建 MyBatis 等不同的 SQL。
parent
6164c1650b
commit
58e085277b
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,8 +16,6 @@
|
||||||
|
|
||||||
package xyz.zhouxy.plusone.commons.jdbc;
|
package xyz.zhouxy.plusone.commons.jdbc;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import org.apache.ibatis.jdbc.AbstractSQL;
|
import org.apache.ibatis.jdbc.AbstractSQL;
|
||||||
|
|
||||||
import com.google.common.annotations.Beta;
|
import com.google.common.annotations.Beta;
|
||||||
|
@ -26,68 +24,16 @@ import com.google.common.annotations.Beta;
|
||||||
* @author ZhouXY
|
* @author ZhouXY
|
||||||
*/
|
*/
|
||||||
@Beta
|
@Beta
|
||||||
public class SQL extends AbstractSQL<SQL> {
|
public abstract class SQL<T> extends AbstractSQL<T> {
|
||||||
|
|
||||||
@Override
|
public static JdbcSql newJdbcSql() {
|
||||||
public SQL getSelf() {
|
return new JdbcSql();
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SQL newSql() {
|
public T WHERE(boolean condition, String sqlConditions) {
|
||||||
return new SQL();
|
|
||||||
}
|
|
||||||
|
|
||||||
public SQL WHERE(boolean condition, String sqlConditions) {
|
|
||||||
if (condition) {
|
if (condition) {
|
||||||
return WHERE(sqlConditions);
|
return WHERE(sqlConditions);
|
||||||
}
|
}
|
||||||
return getSelf();
|
return getSelf();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
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 String 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 String.valueOf(arr);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,7 +3,7 @@ package xyz.zhouxy.plusone.commons.util;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static xyz.zhouxy.plusone.commons.jdbc.SQL.*;
|
import static xyz.zhouxy.plusone.commons.jdbc.JdbcSql.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -27,6 +27,7 @@ import com.zaxxer.hikari.HikariConfig;
|
||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.jdbc.DbRecord;
|
import xyz.zhouxy.plusone.commons.jdbc.DbRecord;
|
||||||
|
import xyz.zhouxy.plusone.commons.jdbc.SQL;
|
||||||
import xyz.zhouxy.plusone.commons.jdbc.SimpleJdbcTemplate;
|
import xyz.zhouxy.plusone.commons.jdbc.SimpleJdbcTemplate;
|
||||||
|
|
||||||
class SimpleJdbcTemplateTests {
|
class SimpleJdbcTemplateTests {
|
||||||
|
@ -56,7 +57,7 @@ class SimpleJdbcTemplateTests {
|
||||||
void testQuery() throws SQLException {
|
void testQuery() throws SQLException {
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
Object[] params = SimpleJdbcTemplate.buildParams("501533", "501554", "544599");
|
Object[] params = SimpleJdbcTemplate.buildParams("501533", "501554", "544599");
|
||||||
String sql = newSql()
|
String sql = SQL.newJdbcSql()
|
||||||
.SELECT("*")
|
.SELECT("*")
|
||||||
.FROM("test_table")
|
.FROM("test_table")
|
||||||
.WHERE(NOT_IN("id", params))
|
.WHERE(NOT_IN("id", params))
|
||||||
|
|
Loading…
Reference in New Issue