Browse Source

fix timestamp bug

Looly 6 years ago
parent
commit
4663e0d175

+ 1 - 0
CHANGELOG.md

@@ -17,6 +17,7 @@
 
 ### Bug修复
 * 【core   】     修复TypeUtil无法获取泛型接口的泛型参数问题(issue#I1BRFI@Gitee)
+* 【core   】     修复MySQL中0000报错问题
 
 -------------------------------------------------------------------------------------------------------------
 ## 5.2.3

+ 56 - 5
hutool-core/src/main/java/cn/hutool/core/lang/tree/Tree.java

@@ -3,6 +3,7 @@ package cn.hutool.core.lang.tree;
 import cn.hutool.core.lang.Assert;
 import cn.hutool.core.util.ObjectUtil;
 
+import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 
@@ -45,6 +46,57 @@ public class Tree<T> extends LinkedHashMap<String, Object> implements Comparable
 	}
 
 	/**
+	 * 获取ID对应的节点,如果有多个ID相同的节点,只返回第一个。<br>
+	 * 此方法只查找此节点及子节点,采用广度优先遍历。
+	 *
+	 * @param id ID
+	 * @return 节点
+	 * @since 5.2.4
+	 */
+	public Tree<T> getNode(T id) {
+		if (ObjectUtil.equal(id, getId())) {
+			return this;
+		}
+
+		// 查找子节点
+		Tree<T> node;
+		for (Tree<T> child : getChildren()) {
+			node = child.getNode(id);
+			if (null != node) {
+				return node;
+			}
+		}
+
+		// 未找到节点
+		return null;
+	}
+
+	/**
+	 * 获取所有父节点名称列表
+	 *
+	 * <p>
+	 * 比如有个人在研发1部,他上面有研发部,接着上面有技术中心<br>
+	 * 返回结果就是:[研发一部, 研发中心, 技术中心]
+	 *
+	 * @param includeCurrentNode 是否包含当前节点的名称
+	 * @return 所有父节点名称列表
+	 * @since 5.2.4
+	 */
+	public List<CharSequence> getParentsName(boolean includeCurrentNode) {
+		final List<CharSequence> result = new ArrayList<>();
+		if (includeCurrentNode) {
+			result.add(this.getName());
+		}
+
+		Tree<T> parent = getParent();
+		while (null != parent) {
+			result.add(parent.getName());
+			parent = parent.getParent();
+		}
+		return result;
+	}
+
+	/**
 	 * 设置父节点
 	 *
 	 * @param parent 父节点
@@ -52,7 +104,7 @@ public class Tree<T> extends LinkedHashMap<String, Object> implements Comparable
 	 */
 	public Tree<T> setParent(Tree<T> parent) {
 		this.parent = parent;
-		if(null != parent){
+		if (null != parent) {
 			this.setParentId(parent.getId());
 		}
 		return this;
@@ -94,12 +146,11 @@ public class Tree<T> extends LinkedHashMap<String, Object> implements Comparable
 		return this;
 	}
 
-	@SuppressWarnings("unchecked")
-	public T getName() {
-		return (T) this.get(treeNodeConfig.getNameKey());
+	public CharSequence getName() {
+		return (CharSequence) this.get(treeNodeConfig.getNameKey());
 	}
 
-	public Tree<T> setName(Object name) {
+	public Tree<T> setName(CharSequence name) {
 		this.put(treeNodeConfig.getNameKey(), name);
 		return this;
 	}

+ 2 - 3
hutool-db/src/main/java/cn/hutool/db/handler/HandleHelper.java

@@ -265,15 +265,14 @@ public class HandleHelper {
 	 * @throws SQLException SQL异常
 	 */
 	private static <T> Object getColumnValue(ResultSet rs, int columnIndex, int type, Type targetColumnType) throws SQLException {
-		Object rawValue;
+		Object rawValue = null;
 		switch (type) {
 		case Types.TIMESTAMP:
 			try{
 				rawValue = rs.getTimestamp(columnIndex);
 			} catch (SQLException ignore){
 				// issue#776@Github
-				// 当数据库中日期为0000-00-00 00:00:00报错,按照普通日期获取
-				rawValue = rs.getDate(columnIndex);
+				// 当数据库中日期为0000-00-00 00:00:00报错,转为null
 			}
 			break;
 		case Types.TIME:

+ 10 - 2
hutool-db/src/test/java/cn/hutool/db/MySQLTest.java

@@ -5,6 +5,7 @@ import org.junit.Ignore;
 import org.junit.Test;
 
 import java.sql.SQLException;
+import java.util.List;
 
 /**
  * MySQL操作单元测试
@@ -26,11 +27,11 @@ public class MySQLTest {
 			);
 		}
 	}
-	
+
 	/**
 	 * 事务测试<br>
 	 * 更新三条信息,低2条后抛出异常,正常情况下三条都应该不变
-	 * 
+	 *
 	 * @throws SQLException SQL异常
 	 */
 	@Test(expected=SQLException.class)
@@ -56,4 +57,11 @@ public class MySQLTest {
 		}
 	}
 
+	@Test
+	@Ignore
+	public void getTimeStampTest() throws SQLException {
+		final List<Entity> all = Db.use("mysql").findAll("test");
+		Console.log(all);
+	}
+
 }

+ 1 - 1
hutool-db/src/test/resources/config/db.setting

@@ -37,7 +37,7 @@ user = looly
 pass = 123456
 
 [mysql]
-url = jdbc:mysql://looly.centos:3306/test_hutool?useSSL=false
+url = jdbc:mysql://looly.centos8:3306/hutool_test?useSSL=false
 user = root
 pass = 123456