buguge - Keep it simple,stupid

知识就是力量,但更重要的,是运用知识的能力why buguge?

导航

【备忘】mybatis的条件判断用<choose>

mybatis并没有if..else,在mybatis的sql mapper文件中,条件判断要用choose..when..otherwise

<choose>
  <when test="status == 'PROCES' or status == 'PENDNG'"> and batcol.status in('PROCES','PENDNG')</when>
  <when test="status == 'PREAUD'"> and batcol.status = '--'</when>
  <otherwise> and batcol.status = #{status,jdbcType=CHAR}</otherwise>
</choose>
 
<if test="status != null and status != 'all'" >
  <choose>
    <when test="status == 'PROCES' or status == 'PENDNG'"> and batcol.status in('PROCES','PENDNG')</when>
    <when test="status == 'PREAUD'"> and batcol.status = '--'</when>
    <otherwise> and batcol.status = #{status,jdbcType=CHAR}</otherwise>
  </choose>

</if>

choose为一个整体,
when表示if ,(when可重复,即实现if..else if..else if..
otherwise表示else。
注意: test里的等号用==,而不是=。


BTW,考考你,下面xml中的sql有什么问题?(提示不是choose)

 

 1 <!-- 按企业名称模糊查询企业Id -->
 2 <select id="selectEnterpriseByNameLike" resultType="java.lang.Long" parameterType="java.util.HashMap">
 3     select enterprise_id as enterpriseId from emax_enterprise
 4     where create_time > #{createTime}
 5     <choose>
 6         <when test="#{usingLike} = '1'">
 7             and FUN_DECRYPTION(enterprise_name) like concat('%', #{enterpriseName} ,'%')
 8         </when>
 9         <otherwise>
10             and FUN_DECRYPTION(enterprise_name) = #{enterpriseName}
11         </otherwise>
12     </choose>
13 </select>

 

 

 

 

答案:问题出现在第6行。应为: <when test="usingLike = '1'"> 

 

【IDEA使用mybatis插件】

菜单 files→settings→plugins,可以搜索Free Mybatis plugin。安装后会提高我们的应用开发效率。
  • mybatis-generator
  • 我们打开生成的接口具体操作数据的Dao.java(有的命名是***Mapper.java),就可以看到右边右箭头可以点击,就可以跳转到具体的映射xml
  • xml也可以直接跳转到具体的接口

posted on 2019-04-11 19:12  buguge  阅读(2945)  评论(0编辑  收藏  举报