情况1:ResultMap 中没有配置字段映射
错误示例
xml
<!-- 空的ResultMap -->
<resultMap id="UserMap" type="User">
<!-- 没有任何字段映射配置 -->
</resultMap>
<select id="selectUser" resultMap="UserMap">
SELECT id, name, email FROM users WHERE id = #{id}
</select>
错误现象
不会报错,但数据映射可能不完整
MyBatis 会尝试自动映射
只有列名与属性名完全匹配的字段会被映射
不符合命名规范的字段会被忽略
返回的对象可能部分字段为 null
java
// User类
public class User {
private Long id;
private String name;
private String email;
private Date createTime; // 表中是create_time
}
// 查询结果
User user = userMapper.selectUser(1);
System.out.println(user.getId()); // 有值 (id列名匹配)
System.out.println(user.getName()); // 有值 (name列名匹配)
System.out.println(user.getEmail()); // 有值 (email列名匹配)
System.out.println(user.getCreateTime()); // null (create_time不匹配)
情况2:ResultMap 中 column 属性错误
错误示例
xml
<resultMap id="UserMap" type="User">
<id property="id" column="user_id"/> <!-- 错误:表中列名是id -->
<result property="name" column="user_name"/><!-- 错误:表中列名是name -->
<result property="email" column="email"/>
</resultMap>
<select id="selectUser" resultMap="UserMap">
SELECT id, name, email FROM users <!-- 实际列名 -->
</select>
错误现象
不会报错,但对应字段映射失败
java
User user = userMapper.selectUser(1);
System.out.println(user.getId()); // 0或null
System.out.println(user.getName()); // null
System.out.println(user.getEmail()); // 有值(因为column="email"匹配)
情况3:复杂类型没有指定 javaType/jdbcType
错误示例
xml
<resultMap id="UserMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="createTime" column="create_time"/>
<!-- 没有指定status字段的类型 -->
<result property="status" column="status"/>
</resultMap>
错误现象
通常不会报错,MyBatis 会尝试类型推断
java
// User类
public class User {
private Long id;
private String name;
private Date createTime;
private Integer status; // 数据库中是TINYINT或VARCHAR
}
如果数据库类型与Java类型兼容:正常映射
如果类型不兼容:可能报类型转换异常
情况4:association/collection 没有指定 javaType/ofType
错误示例
xml
<resultMap id="UserOrderMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<!-- 没有指定javaType -->
<association property="order" >
<id property="id" column="order_id"/>
<result property="orderNo" column="order_no"/>
</association>
</resultMap>
错误现象
可能报错,取决于MyBatis配置
text
### Cause: org.apache.ibatis.builder.BuilderException:
Error parsing Mapper XML. The xml location is file [UserMapper.xml].
Cause: org.apache.ibatis.builder.BuilderException:
Error resolving class. Cause: org.apache.ibatis.type.TypeException:
Could not resolve type alias ''. Cause: java.lang.ClassNotFoundException:
Cannot find class:
或者运行时错误:
text
### Cause: org.apache.ibatis.executor.ResultMapException:
No type handler found for property order
情况5:枚举类型没有指定类型处理器
错误示例
xml
<resultMap id="UserMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="userStatus" column="status"/> <!-- 枚举字段 -->
</resultMap>
java
public enum UserStatus {
ACTIVE, INACTIVE, DELETED
}
public class User {
private Long id;
private String name;
private UserStatus userStatus;
}
错误现象
类型转换异常
text
### Cause: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.executor.result.ResultMapException:
Error attempting to get column 'status' from result set.
Cause: java.sql.SQLException: 无法将字符串转换为枚举
正确的配置方式
方案1:完整的ResultMap配置
xml
<resultMap id="UserMap" type="User">
<id property="id" column="id" javaType="Long" jdbcType="BIGINT"/>
<result property="name" column="name" javaType="String" jdbcType="VARCHAR"/>
<result property="email" column="email" javaType="String" jdbcType="VARCHAR"/>
<result property="createTime" column="create_time" javaType="java.util.Date" jdbcType="TIMESTAMP"/>
<result property="status" column="status" javaType="Integer" jdbcType="TINYINT"
typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
</resultMap>
方案2:依赖自动映射(简化版)
xml
<!-- 开启自动映射 -->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
<!-- 使用自动映射 -->
<resultMap id="UserMap" type="User" autoMapping="true">
<id property="id" column="id"/>
<!-- 只需要配置特殊字段 -->
<result property="createTime" column="create_time"/>
</resultMap>
总结
ResultMap 没有给字段类型设置时:
基本类型字段:通常不会报错,MyBatis 会尝试自动类型转换
列名不匹配:不会报错,但字段映射失败,值为 null
复杂类型/关联字段:可能报类型解析错误
枚举类型:可能报类型转换异常
最佳实践:
对于简单字段,可以依赖自动映射
对于复杂字段、关联字段,明确指定类型
使用 autoMapping="true" 减少配置工作量
在 mybatis-config.xml 中配置 mapUnderscoreToCamelCase 支持驼峰命名
mybatis的类型未设置
最新推荐文章于 2025-12-05 17:02:52 发布
2596

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



