Mybatis报错:There is no getter for property named ‘xxx‘ in ‘class xxx 的解决办法

一、报错问题

用的是lombok的@Data生成Getter、Setter,但是由于数据库名称和后端实体类不对应,搜索不到。如数据库名称”stu_id“,后端实体类的"stuId"。

二、尝试的解决办法

1、@JsonPropery、@Data、@Getter、@Setter

@Data
@Getter
@Setter
public class StuInfo {
    @ExcelProperty(value="学号")
    @JsonProperty("stu_id")
    private String stuId;
}

2、<resultMap></resultMap>可以解决对应问题

    <resultMap id="AdminMap" type="com.lhm.demo.entity.StuInfo">
        <result property="stuId" column="stu_id"/>
        <result property="stuType" column="stu_type"/>
        <result property="gpaScore" column="gpa_score"/>
        <result property="sumScore" column="summary_score"/>
        <result property="volScore" column="volunteer_score"/>
        <result property="servScore" column="service_score"/>
        <result property="pracScore" column="practice_score"/>
        <result property="addScore" column="added_score"/>
        <result property="addScore" column="added_score"/>
        <result property="researchScore" column="research_score"/>
    </resultMap>

加在mapper.xml的<mapper>标签内。

3、修改数据库语句【本人的问题】

 <insert id = "insertStuInfo" parameterType="com.lhm.demo.entity.StuInfo">
        INSERT INTO stuinfo (stu_id,name,gender,phone,email,year,academy,politic,stu_type,major,gpa_score,summary_score,volunteer_score,research_score,
                             service_score,practice_score,added_score) VALUES(#{stuId},#{name},#{gender},#{phone},#{email},#{year},#{academy},#{politic},#{stuType},
                             #{major},#{gpaScore},#{sumScore},#{volScore},#{researchScore},#{servScore},#{pracScore},#{addScore});
    </insert>

#{stu_id}是错误的,#{stuId}是对的,这个#{}内要写的是后端实体类的名字

### MyBatis中ReflectionException异常:没有找到名为'null'的属性getter方法的问题分析与解决 在MyBatis框架中,`ReflectionException`异常通常表明映射配置或参数传递存在问题。当出现`There is no getter for property named 'null' in 'class XXX'`的错误时,这通常是因为SQL语句中的`test`条件表达式未正确解析参数,导致生成的属性名为空字符串`null`[^1]。 以下是对该问题的详细分析及解决方案: #### 1. 参数未正确传递 如果SQL语句中的动态条件使用了`<if test="xxx != null">`,但实际传递的参数为`null`或未定义,则会导致`test`表达式解析失败,从而生成无效的属性名。例如: ```xml <if test="name != null"> and d.name like concat('%', #{name}, '%') </if> ``` 若`name`参数未正确传递,`test`表达式可能被解析为`null != null`,进而导致错误[^4]。 #### 2. 使用`@Param`注解 当Mapper接口方法有多个参数时,MyBatis默认无法区分参数名称。此时需要通过`@Param`注解明确指定参数名。例如: ```java public List<ValidUrl> findValidList(@Param("enable") Boolean enable); ``` 这样可以确保SQL语句中的`${enable}`或`#{enable}`能够正确映射到对应的参数[^3]。 #### 3. 检查SQL语句中的字段名 如果数据库表字段名与实体类属性名不一致,也可能导致类似问题。例如,数据库字段名为`category_id`,而实体类属性名为`categoryId`,则需要在SQL语句中进行适配: ```xml <if test="categoryId != null"> and d.category_id = #{categoryId} </if> ``` 如果字段名与属性名不匹配,可以通过别名解决: ```sql select d.*, c.name as categoryName from dish d left outer join category c on d.category_id = c.id ``` 然后在实体类中定义`categoryName`属性及其Getter/Setter方法。 #### 4. 特殊情况:`IN`查询 对于`IN`查询,直接传递字符串列表可能导致格式问题。例如: ```xml <if test="status != null and status != ''"> and status in (#{status}) </if> ``` 若`status`为逗号分隔的字符串(如`"1,2,3"`),上述写法会失败。可以通过`FIND_IN_SET`函数解决: ```xml <if test="status != null and status != ''"> and FIND_IN_SET(status, #{status}) </if> ``` 或者将`status`转换为集合类型并使用`foreach`标签[^5]。 #### 5. MyBatis版本问题 某些MyBatis版本可能存在兼容性问题,建议升级到最新稳定版本。如果问题仍然存在,可以尝试将参数改为内置参数`_parameter`[^2]。 --- ### 示例代码 以下是针对上述问题的一个完整示例: ```java // Mapper接口 public interface UserMapper { @Select("<script>" + "SELECT * FROM user WHERE 1=1 " + "<if test='username != null'> AND username = #{username} </if>" + "<if test='age != null'> AND age = #{age} </if>" + "</script>") List<User> findUsers(@Param("username") String username, @Param("age") Integer age); } // 实体类 public class User { private String username; private Integer age; // Getter和Setter方法 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花花橙子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值