03_Mybatis__动态SQL

if:判断
choose (when, otherwise):分支选择 switch-case        
		如果带了id就用id查,如果带了gender就用gender查,只会进入一个分支
trim (where(封装查询条件), set(封装修改条件)):字符串截取
foreach(遍历集合)

1、if_判断&OGNL

<!-- 查询员工,要求携带了哪个字段查询条件就带上这个字段的值 -->
<!-- test:判断表达式(OGNL)
                从参数中取值进行判断
            -->
		select * from tbl_employee
            <if test="id!=null">
                id=#{id}
            </if>
            <if test="lastName!=null and lastName!=''">
                and last_name like #{lastName}
            </if>
            <if test="gender==0 or gender==1">
                and gender=#{gender}
            </if>
            <if test="email!=null and email.trim()!=''">
                and email=#{email}
            </if>

2、where_查询条件

<!--where
            查询的时候如果某些条件没带可能sql拼装会有问题
                1、在where 后面 添加1=1(不安全)
                2、mybatis使用where标签来将所有的查询条件包括在内【mybtais推荐】
                    mybatis会将where标签中拼装的sql,多出来的and或者or去掉
                    where只会去掉第一个多出来的and或者or
        -->
		select * from tbl_employee
        <where>
            <!-- test:判断表达式(OGNL)
                从参数中取值进行判断
            -->
            <if test="id!=null">
                id=#{id}
            </if>
            <if test="lastName!=null and lastName!=''">
                and last_name like #{lastName}
            </if>
            <if test="gender==0 or gender==1">
                and gender=#{gender}
            </if>
            <if test="email!=null and email.trim()!=''">
                and email=#{email}
            </if>
        </where>

3、trim_自定义字符串截取

select * from tbl_employee
        <!--
            prefix="" :前缀,trim标签体中是整个字符串拼装后的结果。
                            prefix给拼串后的整个字符串加一个前缀【where】
            prefixOverrides="":前缀覆盖,去掉整个字符串前面多余的字符
            suffix="":后缀,
                            suffix给拼串后的整个字符串加一个后缀
            suffixOverrides="":后缀覆盖,去掉整个字符串后面多余的字符
        -->
        where
        <!-- 自定义字符串的截取规则 -->
        <trim suffix="" suffixOverrides="and">
            <if test="id!=null">
                id=#{id} and
            </if>
            <if test="lastName!=null and lastName!=''">
                 last_name like #{lastName} and
            </if>
            <if test="gender==0 or gender==1">
                 gender=#{gender} and
            </if>
            <if test="email!=null and email.trim()!=''">
                 email=#{email}
            </if>
        </trim>

4、choose_分支选择

select * from tbl_employee
        <where>
           <!-- 如果带了id就用id查,如果带了gender就用gender查,只会进入一个分支 -->
            <choose>
                <when test="id!=null">
                    id=#{id}
                </when>
                <when test="lastName!=null">
                    last_name like #{lastName}
                </when>
                <when test="email!=null">
                    email=#{email}
                </when>
                <otherwise>
                    1=1
                    <!-- gender =0 -->
                </otherwise>
            </choose>
        </where>

5、set_与if结合的动态更新

查询不需要提交外,增删改都需要提交,openSession.commit();手动提交
<!-- public void updateEmp(Employee employee);
            可以使用set标签,或者trim标签,来去除多余的,逗号
    -->
    <update id="updateEmp">
        update tbl_employee
            <!--第一种方式-->
        <!--<set>
        <if test="lastName!=null">
            last_name=#{lastName},
        </if>
        <if test="gender!=null">
            gender=#{gender},
        </if>
        <if test="email!=null">
            email=#{email}
        </if>
        </set>-->
        set
            <!--第二种方式-->
        <trim suffixOverrides=",">
            <if test="lastName!=null">
                last_name=#{lastName},
            </if>
            <if test="gender!=null">
                gender=#{gender},
            </if>
            <if test="email!=null">
                email=#{email}
            </if>
        </trim>
        where id=#{id}
    </update>

6、foreach_遍历集合

动态 SQL 的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)
带IN关键字的查询 【IN关键字用于判断某个字段的值是否在指定集合中,如果是,则满足条件,该字段所在的记录将被查询出来,【也可以是增删改】】
SQL语句:
		select * 【或者】字段名1,字段名2,...
		from 表名
		where 字段名 [not]in(元素1[值],元素2,...)
<!--    public List<Employee> getEmpsByConditionForeach(List<Integer> ids);-->
    <select id="getEmpsByConditionForeach" resultType="com.yuanwu.mybatis.bean.Employee">
        select * from tbl_employee
        where id in
        <!--
            collection:指定要遍历的集合;
                  1、强制指定为list且不可改变
                      [传入list的时候,MyBatis会自动包装在一个map中,
								List实例将会以“list”做为键,会通过“list”去寻找]
                  2、利用注解@Param指定入参名称
                  3、将List包装成Map参数进行传递
            item:将当前遍历出的元素赋值给指定的变量;
            separator:每个元素之间的分隔符;
            open:遍历出所有结果拼接一个开始的字符
            close:遍历出所有结果拼接一个结束的字符
            index:索引,遍历list的时候index就是所有,item就是当前值
                        遍历map的时候index表示map的key,item就是map的值value

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

测试类

List<Employee> list = mapper.getEmpsByConditionForeach(Arrays.asList(1,2,3,4));
            for (Employee emp: list) {
                System.out.println(emp);
            }
/*
Preparing: select * from tbl_employee where id in ( ? , ? , ? , ? )   (BaseJdbcLogger.java:143) 
Parameters: 1(Integer), 2(Integer), 3(Integer), 4(Integer)  (BaseJdbcLogger.java:143) 
DEBUG 10-21 22:23:55,801 <==      Total: 4  (BaseJdbcLogger.java:143) 
Employee{id=1, lastName='Admin', gender='1', email='Tom@yuanwu.com'}
Employee{id=2, lastName='Jerry', gender='0', email='Jerry@yuanwu.com'}
Employee{id=3, lastName='Jerry', gender='0', email='Jerry@yuanwu.com'}
Employee{id=4, lastName='Herbert', gender='1', email='Herbert@yuanwu.com'}
*/
posted @ 2020-10-24 13:03  san只松鼠  阅读(65)  评论(0)    收藏  举报