Mybatis resultMap空值映射问题解决
来源:互联网 发布:java汽车租赁系统 编辑:程序博客网 时间:2024/05/17 22:02
Mybatis在使用resultMap来映射查询结果中的列,如果查询结果中包含空值的列(不是null),则Mybatis在映射的时候,不会映射这个字段,例如 查询 name,sex,age,数据库中的age字段没有值,Mybatis返回的map中只映射了 name和sex字段,而age字段则没有包含。
那么如何将age字段映射到map中呢。提供两种解决方法:
使用Mybatis config配置
创建configuration.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL MAP Config 3.1//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <settings> <setting name="callSettersOnNulls" value="true"/> </settings></configuration>
配置Mybatis的SqlSessionFactoryBean
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:/META-INF/spring/configuration. xml" /> <property name="mapperLocations" value="classpath:/META-INF/spring/mybatis/modelMap/*.xml" /></bean>
在这种配置中,age将以null值映射到map中。
如果想要配置age的默认值,则可以建立一个类,实现Mybatis的TypeHandler接口
public class EmptyStringIfNull implements TypeHandler<String> { @Override public String getResult(ResultSet rs, String columnName) throws SQLException { return (rs.getString(columnName) == null) ? "" : rs.getString(columnName); } @Override public String getResult(ResultSet rs, int columnIndex) throws SQLException { return (rs.getString(columnIndex) == null) ? "" : rs.getString(columnIndex); } <span></span>@Override public String getResult(CallableStatement cs, int columnIndex) throws SQLException { return (cs.getString(columnIndex) == null) ? "" : cs.getString(columnIndex); } @Override public void setParameter(PreparedStatement ps, int arg1, String str, JdbcType jdbcType) <span></span><span style="font-size:9pt;line-height:1.5;">throws SQLException {</span><span style="font-size:9pt;line-height:1.5;"> }</span><span style="font-size:9pt;line-height:1.5;">}</span>
继续在resultMap中使用,即可配置age的默认值(上述代码中age的默认值为”“)
<resultMap id="list" type="java.util.LinkedHashMap"> <result property="name" column="name" /> <result property="sex" column="sex" /> <result property="age" column="age" typeHandler="com.demo.EmptyStringIfNull"/></resultMap>
0 0
- Mybatis resultMap空值映射问题解决
- Mybatis resultMap空值映射问题解决
- Mybatis resultMap空值映射问题解决
- Mybatis resultMap空值映射问题解决
- MyBatis结果集映射(ResultMap)
- [mybatis]ResultMap输出结果映射
- Mybatis 输出映射-- resultType resultMap
- MyBatis输出映射为resultMap
- Mybatis ResultMap 无法继承父resultMap的映射关系
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- 【Mybatis框架】输出映射-resultType与resultMap
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- mybatis-SQL语句映射文件(1)resultMap
- Mybatis 高级结果映射 ResultMap Association Collection
- 如何用IDEA一步一步开发WebService客户端程序
- 工厂模式
- Android_获取手机版本型号
- 主席树 HDOJ2665 && POJ2104 && POJ2761
- c/c++练习–14
- Mybatis resultMap空值映射问题解决
- POJ 3125 Printer Queue(用队列模拟过程)
- GDI+编程
- linux文件系统简介
- CF 1C Ancient Berland Circus
- java 定时任务的几种实现方式
- Android 沉浸式状态栏攻略 让你的状态栏变色吧
- Spring定时任务的几种实现
- 4. Median of Two Sorted Arrays