mybatis

Mybatis 高级xml

2019-07-30  本文已影响0人  凤非飞

官方文档很不错http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html

1.预置sql查询字段

    <!--预置sql查询字段-->
    <sql id="columns"> id,title,content,original_img,is_user_edit,province_id,status,porder
    </sql>
    <!--查询select语句引用columns:-->
    <select id="selectById" resultMap="RM_MsShortcutPanel">
        seelct
        <include refid="columns"/>
        from cms_self_panel
    </select>

2.利用if标签拼装动态where条件
where标记的作用类似于动态sql中的set标记,他的作用主要是用来简化sql语句中where条件判断的书写的,如下所示:

  <select id="selectByParams" parameterType="map" resultType="user">
    select * from user
    <where>
      <if test="id != null ">id=#{id}</if>
      <if test="name != null and name.length()>0" >and name=#{name}</if>
      <if test="gender != null and gender.length()>0">and gender = #{gender}</if>
    </where>
  </select>     

在上述SQL中加入ID的值为null的话,那么打印出来的SQL为:select * from user where name="xx" and gender="xx"

where 标记会自动将其后第一个条件的and或者是or给忽略掉


3.利用choose和otherwise组合标签拼装查询条件
choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

<!--参数为对象-->
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
        select * from t_blog where 1 = 1 
        <choose>
            <when test="title != null">
                and title = #{title}
            </when>
            <when test="content != null">
                and content = #{content}
            </when>
            <otherwise>
                and owner = "owner1"
            </otherwise>
        </choose>
      <if test="gender!=null">  
            and gender=#{gender}  
    </select>


<!--参数为Map-->
 Map<String, Object> map = new HashMap<String, Object>();  
            map.put("searchBy", "position");  
            map.put("gender", "1");  
            map.put("position", "工程师"); 
 List<UserInfo> UIList = userInfo.findUserInfoByOneParam(map); 
<select id="findUserInfoByOneParam" parameterType="Map" resultMap="UserInfoResult">
        select * from userinfo 
        <choose>
            <when test="searchBy=='department'">
                where department=#{department}
            </when>
            <when test="searchBy=='position'">
                where position=#{position}
            </when>
            <otherwise>
                where gender=#{gender}
            </otherwise>
        </choose>
        <if test="true==true">
            AND department = 11
        </if>
</select> 

4.set实现多个字段更新
’MyBatis在update语句里也会碰到多个字段相关的问题。 在这种情况下,就可以使用set标签。使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。没有使用if标签时,如果有一个参数为null,都会导致错误

<!--利用set配合if标签,动态设置数据库字段更新值-->
    <update id="updateById">
        UPDATE cms_label
        <set>
            <if test="labelGroupId != null">
                label_group_id = #{labelGroupId},
            </if>
            dept_id = #{deptId},
            <if test="recommend != null">
                is_recommend = #{recommend},
            </if>
        </set>
        WHERE label_id = #{labelId}
    </update>

5. foreach实现in 查询
foreach属性主要有item,index,collection,open,separator,close。
1、item表示集合中每一个元素进行迭代时的别名,
2、index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,
3、open表示该语句以什么开始,
4、separator表示在每次进行迭代之间以什么符号作为分隔符,
5、close表示以什么结束

注:1.2两种情况适用于调用查询的.xml的Mapper接口参数前面没有@Param标签,如果有@Param标签,collection属性值为@Param标签内设置的值,参考3

1.如果传入的是单参数+集合:
调用查询的.xml的Mapper接口参数前面没有@Param标签

public List<Map<String, Object>> dynamicForeachTest(List<String> ids);
 <select id="dynamicForeachTest" resultType="java.util.Map">
    select * from t_blog where id in
     <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
             #{item}       
      </foreach>    
 </select>

2.如果传入的是单参数+数组:
调用查询的.xml的Mapper接口参数前面没有@Param标签

public List<Map<String, Object>> dynamicForeach2Test(String[] ids);
<select id="dynamicForeach2Test" resultType="java.util.Map">
   select * from t_blog where id in
    <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
          #{item}
    </foreach>
 </select>    

3.如果传入的是多参数+集合

public List<Map<String, Object>> dynamicForeach3Test(@Param("ids") List<String> ids,@Param("contractId") String contractId);
<select id="dynamicForeach3Test" resultType="java.util.Map">
  select * from xxx 
  where cid=#{contractId}
  and fID in 
    <foreach collection="ids"  item = "item" open="(" separator="," close=")">
       #{item}
    </foreach>
</select>
上一篇 下一篇

猜你喜欢

热点阅读