• mybatis之动态SQL


    Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了 解决 拼接SQL语句字符串时的痛点问题。

    if

    if标签可通过test属性的表达式进行判断,若表达式的结果为true,则标签中的内容会执行;反之 标签中的内容不会执行

    1. <select id="getEmpListByMoreTJ" resultType="Emp">
    2.   select * from t_emp where 1=1
    3.    <if test="ename != '' and ename != null">
    4.       and ename = #{ename}
    5.    if>
    6.    <if test="age != '' and age != null">
    7.       and age = #{age}
    8.    if>
    9.    <if test="sex != '' and sex != null">
    10.       and sex = #{sex}
    11.    if>
    12. select>

    where

    where和if一般结合使用:

    a>若where标签中的if条件都不满足,则where标签没有任何功能,即不会添加where关键字

    b>若where标签中的if条件满足,则where标签会自动添加where关键字,并将条件最前方多余的

    and去掉 注意:where标签不能去掉条件最后多余的and

    1. <select id="getEmpListByMoreTJ2" resultType="Emp">
    2.   select * from t_emp
    3.    <where>
    4.        <if test="ename != '' and ename != null">
    5.           ename = #{ename}
    6.        if>
    7.        <if test="age != '' and age != null">
    8.           and age = #{age}
    9.        if>
    10.        <if test="sex != '' and sex != null">
    11.           and sex = #{sex}
    12.        if>
    13.    where>
    14. select>

    trim

    trim用于去掉或添加标签中的内容

    常用属性:

    prefix:在trim标签中的内容的前面添加某些内容

    prefixOverrides:在trim标签中的内容的前面去掉某些内容

    suffix:在trim标签中的内容的后面添加某些内容

    suffixOverrides:在trim标签中的内容的后面去掉某些内容

    1. <select id="getEmpListByMoreTJ" resultType="Emp">
    2. select * from t_emp
    3. <trim prefix="where" suffixOverrides="and">
    4. <if test="ename != '' and ename != null">
    5. ename = #{ename} and
    6. if>
    7. <if test="age != '' and age != null">
    8.       age = #{age} and
    9. if>
    10. <if test="sex != '' and sex != null">
    11.   sex = #{sex}
    12. if>
    13. trim>
    14. select>

    choose、when、otherwise

    choose、when、 otherwise相当于if...else if..else

    1. <select id="getEmpListByChoose" resultType="Emp">
    2. select <include refid="empColumns">include> from t_emp
    3. <where>
    4. <choose>
    5. <when test="ename != '' and ename != null">
    6. ename = #{ename}
    7. when>
    8. <when test="age != '' and age != null">
    9. age = #{age}
    10. when>
    11. <when test="sex != '' and sex != null">
    12.           sex = #{sex}
    13.  when>
    14.  <when test="email != '' and email != null">
    15.           email = #{email}
    16.  when>
    17. choose>
    18. where>
    19. select>

    foreach 

    1. <insert id="insertMoreEmp">
    2.   insert into t_emp values
    3.    <foreach collection="emps" item="emp" separator=",">
    4.       (null,#{emp.ename},#{emp.age},#{emp.sex},#{emp.email},null)
    5.    foreach>
    6. insert>
    7. <delete id="deleteMoreByArray">
    8. delete from t_emp where
    9. <foreach collection="eids" item="eid" separator="or">
    10. eid = #{eid}
    11. foreach>
    12. delete>
    13. <delete id="deleteMoreByArray">
    14. delete from t_emp where eid in
    15. <foreach collection="eids" item="eid" separator="," open="(" close=")">
    16. #{eid}
    17. foreach>
    18. delete>

    我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=1fomewdxp7l2 ​​​​​​​

  • 相关阅读:
    唯一邀请码生成策略
    使用 C# 进行面向对象编程:第 9 部分
    spring-cloud-starter-gateway踩坑
    主机jvisualvm连接到tomcat服务器查看jvm状态
    向量数据库,展望AGI时代
    cmake简略使用介绍
    Spring Boot系列之条件注解
    信号驱动io
    接口回调 and 注解
    【LeetCode-面试经典150题-day23】
  • 原文地址:https://blog.csdn.net/m0_62436868/article/details/127743349