MyBatis注意事项(返回值是Integer,操作IN语句注意事项,插入空值)

余生长醉 提交于 2020-01-10 21:13:12

1. 当方法的返回值是Integer类型时候,需要指明ResultType属性。

错误信息为:

org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped 

Statement 'com.ssi.domains.leave.repository.LeaveRepository.getLeaveCount'.  

It's likely that neither a Result Type nor a Result Map was specified.

在这里插入图片描述
所以,正确的Mybatis配置,要加上 resultType = ‘integer’。

  /**
     * 获取表里所有记录的数量
     *
     * @param statement 要执行的SQL语句的标记,对应的是Mybatis中的id的值
     * @return          表里所有记录的数量
     */
    Integer getCount(String statement);

所以,正确的Mybatis配置,要加上 resultType = ‘integer’。
在这里插入图片描述

2. 操作IN语句注意事项

当用到IN语句时,Dao层接口中传递的是List<String>, Mybatis解析成 id IN (" 221 , 223, 224 "),所以疯狂报错。

这时,选择传递一个:List<Long>

List<QuestionnaireMappingGroupEntity> findAllReceiverGroupMemeber(@Param("list")List<Long> list);
<select id="findAllReceiverGroupMemeber" resultType="com.aisino.domain.questionnaire.entity.QuestionnaireMappingGroupEntity">
    SELECT
    ACCOUNT_ID  accountId,
    TAXPAYER_ID taxpayerId
    FROM

    (SELECT ACCOUNT_ID , TAXPAYER_ID
    FROM T_NTM_RECEIVER_GROUP t
    JOIN T_NTM_RECEIVER_GROUP_DETAIL detail ON DETAIL."GROUP_ID" = t.id
    WHERE t.DELETE_FLAG = 1
    AND DETAIL.DELETE_FLAG = 1
    AND t.ID in
    <foreach collection="list" item="groupId" open="(" close=")" separator=",">
            #{groupId}
    </foreach>

    )result
</select>

3. 插入空值

插入空值报错:
Error setting null for parameter #10 with JdbcType OTHER

mybatis 插入空值時需要指定jdbcType
报错内容:

Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #10 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型

MyBatis 插入空值时,需要指定JdbcType
mybatis insert空值报空值异常,但是在pl/sql不会提示错误,主要原因是mybatis无法进行转换,
解决方法:
在insert语句中,增加jdbcType解决问题

<insert id="save" parameterType="Province">
  <![CDATA[
  insert into t_yp_province
  (fid,fname,fnumber,fsimpleName,fdescription,fcreateTime,flastUpdateTime,fdirect)
  values
  ( #{id,jdbcType=VARCHAR},
   #{name,jdbcType=VARCHAR},
   #{number,jdbcType=VARCHAR},
   #{simpleName,jdbcType=VARCHAR},
   #{description,jdbcType=VARCHAR},
   #{createTime,jdbcType=DATE},
   #{lastUpdateTime,jdbcType=DATE},
   #{direct,jdbcType=NUMERIC}
  )  
  ]]>
 </insert>;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!