add GlobalDbConfig

This commit is contained in:
Looly 2020-07-15 17:29:39 +08:00
parent e50942efbc
commit 84c38aa3f0
8 changed files with 91 additions and 14 deletions

View File

@ -6,6 +6,8 @@
## 5.3.10 (2020-07-14)
### 新特性
* 【db 】 增加DbUtil.setReturnGeneratedKeyGlobalissue#I1NM0K@Gitee
### Bug修复
* 【core 】 修复ZipUtil中finish位于循环内的问题issue#961@Github

View File

@ -32,4 +32,13 @@ public class CrcTest {
crc.update(16);
Assert.assertEquals("cc04", HexUtil.toHex(crc.getValue()));
}
@Test
public void crc16Test2() {
String str = "QN=20160801085857223;ST=23;CN=2011;PW=123456;MN=010000A8900016F000169DC0;Flag=5;CP=&&DataTime=20160801085857; LA-Rtd=50.1&&";
CRC16 crc = new CRC16();
crc.update(str.getBytes(), 0, str.getBytes().length);
String crc16 = HexUtil.toHex(crc.getValue());
Assert.assertEquals("18c", crc16);
}
}

View File

@ -41,7 +41,7 @@ public abstract class AbstractDb implements Serializable {
/**
* 是否大小写不敏感默认大小写不敏感
*/
protected boolean caseInsensitive = DbUtil.caseInsensitiveGlobal;
protected boolean caseInsensitive = GlobalDbConfig.caseInsensitive;
protected SqlConnRunner runner;
// ------------------------------------------------------- Constructor start

View File

@ -5,9 +5,7 @@ import cn.hutool.core.io.IoUtil;
import cn.hutool.db.dialect.Dialect;
import cn.hutool.db.dialect.DialectFactory;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.sql.SqlLog;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.log.level.Level;
import cn.hutool.setting.Setting;
@ -22,12 +20,7 @@ import java.sql.Connection;
* @author Luxiaolei
*/
public final class DbUtil {
private final static Log log = LogFactory.get();
/**
* 是否大小写不敏感默认大小写不敏感
*/
protected static boolean caseInsensitiveGlobal = true;
private final static Log log = Log.get();
/**
* 实例化一个新的SQL运行对象
@ -240,7 +233,7 @@ public final class DbUtil {
* @since 4.1.7
*/
public static void setShowSqlGlobal(boolean isShowSql, boolean isFormatSql, boolean isShowParams, Level level) {
SqlLog.INSTANCE.init(isShowSql, isFormatSql, isShowParams, level);
GlobalDbConfig.setShowSql(isShowSql, isFormatSql, isShowParams, level);
}
/**
@ -251,6 +244,18 @@ public final class DbUtil {
* @since 5.2.4
*/
public static void setCaseInsensitiveGlobal(boolean caseInsensitive) {
caseInsensitiveGlobal = caseInsensitive;
GlobalDbConfig.setCaseInsensitive(caseInsensitive);
}
/**
* 设置全局是否INSERT语句中默认返回主键默认返回主键<br>
* 如果false则在Insert操作后返回影响行数
* 主要用于某些数据库不支持返回主键的情况
*
* @param returnGeneratedKey 是否INSERT语句中默认返回主键
* @since 5.3.10
*/
public static void setReturnGeneratedKeyGlobal(boolean returnGeneratedKey) {
GlobalDbConfig.setReturnGeneratedKey(returnGeneratedKey);
}
}

View File

@ -0,0 +1,54 @@
package cn.hutool.db;
import cn.hutool.db.sql.SqlLog;
import cn.hutool.log.level.Level;
/**
* DB全局配置配置项
*
* @author looly
* @since 5.3.10
*/
public class GlobalDbConfig {
/**
* 是否大小写不敏感默认大小写不敏感
*/
protected static boolean caseInsensitive = true;
/**
* 是否INSERT语句中默认返回主键默认返回主键
*/
protected static boolean returnGeneratedKey = true;
/**
* 设置全局是否在结果中忽略大小写<br>
* 如果忽略则在Entity中调用getXXX时字段值忽略大小写默认忽略
*
* @param isCaseInsensitive 否在结果中忽略大小写
*/
public static void setCaseInsensitive(boolean isCaseInsensitive) {
caseInsensitive = isCaseInsensitive;
}
/**
* 设置全局是否INSERT语句中默认返回主键默认返回主键<br>
* 如果false则在Insert操作后返回影响行数
* 主要用于某些数据库不支持返回主键的情况
*
* @param isReturnGeneratedKey 是否INSERT语句中默认返回主键
*/
public static void setReturnGeneratedKey(boolean isReturnGeneratedKey) {
returnGeneratedKey = isReturnGeneratedKey;
}
/**
* 设置全局配置是否通过debug日志显示SQL
*
* @param isShowSql 是否显示SQL
* @param isFormatSql 是否格式化显示的SQL
* @param isShowParams 是否打印参数
* @param level SQL打印到的日志等级
*/
public static void setShowSql(boolean isShowSql, boolean isFormatSql, boolean isShowParams, Level level) {
SqlLog.INSTANCE.init(isShowSql, isFormatSql, isShowParams, level);
}
}

View File

@ -39,7 +39,7 @@ public class SqlConnRunner implements Serializable {
/**
* 是否大小写不敏感默认大小写不敏感
*/
protected boolean caseInsensitive = DbUtil.caseInsensitiveGlobal;
protected boolean caseInsensitive = GlobalDbConfig.caseInsensitive;
/**
* 实例化一个新的SQL运行对象
@ -532,7 +532,7 @@ public class SqlConnRunner implements Serializable {
//查询全部
if (null == page) {
List<Entity> entityList = this.find(conn, fields, where, new EntityListHandler(DbUtil.caseInsensitiveGlobal));
List<Entity> entityList = this.find(conn, fields, where, new EntityListHandler(GlobalDbConfig.caseInsensitive));
final PageResult<Entity> pageResult = new PageResult<>(0, entityList.size(), entityList.size());
pageResult.addAll(entityList);
return pageResult;

View File

@ -124,7 +124,7 @@ public class StatementUtil {
SqlLog.INSTANCE.log(sql, ArrayUtil.isEmpty(params) ? null : params);
PreparedStatement ps;
if (StrUtil.startWithIgnoreCase(sql, "insert")) {
if (GlobalDbConfig.returnGeneratedKey && StrUtil.startWithIgnoreCase(sql, "insert")) {
// 插入默认返回主键
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
} else {

View File

@ -92,6 +92,13 @@ public class CRUDTest {
Assert.assertEquals(2, results.size());
}
@Test
public void findInTest3() throws SQLException {
List<Entity> results = db.findAll(Entity.create("user")
.set("id", new long[]{1, 2, 3}));
Assert.assertEquals(2, results.size());
}
@Test
public void findAllTest() throws SQLException {
List<Entity> results = db.findAll("user");