浏览代码

jdbc获取Byte和Short时,把null转换成0了,很多时候0是有意义的,容易引发业务错误

talent-tan 7 年之前
父节点
当前提交
b9ec6700e4

+ 50 - 0
src/main/java/com/jfinal/plugin/activerecord/JdbcKit.java

@@ -0,0 +1,50 @@
+/**
+ * 
+ */
+package com.jfinal.plugin.activerecord;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * @author tanyaowu
+ *
+ */
+public class JdbcKit {
+
+	public static Byte getByte(ResultSet rs, int i) throws SQLException {
+		Object value = rs.getObject(i);
+		if (value != null) {
+			value = Byte.parseByte(value + "");
+			return (Byte) value;
+		} else {
+			return null;
+		}
+	}
+	
+	public static Short getShort(ResultSet rs, int i) throws SQLException {
+		Object value = rs.getObject(i);
+		if (value != null) {
+			value = Short.parseShort(value + "");
+			return (Short) value;
+		} else {
+			return null;
+		}
+	}
+
+	/**
+	 * 
+	 */
+	public JdbcKit() {
+	}
+
+	/**
+	 * @param args
+	 * @author tanyaowu
+	 */
+	public static void main(String[] args) {
+		// TODO Auto-generated method stub
+
+	}
+
+}

+ 3 - 2
src/main/java/com/jfinal/plugin/activerecord/builder/KeepByteAndShortModelBuilder.java

@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import com.jfinal.plugin.activerecord.CPI;
+import com.jfinal.plugin.activerecord.JdbcKit;
 import com.jfinal.plugin.activerecord.Model;
 import com.jfinal.plugin.activerecord.ModelBuilder;
 
@@ -57,9 +58,9 @@ public class KeepByteAndShortModelBuilder extends ModelBuilder {
 				int t = types[i];
 				if (t < Types.DATE) {
 					if (t == Types.TINYINT) {
-						value = rs.getByte(i);
+						value = JdbcKit.getByte(rs, i);
 					} else if (t == Types.SMALLINT) {
-						value = rs.getShort(i);
+						value = JdbcKit.getShort(rs, i);
 					} else {
 						value = rs.getObject(i);
 					}

+ 4 - 2
src/main/java/com/jfinal/plugin/activerecord/builder/KeepByteAndShortRecordBuilder.java

@@ -23,8 +23,10 @@ import java.sql.Types;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+
 import com.jfinal.plugin.activerecord.CPI;
 import com.jfinal.plugin.activerecord.Config;
+import com.jfinal.plugin.activerecord.JdbcKit;
 import com.jfinal.plugin.activerecord.ModelBuilder;
 import com.jfinal.plugin.activerecord.Record;
 import com.jfinal.plugin.activerecord.RecordBuilder;
@@ -60,9 +62,9 @@ public class KeepByteAndShortRecordBuilder extends RecordBuilder {
 				int t = types[i];
 				if (t < Types.DATE) {
 					if (t == Types.TINYINT) {
-						value = rs.getByte(i);
+						value = JdbcKit.getByte(rs, i);
 					} else if (t == Types.SMALLINT) {
-						value = rs.getShort(i);
+						value = JdbcKit.getShort(rs, i);
 					} else {
 						value = rs.getObject(i);
 					}