今天有个表用ibaits查询,表结构如下
CREATE TABLE `matrix_library_temp` ( `id` bigint(20) NOT NULL COMMENT '主键', `gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间', `gmt_modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '修改时间', `temp_author_emp_id` varchar(64) DEFAULT NULL COMMENT '作者的工号', `temp_author_nickname` varchar(256) DEFAULT NULL COMMENT '作者的名字', `temp_article_title` varchar(1024) DEFAULT NULL COMMENT '临时文章的内容', `temp_plain_text` text COMMENT '文章的纯文本内容', `temp_content_text` text COMMENT '文章的带格式的文本内容', `temp_html_text` text COMMENT '文章的html格式的内容', `temp_save_id` varchar(256) DEFAULT NULL COMMENT '单页面修改的唯一标识', `temp_logic_deleted` int(11) DEFAULT '0' COMMENT '标识是否显示到草稿列表中', PRIMARY KEY (`id`) )
ibatis的查询语句如下
<select id="getRecoverNoteList" parameterClass="java.util.HashMap" resultClass="miserTempDO"> select id, gmtCreate, gmtModififed, tempAuthorEmpId, tempAuthorNickname, tempNoteTitle, tempPlainText, tempContentText, tempHtmlText from ( select GROUP_CONCAT( DISTINCT temp_save_id ), id as id, gmt_create as gmtCreate, gmt_modified as gmtModififed, temp_author_emp_id as tempAuthorEmpId, temp_author_nickname as tempAuthorNickname, temp_note_title as tempNoteTitle, '' as tempPlainText, '' as tempContentText, '' as tempHtmlText, temp_save_id as tempSaveId from matrix_miser_temp WHERE temp_logic_deleted = 0 <dynamic> <isNotNull property="auhtorEmpId" prepend="and"> temp_author_emp_id=#auhtorEmpId# </isNotNull> <isNotNull property="id" prepend="and"> id=#id# </isNotNull> </dynamic> ) t order by id desc </select>
理论上查询语句会把数据映射成对应的DO类,DO类实现如下
public class MiserTempDO { private int id; private String tempNoteTitle; private String tempPlainText; private String tempContentText; private String tempHtmlText; private Date gmtCreate; private Date gmtModififed; private String tempAuthorEmpId; private String tempAuthorNickname; //getter and setter... }
数据查询没有问题,但是用ibatis调用的时候就报错
--- The error occurred in ibatis/Miser.xml. --- The error occurred while applying a result map. --- Check the miser.getRecoverNoteList-AutoResultMap. --- The error happened while setting a property on the result object. --- Cause: net.sf.cglib.beans.BulkBeanException com.ibatis.common.jdbc.exception.NestedSQLException: --- The error occurred in ibatis/Miser.xml. --- The error occurred while applying a result map. --- Check the miser.getRecoverNoteList-AutoResultMap. --- The error happened while setting a property on the result object. --- Cause: net.sf.cglib.beans.BulkBeanException at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:204) at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForList(MappedStatement.java:139)
网上也有类似的报错,原因是数据库中number字段是nullable的,查询的时候转型的时候错了。
网上的解决方案是再返回中配置为空的时候进行nullvalue处理
<result property="orders" column="ORDERS" javaType="java.lang.Integer" jdbcType="number" nullValue="0" />
但是我没有配置resultmap所以也不想用这样的方案来解决,
首先数据库中是没有数据的,其中只有id是不为空的,并且没有默认值,难道是这里出错了?
接下来再看DO中id是int型的,难道是空值转int的时候错了?
尝试把DO中int改成Integer,问题解决。