145_Java_SpringMybatis_Mybatis_dynamicSql

举报
alexsully 发表于 2021/08/24 19:08:25 2021/08/24
【摘要】 动态SQL

1) 动态SQL是MyBatis强大特性之一, 极大的简化我们拼装SQL的操作
2) 动态SQL元素和使用JSTL或其他类似基于XML的文本处理器相似
3) MyBatis采用功能强大的基于OGNL 的表达式来简化操作

if where 
choose (when,otherwise)
trim(where,set)
foreach


4) OGNL(Object Graph Navigation Language)对象图导航语言,表达式语言,通过它可以非常方便的来操作对象属性 

访问对象属性:		person.name
调用方法:		    person.getName()
调用静态属性/方法:	@java.lang.Math@PI @java.util.UUID@randomUUID()
调用构造方法:		new com.alex.bean.Person(‘admin’).name
运算符:		     +,-*,/,%
逻辑运算符:		 in,not in,>,>=,<,<=,==,!=
注意:xml中特殊符号如”,>,<等这些都需要使用转义字符

接口

public interface EmployeeMapperDynamicSQL {
    //携带了哪个字段查询条件就带上这个字段的值
    public List<Employee> getEmpsByConditionIf(Employee employee);
    public List<Employee> getEmpsByConditionTrim(Employee employee);
    public List<Employee> getEmpsByConditionChoose(Employee employee);
    public void updateEmp(Employee employee);
    //查询员工id'在给定集合中的
    public List<Employee> getEmpsByConditionForeach(@Param("ids")List<Integer> ids);
    public void addEmps(@Param("emps")List<Employee> emps);
}

1) If 用于完成简单的判断.
2) Where用于解决SQL语句中where关键字以及条件中第一个and或者or的问题

 <!-- 1 if 判断 where-->
<!--    1)	If用于完成简单的判断.-->
<!--    2)	Where用于解决SQL语句中where关键字以及条件中第一个and或者or的问题-->
    <!-- public List<Employee> getEmpsByConditionIf(Employee employee);-->
<!-- SELECT * from tbl_employee WHERE id = and last_name =""and 	gender = ""and email =""-->
    <select id="getEmpsByConditionIf" resultType="com.alex.bean.Employee">
        select * from tbl_employee
        <where>
            <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 like #{email}
            </if>
            <!-- ognl会进行字符串与数字的转换判断  "0"==0 -->
            <if test="gender==0 or gender==1">
                and gender=#{gender}
            </if>
        </where>
    </select>

3) Trim 可以在条件判断完的SQL语句前后 添加或者去掉指定的字符
   prefix: 添加前缀 ; prefixOverrides: 去掉前缀
   suffix: 添加后缀; suffixOverrides: 去掉后缀

<!--   2  trim-->
    <!--public List<Employee> getEmpsByConditionTrim(Employee employee);  -->
    <!-- 后面多出的and或者or where标签不能解决
         prefix="":前缀:trim标签体中是整个字符串拼串 后的结果。 prefix给拼串后的整个字符串加一个前缀
         prefixOverrides="": 前缀覆盖: 去掉整个字符串前面多余的字符
         suffix="":后缀  suffix给拼串后的整个字符串加一个后缀
         suffixOverrides="" 后缀覆盖:去掉整个字符串后面多余的字符 -->

    <select id="getEmpsByConditionTrim" resultType="com.alex.bean.Employee">
        select * from tbl_employee
        <!-- 自定义字符串的截取规则 -->
         <trim prefix="where" suffixOverrides="and">
             <if test="id!=null">
                 id=#{id} and
             </if>
             <if test="lastName!=null &amp;&amp; lastName!=&quot;&quot;">
                 last_name like #{lastName} and
             </if>
             <if test="email!=null and email.trim()!=&quot;&quot;">
                 email like #{email} and
             </if>
             <!-- ognl会进行字符串与数字的转换判断  "0"==0 -->
             <if test="gender==0 or gender==1">
                 gender=#{gender}
             </if>
         </trim>

    </select>

4) choose(when、otherwise) 主要是用于分支判断,类似于java中的switch case,只会满足所有分支中的一个

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

5) set 主要是用于解决修改操作中SQL语句中可能多出逗号的问题

<!-- 5) set 主要是用于解决修改操作中SQL语句中可能多出逗号的问题-->
<!--  public void updateEmp(Employee employee);-->
    <update id="updateEmp" >
        update tbl_employee
        <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>

6) foreach 主要用户循环迭代
collection: 要迭代的集合; item: 当前从集合中迭代出的元素
open: 开始字符 close:结束字符; separator: 元素与元素之间的分隔符
index: 迭代的是List集合: index表示的当前元素的下标 ;迭代的Map集合:  index表示的当前元素的key

7) sql 标签是用于抽取可重用的sql片段

<!--    sql 标签是用于抽取可重用的sql片段,将相同的,使用频繁的SQL片段抽取出来,单独定义,方便多次引用-->
    <!-- 抽取可重用的sql片段。方便后面引用
      1、sql抽取:经常将要查询的列名,或者插入用的列名抽取出来方便引用
      2、include来引用已经抽取的sql:
       3、include还可以自定义一些property,sql标签内部就能使用自定义的属性include-property:取值的正确方式${prop},#{不能使用这种方式}
-->
    <sql id="selectSQL">
        select * from tbl_employee
    </sql>

    <!--    6) foreach 主要用户循环迭代-->
<!--    public List<Employee> getEmpsByConditionForeach(@Param("ids")List<Integer> ids);-->
    <select id="getEmpsByConditionForeach" resultType="com.alex.bean.Employee">
        <include refid="selectSQL"></include>

        <foreach collection="ids" item="item" open="where id in (" close=")" separator=",">
            #{item}
        </foreach>
    </select>

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。