remove deprecated methods

This commit is contained in:
Looly
2021-06-16 00:51:20 +08:00
parent 8b85134c4b
commit f9b6110042
59 changed files with 214 additions and 2185 deletions

View File

@@ -52,42 +52,6 @@ public final class DbUtil {
return SqlConnRunner.create(DialectFactory.newDialect(conn));
}
/**
* 实例化一个新的SQL运行对象使用默认数据源
*
* @return SQL执行类
* @deprecated 请使用 {@link #use()}
*/
@Deprecated
public static SqlRunner newSqlRunner() {
return SqlRunner.create(getDs());
}
/**
* 实例化一个新的SQL运行对象
*
* @param ds 数据源
* @return SQL执行类
* @deprecated 请使用 {@link #use(DataSource)}
*/
@Deprecated
public static SqlRunner newSqlRunner(DataSource ds) {
return SqlRunner.create(ds);
}
/**
* 实例化一个新的SQL运行对象
*
* @param ds 数据源
* @param dialect SQL方言
* @return SQL执行类
* @deprecated 请使用 {@link #use(DataSource, Dialect)}
*/
@Deprecated
public static SqlRunner newSqlRunner(DataSource ds, Dialect dialect) {
return SqlRunner.create(ds, dialect);
}
/**
* 实例化一个新的Db使用默认数据源
*

View File

@@ -96,26 +96,6 @@ public class Page implements Segment<Integer>, Serializable {
this.pageNumber = Math.max(pageNumber, 0);
}
/**
* @return 每页结果数
* @deprecated 使用 {@link #getPageSize()} 代替
*/
@Deprecated
public int getNumPerPage() {
return getPageSize();
}
/**
* 设置每页结果数
*
* @param pageSize 每页结果数
* @deprecated 使用 {@link #setPageSize(int)} 代替
*/
@Deprecated
public void setNumPerPage(int pageSize) {
setPageSize(pageSize);
}
/**
* @return 每页结果数
*/

View File

@@ -89,26 +89,6 @@ public class PageResult<T> extends ArrayList<T> {
this.page = page;
}
/**
* @return 每页结果数
* @deprecated 请使用{@link #getPageSize()}
*/
@Deprecated
public int getNumPerPage() {
return pageSize;
}
/**
* 设置每页结果数
*
* @param pageSize 每页结果数
* @deprecated 请使用 {@link #setPageSize(int)}
*/
@Deprecated
public void setNumPerPage(int pageSize) {
this.pageSize = pageSize;
}
/**
* @return 每页结果数
*/

View File

@@ -20,7 +20,7 @@ import java.sql.Savepoint;
* 会话通过共用Connection而可以实现JDBC事务<br>
* 一个会话只维护一个连接推荐在执行完后关闭Session避免重用<br>
* 本对象并不是线程安全的多个线程共用一个Session将会导致不可预知的问题
*
*
* @author loolly
*
*/
@@ -30,17 +30,17 @@ public class Session extends AbstractDb implements Closeable {
/**
* 创建默认数据源会话
*
*
* @return Session
* @since 3.2.3
*/
public static Session create() {
return new Session(DSFactory.get());
}
/**
* 创建会话
*
*
* @param group 分组
* @return Session
* @since 4.0.11
@@ -51,7 +51,7 @@ public class Session extends AbstractDb implements Closeable {
/**
* 创建会话
*
*
* @param ds 数据源
* @return Session
*/
@@ -62,16 +62,16 @@ public class Session extends AbstractDb implements Closeable {
// ---------------------------------------------------------------------------- Constructor start
/**
* 构造从DataSource中识别方言
*
*
* @param ds 数据源
*/
public Session(DataSource ds) {
this(ds, DialectFactory.getDialect(ds));
}
/**
* 构造
*
*
* @param ds 数据源
* @param driverClassName 数据库连接驱动类名,用于识别方言
*/
@@ -81,7 +81,7 @@ public class Session extends AbstractDb implements Closeable {
/**
* 构造
*
*
* @param ds 数据源
* @param dialect 方言
*/
@@ -93,7 +93,7 @@ public class Session extends AbstractDb implements Closeable {
// ---------------------------------------------------------------------------- Getters and Setters end
/**
* 获得{@link SqlConnRunner}
*
*
* @return {@link SqlConnRunner}
*/
@Override
@@ -105,7 +105,7 @@ public class Session extends AbstractDb implements Closeable {
// ---------------------------------------------------------------------------- Transaction method start
/**
* 开始事务
*
*
* @throws SQLException SQL执行异常
*/
public void beginTransaction() throws SQLException {
@@ -116,7 +116,7 @@ public class Session extends AbstractDb implements Closeable {
/**
* 提交事务
*
*
* @throws SQLException SQL执行异常
*/
public void commit() throws SQLException {
@@ -133,7 +133,7 @@ public class Session extends AbstractDb implements Closeable {
/**
* 回滚事务
*
*
* @throws SQLException SQL执行异常
*/
public void rollback() throws SQLException {
@@ -168,7 +168,7 @@ public class Session extends AbstractDb implements Closeable {
/**
* 回滚到某个保存点保存点的设置请使用setSavepoint方法
*
*
* @param savepoint 保存点
* @throws SQLException SQL执行异常
*/
@@ -186,7 +186,7 @@ public class Session extends AbstractDb implements Closeable {
/**
* 静默回滚到某个保存点保存点的设置请使用setSavepoint方法
*
*
* @param savepoint 保存点
*/
public void quietRollback(Savepoint savepoint) {
@@ -205,7 +205,7 @@ public class Session extends AbstractDb implements Closeable {
/**
* 设置保存点
*
*
* @return 保存点对象
* @throws SQLException SQL执行异常
*/
@@ -215,7 +215,7 @@ public class Session extends AbstractDb implements Closeable {
/**
* 设置保存点
*
*
* @param name 保存点的名称
* @return 保存点对象
* @throws SQLException SQL执行异常
@@ -226,13 +226,13 @@ public class Session extends AbstractDb implements Closeable {
/**
* 设置事务的隔离级别<br>
*
*
* Connection.TRANSACTION_NONE 驱动不支持事务<br>
* Connection.TRANSACTION_READ_UNCOMMITTED 允许脏读、不可重复读和幻读<br>
* Connection.TRANSACTION_READ_COMMITTED 禁止脏读,但允许不可重复读和幻读<br>
* Connection.TRANSACTION_REPEATABLE_READ 禁止脏读和不可重复读,单运行幻读<br>
* Connection.TRANSACTION_SERIALIZABLE 禁止脏读、不可重复读和幻读<br>
*
*
* @param level 隔离级别
* @throws SQLException SQL执行异常
*/
@@ -242,10 +242,10 @@ public class Session extends AbstractDb implements Closeable {
}
getConnection().setTransactionIsolation(level);
}
/**
* 在事务中执行操作,通过实现{@link VoidFunc1}接口的call方法执行多条SQL语句从而完成事务
*
*
* @param func 函数抽象在函数中执行多个SQL操作多个操作会被合并为同一事务
* @throws SQLException SQL异常
* @since 3.2.3
@@ -261,24 +261,6 @@ public class Session extends AbstractDb implements Closeable {
}
}
/**
* 在事务中执行操作,通过实现{@link VoidFunc1}接口的call方法执行多条SQL语句从而完成事务
*
* @param func 函数抽象在函数中执行多个SQL操作多个操作会被合并为同一事务
* @since 3.2.3
* @deprecated 请使用{@link #tx(VoidFunc1)}
*/
@Deprecated
public void trans(VoidFunc1<Session> func) {
try {
beginTransaction();
func.call(this);
commit();
} catch (Exception e) {
quietRollback();
throw new DbRuntimeException(e);
}
}
// ---------------------------------------------------------------------------- Transaction method end
// ---------------------------------------------------------------------------- Getters and Setters start
@@ -291,7 +273,7 @@ public class Session extends AbstractDb implements Closeable {
public Session setWrapper(Wrapper wrapper) {
return (Session) super.setWrapper(wrapper);
}
@Override
public Session disableWrapper() {
return (Session) super.disableWrapper();
@@ -313,7 +295,7 @@ public class Session extends AbstractDb implements Closeable {
} catch (SQLException e) {
log.error(e);
}
// 普通请求关闭(或归还)连接
ThreadLocalConnection.INSTANCE.close(this.ds);
}

View File

@@ -111,18 +111,6 @@ public abstract class DSFactory implements Closeable, Serializable{
return GlobalDSFactory.get().getDataSource(group);
}
/**
* 根据Setting获取当前数据源工厂对象
*
* @param setting 数据源配置文件
* @return 当前使用的数据源工厂
* @deprecated 此方法容易引起歧义,应使用{@link #create(Setting)} 方法代替之
*/
@Deprecated
public static DSFactory getCurrentDSFactory(Setting setting) {
return create(setting);
}
/**
* 设置全局的数据源工厂<br>
* 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br>

View File

@@ -62,19 +62,6 @@ public class Column implements Serializable, Cloneable {
private boolean isPk;
// ----------------------------------------------------- Fields end
/**
* 创建列对象
*
* @param tableName 表名
* @param columnMetaRs 列元信息的ResultSet
* @return 列对象
* @deprecated 请使用 {@link #create(Table, ResultSet)}
*/
@Deprecated
public static Column create(String tableName, ResultSet columnMetaRs) {
return new Column(tableName, columnMetaRs);
}
/**
* 创建列对象
*
@@ -95,22 +82,6 @@ public class Column implements Serializable, Cloneable {
public Column() {
}
/**
* 构造
*
* @param tableName 表名
* @param columnMetaRs Meta信息的ResultSet
* @deprecated 请使用 {@link #Column(Table, ResultSet)}
*/
@Deprecated
public Column(String tableName, ResultSet columnMetaRs) {
try {
init(tableName, columnMetaRs);
} catch (SQLException e) {
throw new DbRuntimeException(StrUtil.format("Get table [{}] meta info error!", tableName));
}
}
/**
* 构造
*
@@ -127,19 +98,6 @@ public class Column implements Serializable, Cloneable {
}
// ----------------------------------------------------- Constructor end
/**
* 初始化
*
* @param tableName 表名
* @param columnMetaRs 列的meta ResultSet
* @throws SQLException SQL执行异常
* @deprecated 请使用 {@link #init(Table, ResultSet)}
*/
@Deprecated
public void init(String tableName, ResultSet columnMetaRs) throws SQLException {
init(Table.create(tableName), columnMetaRs);
}
/**
* 初始化
*

View File

@@ -373,7 +373,7 @@ public class Condition extends CloneSupport<Condition> {
}
// 处理 AND y
conditionStrBuilder.append(StrUtil.SPACE).append(LogicalOperator.AND.toString());
conditionStrBuilder.append(StrUtil.SPACE).append(LogicalOperator.AND);
if (isPlaceHolder()) {
// 使用条件表达式占位符
conditionStrBuilder.append(" ?");

View File

@@ -17,7 +17,7 @@ import java.util.Map;
* 2、@name
* 3、?name
* </pre>
*
*
* @author looly
* @since 4.0.10
*/
@@ -30,7 +30,7 @@ public class NamedSql {
/**
* 构造
*
*
* @param namedSql 命名占位符的SQL
* @param paramMap 名和参数的对应Map
*/
@@ -41,16 +41,16 @@ public class NamedSql {
/**
* 获取SQL
*
*
* @return SQL
*/
public String getSql() {
return this.sql;
}
/**
* 获取参数列表,按照占位符顺序
*
*
* @return 参数数组
*/
public Object[] getParams() {
@@ -59,7 +59,7 @@ public class NamedSql {
/**
* 获取参数列表,按照占位符顺序
*
*
* @return 参数列表
*/
public List<Object> getParamList() {
@@ -68,7 +68,7 @@ public class NamedSql {
/**
* 解析命名占位符的SQL
*
*
* @param namedSql 命名占位符的SQL
* @param paramMap 名和参数的对应Map
*/
@@ -138,8 +138,20 @@ public class NamedSql {
if(paramMap.containsKey(nameStr)) {
// 有变量对应值值可以为null替换占位符为?变量值放入相应index位置
final Object paramValue = paramMap.get(nameStr);
sqlBuilder.append('?');
this.params.add(paramValue);
if(ArrayUtil.isArray(paramValue) && StrUtil.contains(sqlBuilder, "in")){
// 可能为select in (xxx)语句则拆分参数为多个参数变成in (?,?,?)
final int length = ArrayUtil.length(paramValue);
for (int i = 0; i < length; i++) {
if(0 != i){
sqlBuilder.append(',');
}
sqlBuilder.append('?');
this.params.add(ArrayUtil.get(paramValue, i));
}
} else{
sqlBuilder.append('?');
this.params.add(paramValue);
}
} else {
// 无变量对应值,原样输出
sqlBuilder.append(nameStartChar).append(name);
@@ -151,7 +163,7 @@ public class NamedSql {
/**
* 是否为标准的字符,包括大小写字母、下划线和数字
*
*
* @param c 字符
* @return 是否标准字符
*/

View File

@@ -335,20 +335,6 @@ public class SqlBuilder implements Builder<String> {
return this;
}
/**
* 添加Where语句<br>
* 只支持单一的逻辑运算符(例如多个条件之间)
*
* @param logicalOperator 逻辑运算符
* @param conditions 条件当条件为空时只添加WHERE关键字
* @return 自己
* @deprecated logicalOperator放在Condition中了因此请使用 {@link #where(Condition...)}
*/
@Deprecated
public SqlBuilder where(LogicalOperator logicalOperator, Condition... conditions) {
return where(conditions);
}
/**
* 添加Where语句<br>
*
@@ -395,19 +381,6 @@ public class SqlBuilder implements Builder<String> {
return this;
}
/**
* 添加Having语句
*
* @param logicalOperator 逻辑运算符
* @param conditions 条件
* @return 自己
* @deprecated logicalOperator放在Condition中了因此请使用 {@link #having(Condition...)}
*/
@Deprecated
public SqlBuilder having(LogicalOperator logicalOperator, Condition... conditions) {
return having(conditions);
}
/**
* 添加Having语句所有逻辑之间关系使用{@link Condition#setLinkOperator(LogicalOperator)} 定义
*
@@ -498,20 +471,6 @@ public class SqlBuilder implements Builder<String> {
return this;
}
/**
* 配合JOIN的 ON语句多表关联的条件语句<br>
* 只支持单一的逻辑运算符(例如多个条件之间)
*
* @param logicalOperator 逻辑运算符
* @param conditions 条件
* @return 自己
* @deprecated logicalOperator放在Condition中了因此请使用 {@link #on(Condition...)}
*/
@Deprecated
public SqlBuilder on(LogicalOperator logicalOperator, Condition... conditions) {
return on(conditions);
}
/**
* 配合JOIN的 ON语句多表关联的条件语句所有逻辑之间关系使用{@link Condition#setLinkOperator(LogicalOperator)} 定义
*
@@ -602,7 +561,7 @@ public class SqlBuilder implements Builder<String> {
* @return this
*/
public SqlBuilder query(Query query) {
return this.select(query.getFields()).from(query.getTableNames()).where(LogicalOperator.AND, query.getWhere());
return this.select(query.getFields()).from(query.getTableNames()).where(query.getWhere());
}
// --------------------------------------------------------------- Builder end

View File

@@ -26,19 +26,27 @@ import java.util.Calendar;
/**
* {@link PreparedStatement} 包装类,用于添加拦截方法功能<br>
* 拦截方法包括:
*
*
* <pre>
* 1. 提供参数注入
* 2. 提供SQL打印日志拦截
* </pre>
*
*
* @author looly
* @since 4.1.0
*/
public class StatementWrapper implements PreparedStatement {
private PreparedStatement rawStatement;
/**
* 构造
*
* @param rawStatement {@link PreparedStatement}
*/
public StatementWrapper(PreparedStatement rawStatement) {
this.rawStatement = rawStatement;
}
@Override
public ResultSet executeQuery(String sql) throws SQLException {

View File

@@ -2,6 +2,7 @@ package cn.hutool.db;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.pojo.User;
import cn.hutool.db.sql.Condition;
@@ -189,4 +190,11 @@ public class CRUDTest {
int[] result = db.insert(CollUtil.newArrayList(data1));
Console.log(result);
}
@Test
public void selectInTest() throws SQLException {
final List<Entity> results = db.query("select * from user where id in (:ids)",
MapUtil.of("ids", new int[]{1, 2, 3}));
Assert.assertEquals(2, results.size());
}
}

View File

@@ -6,6 +6,7 @@ import org.junit.Assert;
import org.junit.Test;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -27,18 +28,18 @@ public class NamedSqlTest {
Assert.assertEquals("张三", namedSql.getParams()[0]);
Assert.assertEquals("小豆豆", namedSql.getParams()[1]);
}
@Test
public void parseTest2() {
String sql = "select * from table where id=@id and name = @name1 and nickName = :subName";
Map<String, Object> paramMap = MapUtil
.builder("name1", (Object)"张三")
.put("age", 12)
.put("subName", "小豆豆")
.put("id", null)
.build();
NamedSql namedSql = new NamedSql(sql, paramMap);
Assert.assertEquals("select * from table where id=? and name = ? and nickName = ?", namedSql.getSql());
//指定了null参数的依旧替换参数值为null
@@ -73,6 +74,18 @@ public class NamedSqlTest {
Assert.assertEquals(sql, namedSql.getSql());
}
@Test
public void parseInTest(){
String sql = "select * from user where id in (:ids)";
final HashMap<String, Object> paramMap = MapUtil.of("ids", new int[]{1, 2, 3});
NamedSql namedSql = new NamedSql(sql, paramMap);
Assert.assertEquals("select * from user where id in (?,?,?)", namedSql.getSql());
Assert.assertEquals(1, namedSql.getParams()[0]);
Assert.assertEquals(2, namedSql.getParams()[1]);
Assert.assertEquals(3, namedSql.getParams()[2]);
}
@Test
public void queryTest() throws SQLException {
Map<String, Object> paramMap = MapUtil