mybatis小坑总结帖(不定时更新)

  1. mapper.xml在使用 <if test="status != null">判断时候 如果传的Integer参数的值是0的话 会判为空!!! 所以写成 <if test="status != null and status != '' or status == 0">
  2. foreach
    foreach 用于处理集合,比如批量插入、批量查询条件等情况,支持 List、Array、Set、Map 等。在下面的例子中,查询条件是一个 list,使用 in 来查询。
<select id="select" resultType="Person">
    SELECT * FROM Person WHERE id in  
    <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
    </foreach>
</select>

foreach 的属性定义如下:

Item:集合中元素迭代的别名,如果集合为 map 这里是 value;
index:集合中元素迭代的索引,如果集合为 map 这里是 key;
collection:集合的类型;
open:语句的前缀;
separator:语句的分隔符;
close:语句的后缀。

3.数据库字段定义integer 11位,xml映射文件为integer,运行调接口有时候会报错
outside valid range for the datatype INTEGER
是因为mybatis的entity的xml里面映射文件设置的type=integer太短,不符合数据库中的长int所导致,需要在 entity中的这个属性的类型改为Long,还需要把xml中的改为jdbcType=”BIGINT” 即可解决

posted @ 2018-02-05 14:32  qwer78  阅读(38)  评论(0)    收藏  举报