mybatis_动态SQL

2年前 (2022) 程序员胖胖胖虎阿
211 0 0

动态sql是指sql语句可动态的变化

/**
 * 通过条件查询员工信息【条件不确定】
         private Integer id;         //员工id
         private String lastName;    //员工姓名
         private String email;       //员工邮箱
         private Double salary;      //员工薪资
    Employee对象中属性不为空,就添加到查询条件中
 id not null->sql:          select id,last_name,email,salary from tbl_employee where id=#{id}
 lastName not null -> sql   select id,last_name,email,salary from tbl_employee where last_name=#{lastName}
 */
public List<Employee> selectEmpByDynamicOpr(Employee employee);

myabtis不支持方法的重载

  • 因为:Mybatis规定方法名与SQL的Id一致【SQL的Id不能重复】导致了方法不能重载

  • Mybatis映射文件中注释

    • 推荐使用:<!-- -->

    • 不推荐使用:--

  • 动态数据:使用Thymeleaf动态渲染html【静态页面】

  • Mybatis动态SQL中支持:OGNL

    • OGNL( Object Graph Navigation Language )对象图导航语言,这是一种强大的

      表达式语言,通过它可以非常方便的来操作对象属性。 类似于我们的EL,SpEL等

  动态sql标签

  • if标签:主要用于基本的if判断
  • where标签 :解决where关键字以及出现的第一个and或者or关键字导致sql语句错误
  • trim标签:可以在条件判断完的SQL语句前后添加或者去掉指定的字符

    • prefix: 添加前缀

    • prefixOverrides: 去掉前缀

    • suffix: 添加后缀

    • suffixOverrides: 去掉后缀

  • choose标签:主要用于分支判断 单条件 类似于java中的if-else 或者 Switch-case 只会满足所有分支中的一个
  • set标签:去除修改语句中的{,}逗号的问题
  • foreach标签:类似于java中的增强for循环
    • collection: 要迭代的集合
    • item: 当前从集合中迭代出的元素
    • open: 开始字符
    • index:

      • 迭代的是List集合: index表示的当前元素的下标

      • 迭代的Map集合: index表示的当前元素的key

    • separator: 元素与元素之间的分隔符

    •      close:结束字符

  • sql标签:定义SQL片段

if -where标签

接口方法 查询员工 根据不同的条件来判断

    /**
     * 动态sql if where 标签 根据传入参数不同动态判断条件
     */
    Employee selectDByEmp(Employee employee);

mapper对应的SQL

    <select id="selectDByEmp" resultType="com.yu.pojo.Employee">
        select id,last_name,email,salary
        from tbl_employee
        <where>
            <if test="id != null">
                and id=#{id}
            </if>
            <if test="lastName != null ">
                and last_name=#{lastName}
            </if>
            <if test="email != null">
                and email=#{email}
            </if>
            <if test="salary != null">
                and salary=#{salary}
            </if>
        </where>
    </select>

测试  不同的条件可以查出来

mybatis_动态SQL

 



 

  choose标签

    /**
     * 动态sql choose 标签 单条件查询 有一个条件满足就只接受一个条件 即使有多个条件满足 也无法加入进来
     * 相当于 if-else 或者 switch-case
     */
    Employee selectDByChoose(Employee employee);

mapper

    <select id="selectDByChoose" resultType="com.yu.pojo.Employee">
        select id,last_name,email,salary
        from tbl_employee
        <where>
            <choose>
                <when test="id != null">
                    id=#{id}
                </when>
                <when test="lastName != null">
                    last_name=#{lastName}
                </when>
                <when test="email != null and">
                    email=#{email}
                </when>
                <when test="salary != null and">
                    salary=#{salary}
                </when>
                <otherwise>
                    1=1
                </otherwise>
            </choose>
        </where>
    </select>

mybatis_动态SQL

 



foreach标签

第一个应用 批量查询

    /**
     * 动态sql foreach 标签  循环遍历 通过id数组来查询
     * 千万别忘了家@Param
     */
    List<Employee> selectByIdsEmp(@Param("ids") List<Integer> ids);

mapper

mybatis_动态SQL

 

<!--collection="ids"遍历的集合 item="id"遍历出来存放的临时变量 separator=","条件之间的分隔符  close=""  open=""    -->
<!--
- open: 开始字符
- close:结束字符
- separator: 元素与元素之间的分隔符
- index:
  - 迭代的是List集合: index表示的当前元素的下标
  - 迭代的Map集合:  index表示的当前元素的key
-->
    <select id="selectByIdsEmp" resultType="com.yu.pojo.Employee">
        select * from tbl_employee
        where id in (
        <foreach collection="ids" item="id" separator=",">
            #{id}
        </foreach>
        )
    </select>

测试

mybatis_动态SQL

 第二个应用

批量添加

    /**
     * 动态sql foreach 标签  批量添加案例
     */
    void insetEmp(@Param("emps") List<Employee> employees);

mapper

mybatis_动态SQL

 测试

mybatis_动态SQL

自己做个批量删除

mybatis_动态SQL

 自己犯了个错

<!--    批量删除 -->
<delete id="deleteByEmpIds">
    delete
    from tbl_employee
    where id in
    <foreach collection="ids" item="id" separator="," close=")" open="(">
        #{id}
    </foreach>

</delete>

mybatis_动态SQL

 

mybatis_动态SQL

 测试

mybatis_动态SQL

 



set标签

接口方法

mybatis_动态SQL

 

set标签 解决语句上的逗号问题 注意写在where前面

    </insert>
    <update id="update">
        update tbl_employee
        <set>
            <if test="lastName != null ">
                last_name=#{lastName},
            </if>
            <if test="email != null ">
                email=#{email},
            </if>
            <if test="salary != null ">
                salary=#{salary},
            </if>
        </set>
        where
        id=#{id}
    </update>

测试

mybatis_动态SQL

 

版权声明:程序员胖胖胖虎阿 发表于 2022年10月6日 下午5:00。
转载请注明:mybatis_动态SQL | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...