MyBatis(动态SQL)

if:判断

choose(when,otherwise):分支选择,类似java中的switch-case

  如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个

trim:字符串截取(where(封装查询条件),set(封装修改条件))

foreach

一、使用if

 

查询的时候,如果某些条件没带可能sql拼装会有问题

1、给where后面加上1=1 ,以后的条件都and xxx;

2、MyBatis使用where标签来将所有的查询条件包括在内。MyBatis就会将where标签中拼装的sql,多出来的and或者or去掉。(只会去掉第一个多出来的and或者or),and或者or都放在左边。

    //携带了哪个字段查询条件就带上这个字段的值
    public List<Employee> getEmpsByConditionIf(Employee employee);
    <!--查询员工,要求:携带了哪个字段查询条件就带上这个字段的值-->
    <select id="getEmpsByConditionIf" resultType="emp">
      select * from tbl_employee
      <where>
          <!--
              test:判断表达式(OGNL)
              OGNL的使用参照官方文档或者百度
              从参数中取值进行判断
              遇见特殊符号应该写转义字符;
           -->
          <if test="id!=null">
            id=#{id}
          </if>
          <if test="lastName!=null and lastName!=''">
              and last_name like #{lastName}
          </if>
          <if test="email!=null and email.trim()!=''">
              and email=#{email}
          </if>
            <!--OGNL会进行字符串与数字的转换判断-->
          <if test="gender==0 or gender==1">
              and gender=#{gender}
          </if>
      </where>
    </select>

 二、使用trim(不常用)

    public List<Employee> getEmpsByConditionTrim(Employee employee);
    <select id="getEmpsByConditionTrim" resultType="emp">
      select * from tbl_employee
      <!-- 后边多出的and或者or,where标签不能解决
        prefix="":前缀:trim标签体中是整个字符串拼串后的结果。prefix给拼串后的整个字符串加一个前缀
        prefixOverrides="":前缀覆盖:去掉整个字符串前面的多余的字符
        suffix="":后缀:suffix给拼串后的整个字符串加一个后缀
        suffixOverrides=""后缀覆盖:去掉整个字符串后面的多余的字符
      -->
      <trim prefix="where" suffixOverrides="and">
        <if test="id!=null">
            id=#{id} and
        </if>
        <if test="lastName!=null and lastName!=''">
            last_name like #{lastName} and
        </if>
        <if test="email!=null and email.trim()!=''">
            email=#{email} and
        </if>
        <if test="gender==0 or gender==1">
            gender=#{gender}
        </if>
      </trim>
    </select>

三、使用choose

    public List<Employee> getEmpsByConditionChoose(Employee employee);
    <select id="getEmpsByConditionChoose" resultType="emp">
        select * from tbl_employee
        <where>
            <!-- 如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个 -->
            <choose>
                <!--如果-->
                <when test="id!=null">
                    id=#{id}
                </when>
                <when test="lastName!=null and lastName!=''">
                    last_name like #{lastName}
                </when>
                <when test="email!=null">
                    email=#{email}
                </when>
                <!--否则-->
                <otherwise>
                    gender=0
                </otherwise>
            </choose>
        </where>
    </select>

四、set

set标签:更改员工信息时,不需要把所有数据重写一遍,只需要传需要修改的值就可以,其他值不会修改。

    public void updateEmp(Employee employee);
    <update id="updateEmp">
          update tbl_employee
          <!-- set标签取出多余的逗号 -->
          <set>
              <if test="lastName!=null">
                  last_name=#{lastName},
              </if>
              <if test="email!=null">
                  email=#{email},
              </if>
              <if test="gender!=null">
                  gender=#{gender}
              </if>
          </set>
          where id= #{id}
    </update>

 使用trim标签:去除多余的逗号

     <update id="updateEmp">
       update tbl_employee
        <trim prefix="set" suffixOverrides=",">
            <if test="lastName!=null">
                last_name=#{lastName},
            </if>
            <if test="email!=null">
                email=#{email},
            </if>
            <if test="gender!=null">
                gender=#{gender}
            </if>
        </trim>
        where id= #{id}
    </update>

五、使用foreach

 接口代码:

    public List<Employee> getEmpsbyCondtionForeach(List<Integer> ids);

xml代码:

    <!--多个id查询信息===id in (1,2,3)-->
    <select id="getEmpsbyCondtionForeach" resultType="emp">
        select * from tbl_employee where id in
        <!-- collection:指定要遍历的集合:
                list类型的参数会特殊处理封装在map中,map的key就是list
             item:将当前遍历出的元素赋值给指定的变量
             separator:每个元素之间的分隔符
             open:遍历出所有结果拼接一个开始的字符
             close:遍历出所有结果拼接一个结束的字符
             index:索引。遍历list的时候是index就是索引,item就是当前值
                         遍历map时index表示的就是map的key,item就是map的值

             #{变量名}就能取出变量值,也就是当前遍历出的元素
         -->
        <foreach collection="list" item="item_id" separator=","
                open="(" close=")">
            #{item_id}
        </foreach>
    </select>

六、批量保存

数据库SQL:

/*批量保存*/
INSERT INTO tbl_employee(last_name,email,gender,d_id)
VALUES('Krystal','[email protected]','0',1),('Henry','[email protected]','1',2);

接口代码:

扫描二维码关注公众号,回复: 7954708 查看本文章
    public void addEmps(@Param("emps") List<Employee> emps);

 xml代码:

    <!--批量保存-->
    <!--MySQL下批量保存:可以foreach遍历 mysql支持(),(),()语法-->
    <insert id="addEmps">
        <!--批量保存XJS-->
        INSERT INTO tbl_employee(last_name,email,gender,d_id)
        VALUES
        <foreach collection="emps" item="emp" separator=",">
            (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
        </foreach>
    </insert><!--推荐使用-->

或者

需要在数据库的配置文件的url后面添上参数:

allowMultiQueries:在一条语句中,允许使用 “;”来分隔多条查询(真/假,默认值为“假”)

设置为true

这种分号分隔多个SQL可以用于其他的批量操作(删除,修改)

jdbc.url = jdbc:mysql://localhost:3306/mybatis_sgg?allowMultiQueries=true
    <!--这种方式需要数据库连接属性allowMultiQueries=true-->
    <insert id="addEmps">
      <foreach collection="emps" item="emp" separator=";">
          INSERT INTO tbl_employee(last_name,email,gender,d_id)
          VALUES (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
      </foreach>
    </insert>

猜你喜欢

转载自www.cnblogs.com/xjs1874704478/p/11896772.html