您现在的位置是:网站首页>列表内容
解决mybatis查询结果为null时,值被默认值替换问题_Mysql_
2023-05-27 06:11:55
66人已围观
简介 解决mybatis查询结果为null时,值被默认值替换问题_Mysql_
查询结果为null时,值被默认值替换
问题:pojo种设置了一个默认值,当此字段查询结果为空时,字段值变成了默认值0,经过排查发现,mybatis在赋值时并没有调用set方法赋值,而是直接调用get方法,取了默认值
问题原因
原因是因为mybatis在给map赋值时,如果返回值不是基本数据类型,且返回值为null,就不会处理这个字段,不会将字段的值映射到map中。也就是说返回的map中是没有这个字段的,当结果返回的时候,调用get方法,就直接调用了字段设置的默认值0
源码:
解决办法
在application.yml配置中添加配置call-setters-on-nulls: true,让mybatis在给map参数映射的时候连null值也一并带过来
mybatis查询结果处理
处理核心流程
PreparedStatement的查询结果需要进行映射
publicList query(Statement statement, ResultHandler resultHandler) throws SQLException { PreparedStatement ps = (PreparedStatement) statement; // 装换preparedStatement ps.execute(); // 执行SQL return resultSetHandler. handleResultSets(ps); //处理结果集 }
处理结果集会用到结果集处理ResultSetHandler,他有两个实现类:FastResultSetHandler和NestedResultSetHandler,前者用于普通结果集处理,后者用于嵌套结果集处理
就FastResultSetHandler而言,handleResultSets的执行步骤为
public List
返回类型处理ResultHandler
在FastResultSetHandler#handleRowValues的resultHandler.handleResult(resultContext)中会调用结果处理器ResultHandler,他主要有下面两个实现类
DefaultResultHandler主要用于查询结果为resultType处理,DefaultMapResultHandler主要用于查询结果为resultMap的处理
这里应为查询结果为resultType,所以使用的是DefaultResultHandler#handleResult,主要是将处理后的结果值,放入结果列表中
public void handleResult(ResultContext context) { list.add(context.getResultObject()); }
字段类型处理TypeHandler
Mybatis主要使用TypeHadler进行返回结果字段类型的处理,他的主要子类是BaseTypeHandler, 预留了setNonNullParameter,getNullableResult等接口给子类实现
public abstract class BaseTypeHandlerextends TypeReference implements TypeHandler { protected Configuration configuration; public void setConfiguration(Configuration c) { this.configuration = c; } public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException { if (parameter == null) { if (jdbcType == null) { throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters."); } try { ps.setNull(i, jdbcType.TYPE_CODE); } catch (SQLException e) { throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . " + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. " + "Cause: " + e, e); } } else { setNonNullParameter(ps, i, parameter, jdbcType); } } public T getResult(ResultSet rs, String columnName) throws SQLException { T result = getNullableResult(rs, columnName); if (rs.wasNull()) { return null; } else { return result; } } public T getResult(ResultSet rs, int columnIndex) throws SQLException { T result = getNullableResult(rs, columnIndex); if (rs.wasNull()) { return null; } else { return result; } } public T getResult(CallableStatement cs, int columnIndex) throws SQLException { T result = getNullableResult(cs, columnIndex); if (cs.wasNull()) { return null; } else { return result; } } public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException; public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException; public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException; public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException; }
他的子类主要有StringTypeHandler、BOOleanTypeHandler等,分别用于处理不同的字段类型值
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
点击排行

- 魔兽世界6.0万圣节坐骑宠物怎么得_6.0万圣节坐骑宠物获得方法介绍_网络游戏_游戏攻略_
- 上古世纪船只怎么改造 船只系统详细介绍_网络游戏_游戏攻略_
- 梦幻西游2014精武天神PK大赛来袭 规则奖励详解_网络游戏_游戏攻略_
- 魔兽世界6.0火法怎么玩_魔兽世界6.0火法输出心得_网络游戏_游戏攻略_
- cf新版VIP特权等级 新版CFVIP专属名片等级介绍_网络游戏_游戏攻略_
- 风暴英雄新手上分首选英雄攻略心得_风暴英雄新手强势英雄推荐一览_网络游戏_游戏攻略_
- 逆战10月21日论坛签到活动详情_逆战10月21日论坛签到活动网址介绍_网络游戏_游戏攻略_
- 斗战神新副本狮王监牢打法攻略_网络游戏_游戏攻略_
- 最终幻想142.2新版本装备修改详情_最终幻想142.2新版本装备修改介绍_网络游戏_游戏攻略_
本栏推荐

-
魔兽世界6.0万圣节坐骑宠物怎么得_6.0万圣节坐骑宠物获得方法介绍_网络游戏_游戏攻略_
-
上古世纪船只怎么改造 船只系统详细介绍_网络游戏_游戏攻略_
-
梦幻西游2014精武天神PK大赛来袭 规则奖励详解_网络游戏_游戏攻略_
-
魔兽世界6.0火法怎么玩_魔兽世界6.0火法输出心得_网络游戏_游戏攻略_
-
cf新版VIP特权等级 新版CFVIP专属名片等级介绍_网络游戏_游戏攻略_
-
风暴英雄新手上分首选英雄攻略心得_风暴英雄新手强势英雄推荐一览_网络游戏_游戏攻略_
-
逆战10月21日论坛签到活动详情_逆战10月21日论坛签到活动网址介绍_网络游戏_游戏攻略_
-
斗战神新副本狮王监牢打法攻略_网络游戏_游戏攻略_
-
最终幻想142.2新版本装备修改详情_最终幻想142.2新版本装备修改介绍_网络游戏_游戏攻略_