Browse Source

add GlobalDbConfig

Looly 5 years ago
parent
commit
84c38aa3f0

+ 2 - 0
CHANGELOG.md

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

+ 9 - 0
hutool-core/src/test/java/cn/hutool/core/io/checksum/CrcTest.java

@@ -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);
+	}
 }

+ 1 - 1
hutool-db/src/main/java/cn/hutool/db/AbstractDb.java

@@ -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

+ 15 - 10
hutool-db/src/main/java/cn/hutool/db/DbUtil.java

@@ -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);
 	}
 }

+ 54 - 0
hutool-db/src/main/java/cn/hutool/db/GlobalDbConfig.java

@@ -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);
+	}
+}

+ 2 - 2
hutool-db/src/main/java/cn/hutool/db/SqlConnRunner.java

@@ -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;

+ 1 - 1
hutool-db/src/main/java/cn/hutool/db/StatementUtil.java

@@ -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 {

+ 7 - 0
hutool-db/src/test/java/cn/hutool/db/CRUDTest.java

@@ -93,6 +93,13 @@ public class CRUDTest {
 	}
 
 	@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");
 		Assert.assertEquals(4, results.size());