zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

异常:nested exception is org.apache.ibatis.exceptions.PersistenceException,error SQL

2023-04-18 16:12:58 时间

一般出现这种错误提示代表程序写的有问题,一般是SQL语句不对,主要有以下几种类型:

1.实体类属性和数据库表字段不对应,主要有以下几种类型错误:

  • Mapper.xml中配置的resultMap有问题,检查column和property是否对应;column和property的拼写是否有问题。  注:column对应数据库表字段,property对应实体类属性。如图1.1所示:
<resultMap id="enterpriseMap" type="com.fjz.screendata.entity.Enterprise">
        <result column="id" property="id"/>
        <result column="create_time" property="createTime"/>
        <result column="create_user" property="createUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="name" property="name"/>
    </resultMap>

图1.1

  •  SQL语句中表名访问的字段是否在数据库表中存在,如图1.2所示,bdb.dict_value中的dict_value字段如果在bdb对应的blade_dict_biz表中不存在,会导致错误。

 图1.2

2.数据表字段和实体类属性的数据类型不一致,或者是在程序或SQL语句中错误的判断逻辑导致字段或属性的数据类型出现问题。

实体类:

private String remark;

Mapper.xml:

<select id='selectInfo'>
    select * from table
    <where>
        <if test="remark != null and remark != 0">
            AND remark = #{remark}
        </if>
    </where>
</select>

注:由于remark为String类型,但select标签中由于使用了 remark != 0的判断,导致程序把remark当做数字来处理,但String是不能进行!= 0的操作的。

3.Mapper.xml中SQL语句from后面的表名不对,检查表名拼写有无错误,以及表名是否和实体类对应的表名一致。

4.Mapper.xml中SQL语句语法有问题,可能包括:

图2.1

5.检查数据库地址是否正确,数据库中是否有对应的表,Reconnect数据库。