MyBatis的问题:There is no getter for property named '数据表中的字段名' in '实体类'!

版权声明:只做原创,告别转载。 https://blog.csdn.net/pingsha_luoyan/article/details/80040846

原因:说白了就是MyBatis中的SQL语句大小写弄错了;(解决方案在最后,中间的是解释原因)

在MyBaits中的配置文件中:mybaits-config.xml中有这样一句话:在代码中用红色标注:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        
        <setting name="cacheEnabled" value="true"/>
       
        <setting name="defaultStatementTimeout" value="3000"/>
        
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        
        <setting name="useGeneratedKeys" value="true"/>
    </settings>

    <!-- Continue going here -->

</configuration><setting name="useGeneratedKeys" value="true"/>
    </settings>

    <!-- Continue going here -->

</configuration>

它的作用是:设置是否使用JDBC的getgenereatedkeys方法获取主键并赋值到keyproperty设置的领域模型中;

说到这里,可能会有很多人不明白什么意思:

举个例子就明白了:例如:

  String INSERT_FIELDS = " title , content , created_date , user_id , comment_count  ";String INSERT_FIELDS = " title , content , created_date , user_id , comment_count  ";
<span style="color:#ff0000"> @Insert({"insert into ", TABLE_NAME, "(", INSERT_FIELDS,
            ") values (#{title},#{content},#{createdDate},#{userId},#{commentCount})"})</span>

用红色标注的部分:user_id 是在数据库中的字段,但在实例中使用时将其变为userId

created_date是在数据库中的字段,但在实例中使用时将其变为createdDate,

comment_count是在数据库中的字段,但在实例中使用时将其变为commentCount;

相信到这里聪明的你已经明白了到底是怎么回事:将_后面的部分将第一个小写字母转换为大写字母,其他的不变。将会解决该问题。

扫描二维码关注公众号,回复: 6421558 查看本文章

猜你喜欢

转载自blog.csdn.net/pingsha_luoyan/article/details/80040846