mybatis的类型未设置

情况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 支持驼峰命名

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值