美文网首页mybatis
Mybatis 高级xml

Mybatis 高级xml

作者: 凤非飞 | 来源:发表于2019-07-30 09:31 被阅读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>

相关文章

  • Mybatis 高级xml

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

  • MyBatis

    mybatis简介 关键词:持久层框架、xml配置、JDBC高级对象映射MyBatis前身是apache的开源项目...

  • Mybatis 应用 简介

    MyBatis 是优秀的持久层框架。支持定制化 SQL、存储过程以及高级映射。 MyBatis 使用简单的 XML...

  • Mybatis

    1、Mybatis支持普通SQL查询、存储一级高级映射的优秀持久层框架 2、Mybatis可以使用简单的XML或注...

  • MyBatis学习笔记 - MyBatis CRUD基本使用

    使用MyBatis必要配置如下: (1) MyBatis配置信息,mybatis.xml mybatis.xml中...

  • mybatis和Spring整合配置文件

    mybatis-config.xml文件 mybatis-config.xml文件

  • SpringBoot集成Mybatis(四)

    Mybatis是一款支持复杂的SQL语句,存储过程及高级映射的持久层的框架。使用Mybatis有两种方式,XML和...

  • SpringMVC SSM框架笔记二:xml配置Mybatis

    spring整合mybatis有多重方式 xml配置Mybatis,代码通过mybatis的xml配置生成SqlS...

  • Mybatis的学习

    学习mybatis当然配置拉mybatis-config.xml 接口mapper对应的xml

  • spring-mybatis

    spring-mybatis spring-mybatis.xml 自行编写db文件 UserMapper.xml...

网友评论

    本文标题:Mybatis 高级xml

    本文链接:https://www.haomeiwen.com/subject/dorbrctx.html