MySQL bigint types and iBATIS
One nuance I recently ran into while using iBATIS was inserting data into MySQL bigint unsigned columns. iBATIS doesn’t seem to have a way to handle BigInteger data types and throws an exception when attempting to do an insert. Fetching data out seemed to work OK because if iBATIS doesn’t know how to handle a certain type it just returns a java.lang.Object. The way to go about inserting BigInteger types is to set up a type handler. Here’s an example type handler for BigInteger types:
package org.qnot.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.SQLException;
import java.sql.Types;
import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
public class BigIntegerTypeHandler implements TypeHandlerCallback {
public Object getResult(ResultGetter getter) throws SQLException {
if(getter.wasNull()) {
return null;
}
Object o = getter.getObject();
if(o instanceof BigDecimal) {
BigDecimal bd = (BigDecimal)o;
return bd.toBigInteger();
} else if(o instanceof BigInteger) {
return (BigInteger)o;
} else {
return o;
}
}
public void setParameter(ParameterSetter setter, Object parameter)
throws SQLException {
if (parameter == null) {
setter.setNull(Types.BIGINT);
} else {
BigInteger i = (BigInteger) parameter;
setter.setBigDecimal(new BigDecimal(i));
}
}
public Object valueOf(String s) {
return s;
}
}
Then all you have to do is register the type handler in your sqlmap-config.xml file like so:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true" />
<typeHandler javaType="java.math.BigInteger"
callback="org.qnot.util.BigIntegerTypeHandler"/>
<sqlMap resource="sql/example.xml"/>
</sqlMapConfig>
One nuance I recently ran into while using iBATIS was inserting data into MySQL bigint unsigned columns. iBATIS doesn’t seem to have a way to handle BigInteger data types and throws an exception when attempting to do an insert. Fetching data out seemed to work OK because if iBATIS doesn’t know how to handle a certain type it just returns a java.lang.Object. The way to go about inserting BigInteger types is to set up a type handler. Here’s an example type handler for BigInteger types:
package org.qnot.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.SQLException;
import java.sql.Types;
import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
public class BigIntegerTypeHandler implements TypeHandlerCallback {
public Object getResult(ResultGetter getter) throws SQLException {
if(getter.wasNull()) {
return null;
}
Object o = getter.getObject();
if(o instanceof BigDecimal) {
BigDecimal bd = (BigDecimal)o;
return bd.toBigInteger();
} else if(o instanceof BigInteger) {
return (BigInteger)o;
} else {
return o;
}
}
public void setParameter(ParameterSetter setter, Object parameter)
throws SQLException {
if (parameter == null) {
setter.setNull(Types.BIGINT);
} else {
BigInteger i = (BigInteger) parameter;
setter.setBigDecimal(new BigDecimal(i));
}
}
public Object valueOf(String s) {
return s;
}
}
Then all you have to do is register the type handler in your sqlmap-config.xml file like so:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true" />
<typeHandler javaType="java.math.BigInteger"
callback="org.qnot.util.BigIntegerTypeHandler"/>
<sqlMap resource="sql/example.xml"/>
</sqlMapConfig>
本文介绍了一个使用iBATIS框架时遇到的问题:如何处理MySQL中的bigint unsigned类型的字段。通过实现一个自定义的BigIntegerTypeHandler来解决iBATIS在插入bigint数据时抛出的异常,并提供了一个注册该处理器的具体配置示例。
719

被折叠的 条评论
为什么被折叠?



