mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add GlobalDbConfig
This commit is contained in:
parent
e50942efbc
commit
84c38aa3f0
@ -6,6 +6,8 @@
|
|||||||
## 5.3.10 (2020-07-14)
|
## 5.3.10 (2020-07-14)
|
||||||
|
|
||||||
### 新特性
|
### 新特性
|
||||||
|
* 【db 】 增加DbUtil.setReturnGeneratedKeyGlobal(issue#I1NM0K@Gitee)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【core 】 修复ZipUtil中finish位于循环内的问题(issue#961@Github)
|
* 【core 】 修复ZipUtil中finish位于循环内的问题(issue#961@Github)
|
||||||
|
|
||||||
|
@ -32,4 +32,13 @@ public class CrcTest {
|
|||||||
crc.update(16);
|
crc.update(16);
|
||||||
Assert.assertEquals("cc04", HexUtil.toHex(crc.getValue()));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ public abstract class AbstractDb implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* 是否大小写不敏感(默认大小写不敏感)
|
* 是否大小写不敏感(默认大小写不敏感)
|
||||||
*/
|
*/
|
||||||
protected boolean caseInsensitive = DbUtil.caseInsensitiveGlobal;
|
protected boolean caseInsensitive = GlobalDbConfig.caseInsensitive;
|
||||||
protected SqlConnRunner runner;
|
protected SqlConnRunner runner;
|
||||||
|
|
||||||
// ------------------------------------------------------- Constructor start
|
// ------------------------------------------------------- Constructor start
|
||||||
|
@ -5,9 +5,7 @@ import cn.hutool.core.io.IoUtil;
|
|||||||
import cn.hutool.db.dialect.Dialect;
|
import cn.hutool.db.dialect.Dialect;
|
||||||
import cn.hutool.db.dialect.DialectFactory;
|
import cn.hutool.db.dialect.DialectFactory;
|
||||||
import cn.hutool.db.ds.DSFactory;
|
import cn.hutool.db.ds.DSFactory;
|
||||||
import cn.hutool.db.sql.SqlLog;
|
|
||||||
import cn.hutool.log.Log;
|
import cn.hutool.log.Log;
|
||||||
import cn.hutool.log.LogFactory;
|
|
||||||
import cn.hutool.log.level.Level;
|
import cn.hutool.log.level.Level;
|
||||||
import cn.hutool.setting.Setting;
|
import cn.hutool.setting.Setting;
|
||||||
|
|
||||||
@ -22,12 +20,7 @@ import java.sql.Connection;
|
|||||||
* @author Luxiaolei
|
* @author Luxiaolei
|
||||||
*/
|
*/
|
||||||
public final class DbUtil {
|
public final class DbUtil {
|
||||||
private final static Log log = LogFactory.get();
|
private final static Log log = Log.get();
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否大小写不敏感(默认大小写不敏感)
|
|
||||||
*/
|
|
||||||
protected static boolean caseInsensitiveGlobal = true;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例化一个新的SQL运行对象
|
* 实例化一个新的SQL运行对象
|
||||||
@ -240,7 +233,7 @@ public final class DbUtil {
|
|||||||
* @since 4.1.7
|
* @since 4.1.7
|
||||||
*/
|
*/
|
||||||
public static void setShowSqlGlobal(boolean isShowSql, boolean isFormatSql, boolean isShowParams, Level level) {
|
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
|
* @since 5.2.4
|
||||||
*/
|
*/
|
||||||
public static void setCaseInsensitiveGlobal(boolean caseInsensitive) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
54
hutool-db/src/main/java/cn/hutool/db/GlobalDbConfig.java
Normal file
54
hutool-db/src/main/java/cn/hutool/db/GlobalDbConfig.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -39,7 +39,7 @@ public class SqlConnRunner implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* 是否大小写不敏感(默认大小写不敏感)
|
* 是否大小写不敏感(默认大小写不敏感)
|
||||||
*/
|
*/
|
||||||
protected boolean caseInsensitive = DbUtil.caseInsensitiveGlobal;
|
protected boolean caseInsensitive = GlobalDbConfig.caseInsensitive;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例化一个新的SQL运行对象
|
* 实例化一个新的SQL运行对象
|
||||||
@ -532,7 +532,7 @@ public class SqlConnRunner implements Serializable {
|
|||||||
|
|
||||||
//查询全部
|
//查询全部
|
||||||
if (null == page) {
|
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());
|
final PageResult<Entity> pageResult = new PageResult<>(0, entityList.size(), entityList.size());
|
||||||
pageResult.addAll(entityList);
|
pageResult.addAll(entityList);
|
||||||
return pageResult;
|
return pageResult;
|
||||||
|
@ -124,7 +124,7 @@ public class StatementUtil {
|
|||||||
|
|
||||||
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 (GlobalDbConfig.returnGeneratedKey && StrUtil.startWithIgnoreCase(sql, "insert")) {
|
||||||
// 插入默认返回主键
|
// 插入默认返回主键
|
||||||
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||||
} else {
|
} else {
|
||||||
|
@ -92,6 +92,13 @@ public class CRUDTest {
|
|||||||
Assert.assertEquals(2, results.size());
|
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
|
@Test
|
||||||
public void findAllTest() throws SQLException {
|
public void findAllTest() throws SQLException {
|
||||||
List<Entity> results = db.findAll("user");
|
List<Entity> results = db.findAll("user");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user