mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix db bug
This commit is contained in:
parent
696a66f8db
commit
a9d8cae6b1
@ -11,6 +11,7 @@
|
|||||||
* 【http 】 修复body方法添加多余头的问题(issue#769@Github)
|
* 【http 】 修复body方法添加多余头的问题(issue#769@Github)
|
||||||
* 【bloomFilter 】修复默认为int类型,左移超过32位后,高位丢失问题(pr#770@Github)
|
* 【bloomFilter 】修复默认为int类型,左移超过32位后,高位丢失问题(pr#770@Github)
|
||||||
* 【core 】 修复beginOfWeek和endOfWeek一周开始计算错误问题(issue#I1BDPW@Gitee)
|
* 【core 】 修复beginOfWeek和endOfWeek一周开始计算错误问题(issue#I1BDPW@Gitee)
|
||||||
|
* 【db 】 修复Db.query使用命名方式查询产生的歧义(issue#776@Github)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -71,6 +71,19 @@ public abstract class AbstractDb implements Serializable {
|
|||||||
*/
|
*/
|
||||||
public abstract void closeConnection(Connection conn);
|
public abstract void closeConnection(Connection conn);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询
|
||||||
|
*
|
||||||
|
* @param sql 查询语句
|
||||||
|
* @param params 参数
|
||||||
|
* @return 结果对象
|
||||||
|
* @throws SQLException SQL执行异常
|
||||||
|
* @since 3.1.1
|
||||||
|
*/
|
||||||
|
public List<Entity> query(String sql, Map<String, Object> params) throws SQLException {
|
||||||
|
return query(sql, new EntityListHandler(), params);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询
|
* 查询
|
||||||
*
|
*
|
||||||
@ -164,7 +177,7 @@ public abstract class AbstractDb implements Serializable {
|
|||||||
* @param paramMap 参数
|
* @param paramMap 参数
|
||||||
* @return 结果对象
|
* @return 结果对象
|
||||||
* @throws SQLException SQL执行异常
|
* @throws SQLException SQL执行异常
|
||||||
* @since 5.1.1
|
* @since 5.2.2
|
||||||
*/
|
*/
|
||||||
public <T> T query(String sql, RsHandler<T> rsh, Map<String, Object> paramMap) throws SQLException {
|
public <T> T query(String sql, RsHandler<T> rsh, Map<String, Object> paramMap) throws SQLException {
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
|
@ -2,9 +2,11 @@ package cn.hutool.db;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.ArrayIter;
|
import cn.hutool.core.collection.ArrayIter;
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.db.sql.NamedSql;
|
||||||
import cn.hutool.db.sql.SqlBuilder;
|
import cn.hutool.db.sql.SqlBuilder;
|
||||||
import cn.hutool.db.sql.SqlLog;
|
import cn.hutool.db.sql.SqlLog;
|
||||||
import cn.hutool.db.sql.SqlUtil;
|
import cn.hutool.db.sql.SqlUtil;
|
||||||
@ -96,7 +98,7 @@ public class StatementUtil {
|
|||||||
* @since 3.2.3
|
* @since 3.2.3
|
||||||
*/
|
*/
|
||||||
public static PreparedStatement prepareStatement(Connection conn, String sql, Collection<Object> params) throws SQLException {
|
public static PreparedStatement prepareStatement(Connection conn, String sql, Collection<Object> params) throws SQLException {
|
||||||
return prepareStatement(conn, sql, params.toArray(new Object[params.size()]));
|
return prepareStatement(conn, sql, params.toArray(new Object[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,8 +113,15 @@ public class StatementUtil {
|
|||||||
*/
|
*/
|
||||||
public static PreparedStatement prepareStatement(Connection conn, String sql, Object... params) throws SQLException {
|
public static PreparedStatement prepareStatement(Connection conn, String sql, Object... params) throws SQLException {
|
||||||
Assert.notBlank(sql, "Sql String must be not blank!");
|
Assert.notBlank(sql, "Sql String must be not blank!");
|
||||||
|
|
||||||
sql = sql.trim();
|
sql = sql.trim();
|
||||||
|
|
||||||
|
if(ArrayUtil.isNotEmpty(params) && 1 == params.length && params[0] instanceof Map){
|
||||||
|
// 检查参数是否为命名方式的参数
|
||||||
|
final NamedSql namedSql = new NamedSql(sql, Convert.toMap(String.class, Object.class, params[0]));
|
||||||
|
sql = namedSql.getSql();
|
||||||
|
params = namedSql.getParams();
|
||||||
|
}
|
||||||
|
|
||||||
SqlLog.INSTANCE.log(sql, ArrayUtil.isEmpty(params) ? null : params);
|
SqlLog.INSTANCE.log(sql, ArrayUtil.isEmpty(params) ? null : params);
|
||||||
PreparedStatement ps;
|
PreparedStatement ps;
|
||||||
if (StrUtil.startWithIgnoreCase(sql, "insert")) {
|
if (StrUtil.startWithIgnoreCase(sql, "insert")) {
|
||||||
@ -135,7 +144,7 @@ public class StatementUtil {
|
|||||||
* @since 4.1.13
|
* @since 4.1.13
|
||||||
*/
|
*/
|
||||||
public static PreparedStatement prepareStatementForBatch(Connection conn, String sql, Object[]... paramsBatch) throws SQLException {
|
public static PreparedStatement prepareStatementForBatch(Connection conn, String sql, Object[]... paramsBatch) throws SQLException {
|
||||||
return prepareStatementForBatch(conn, sql, new ArrayIter<Object[]>(paramsBatch));
|
return prepareStatementForBatch(conn, sql, new ArrayIter<>(paramsBatch));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,7 +50,7 @@ public class NamedSql {
|
|||||||
* @return 参数数组
|
* @return 参数数组
|
||||||
*/
|
*/
|
||||||
public Object[] getParams() {
|
public Object[] getParams() {
|
||||||
return this.params.toArray(new Object[this.params.size()]);
|
return this.params.toArray(new Object[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package cn.hutool.db.sql;
|
package cn.hutool.db.sql;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.ArrayIter;
|
||||||
|
import cn.hutool.db.DbUtil;
|
||||||
|
import cn.hutool.db.StatementUtil;
|
||||||
|
import cn.hutool.db.handler.RsHandler;
|
||||||
|
|
||||||
import java.sql.CallableStatement;
|
import java.sql.CallableStatement;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
@ -8,11 +13,6 @@ import java.sql.SQLException;
|
|||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import cn.hutool.core.collection.ArrayIter;
|
|
||||||
import cn.hutool.db.DbUtil;
|
|
||||||
import cn.hutool.db.StatementUtil;
|
|
||||||
import cn.hutool.db.handler.RsHandler;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SQL执行器,全部为静态方法,执行查询或非查询的SQL语句<br>
|
* SQL执行器,全部为静态方法,执行查询或非查询的SQL语句<br>
|
||||||
* 此方法为JDBC的简单封装,与数据库类型无关
|
* 此方法为JDBC的简单封装,与数据库类型无关
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
package cn.hutool.db;
|
package cn.hutool.db;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.db.sql.NamedSql;
|
import cn.hutool.db.sql.NamedSql;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class NamedSqlTest {
|
public class NamedSqlTest {
|
||||||
|
|
||||||
@ -36,4 +37,19 @@ public class NamedSqlTest {
|
|||||||
Assert.assertEquals("张三", namedSql.getParams()[1]);
|
Assert.assertEquals("张三", namedSql.getParams()[1]);
|
||||||
Assert.assertEquals("小豆豆", namedSql.getParams()[2]);
|
Assert.assertEquals("小豆豆", namedSql.getParams()[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void queryTest() throws SQLException {
|
||||||
|
Map<String, Object> paramMap = MapUtil
|
||||||
|
.builder("name1", (Object)"王五")
|
||||||
|
.put("age1", 18).build();
|
||||||
|
String sql = "select * from user where name = @name1 and age = @age1";
|
||||||
|
|
||||||
|
List<Entity> query = Db.use().query(sql, paramMap);
|
||||||
|
Assert.assertEquals(1, query.size());
|
||||||
|
|
||||||
|
// 采用传统方式查询是否能识别Map类型参数
|
||||||
|
query = Db.use().query(sql, new Object[]{paramMap});
|
||||||
|
Assert.assertEquals(1, query.size());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user