【resultType】Mybatis种insert或update的resultType问题

时间:2021-10-30 14:55:56

Attribute "resultType" must be declared for element type "insert"或"update"

insert报错

这个报错日志中说Attribute "resultType" must be declared for element type "insert",还有一个报错信息是Attribute "resultType" must be declared for element type "update"

当时一直没有找到那里有问题,一直以为问题是我写insert和update语句是没有写resultType,后来发现,问题恰恰出在此处。

因为在insert和update语句中是没有resultType的,报错的信息不是说没有写resultType,而是说要在mybatis的文件中声明resultType之后,才可以使用resultType.

在xml文件的开头一般有两行代码:

DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"

将http://mybatis.org/dtd/mybatis-3-mapper.dtd复制到浏览器中,会自动下载dtd文件,打开后,搜索update和insert和select,会发现之间的区别:

select的声明如下:

ATTLIST selectid CDATA #REQUIREDparameterMap CDATA #IMPLIEDparameterType CDATA #IMPLIEDresultMap CDATA #IMPLIEDresultType CDATA #IMPLIEDresultSetType (FORWARD_ONLY | SCROLL_INSENSITIVE | SCROLL_SENSITIVE) #IMPLIEDstatementType (STATEMENT|PREPARED|CALLABLE) #IMPLIEDfetchSize CDATA #IMPLIEDtimeout CDATA #IMPLIEDflushCache (true|false) #IMPLIEDuseCache (true|false) #IMPLIEDdatabaseId CDATA #IMPLIEDlang CDATA #IMPLIEDresultOrdered (true|false) #IMPLIEDresultSets CDATA #IMPLIED 

insert的声明如下:

ATTLIST insertid CDATA #REQUIREDparameterMap CDATA #IMPLIEDparameterType CDATA #IMPLIEDtimeout CDATA #IMPLIEDflushCache (true|false) #IMPLIEDstatementType (STATEMENT|PREPARED|CALLABLE) #IMPLIEDkeyProperty CDATA #IMPLIEDuseGeneratedKeys (true|false) #IMPLIEDkeyColumn CDATA #IMPLIEDdatabaseId CDATA #IMPLIEDlang CDATA #IMPLIED

update的声明如下:

ATTLIST updateid CDATA #REQUIREDparameterMap CDATA #IMPLIEDparameterType CDATA #IMPLIEDtimeout CDATA #IMPLIEDflushCache (true|false) #IMPLIEDstatementType (STATEMENT|PREPARED|CALLABLE) #IMPLIEDkeyProperty CDATA #IMPLIEDuseGeneratedKeys (true|false) #IMPLIEDkeyColumn CDATA #IMPLIEDdatabaseId CDATA #IMPLIEDlang CDATA #IMPLIED

可以看出select声明中有resultType,而update和insert中没有resultType
错误在此。