1. 操作系统版本及内核发行版本:
CentOS 7.6–3.10.0-693.el7.x86_64
2。 数据库版本及架构:
数据库架构 单机, 数据库版本为 Release 5.0 22.1.0 revision(28cd72e)
3. 问题描述:
SUNDB数据库int类型 jdbc 返回的数据类型为BigDecimal问题
4. 问题原因:
SUNDB 数据库的 integer 类型为 number(10,0) 类型的别名,而number类型对应的java类型为BigDecimal 所以返回类型为BigDecimal。
1)JDBC代码:
Collection<Object[]> results = query.list(); for (Object[] result : results) {
Map<String, Object> task = new HashMap<String, Object>();
String name = (String) result[2];
long status = 0;
if (result[5] instanceof BigDecimal) {
status = ((BigDecimal) result[5]).longValue();
} if (result[5] instanceof BigInteger) {
status = ((BigInteger) result[5]).longValue();
}
Date taskBeginTime = (Date) result[6];
Date taskEndTime = (Date) result[7];
int cycleType = (Integer) result[10];
Date settingTime = (Date) result[11];
代码中的 int cycleType = (Integer) result[10]; 返回值应为integer类型但实际返回 BigDecimal 类型。
2)Mybatis 代码
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springboot_druid_mybatis.mapper.UserMapper">
<resultMap id="BaseResultMap1" type="java.util.Map">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="userName" jdbcType="VARCHAR" property="userName" />
<result column="passWord" jdbcType="VARCHAR" property="passWord" />
<result column="realName" jdbcType="VARCHAR" property="realName" />
</resultMap>
<select id="SelAll" resultMap="BaseResultMap1">
select * from t_user
</select>
</mapper>
结果集 BaseResultMap1 中 id 列的类型应为Integer 但实际为 BigDecimal
5. 解决方案:
1)Hibernate使用原生JDBC操作数据库则需要进行类型转换:
int cycleType = (Integer) result[10].intValue();
或
BigDecimal cycleType = (BigDecimal) result[10];
2)Mybatis 两种解决方案
#1 mapping 中如果resultMap 的 type 类型为 java.util.Map 或 java.util.HashMap,
则需要将 mapping 文件中的对应列添加 javaType 如下:
#2 建立对应的实体类将 resultMap 的type 设置为实体类即可
实体类:
private Integer id;
private String userName;
private String passWord;
private String realName;
private List<Course> courseList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public List<Course> getCourseList() {
return courseList;
}
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
}
mapping文件:
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springboot_druid_mybatis.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.springboot_druid_mybatis.dao.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="userName" jdbcType="VARCHAR" property="userName" />
<result column="passWord" jdbcType="VARCHAR" property="passWord" />
<result column="realName" jdbcType="VARCHAR" property="realName" />
</resultMap>
<select id="SelAll" resultMap="BaseResultMap">
select * from t_user
</select>
</mapper>
最后修改时间:2023-07-17 14:54:58
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




