动态SQL

动态SQL

1.利用Maven创建工程后,配置pom.xml以及mybatils相关文件。
2.sql映射文件中书写动态SQL来实现雇员表的查询。
雇员表:
在这里插入图片描述
EmployeeDao中的方法:
在这里插入图片描述
3.if,where。where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。
第一种方式使用where和if来实现动态SQL:
1.注意所有字母和标点符号均为英文状态下的。
2.在每一个if语句前加and。
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!--sql映射文件:约束 sql映射相关的标签   -->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- namespace 和 dao层的相关接口的全限定名保持一致即可-->
<mapper namespace="com.bdqn.dao.EmployeeDao">

<select id="getEmpList" resultType="com.bdqn.entity.Employee">
    SELECT * FROM l_employee
    <where>
        <if test="employee.name !=null and employee.name !=''">
            and `name` like #{employee.name}
        </if>
        <if test="employee.salary!=null and employee.salary!=0">
             and salary>#{employee.salary}
         </if>
    </where>

</select>
</mapper>

第二种方式只是用if:
1.在WHERE后加上 恒成立的条件比如1=1,if语句与前面一样

<?xml version="1.0" encoding="UTF-8" ?>
<!--sql映射文件:约束 sql映射相关的标签   -->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- namespace 和 dao层的相关接口的全限定名保持一致即可-->
<mapper namespace="com.bdqn.dao.EmployeeDao">

<select id="getEmpList" resultType="com.bdqn.entity.Employee">
    SELECT * FROM l_employee WHERE 1=1
    <if test="employee.name !=null and employee.name !=''">
         and `name` like #{employee.name}
    </if>
    <if test="employee.salary!=null and employee.salary!=0">
        and salary>#{employee.salary}
    </if>

</select>

</mapper>

4.choose、when、otherwise。有时候,我们不想使用所有的条件,而只是想从多个条件中选择一个使用。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。when相当于if,otherwise相当于else

<?xml version="1.0" encoding="UTF-8" ?>
<!--sql映射文件:约束 sql映射相关的标签   -->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- namespace 和 dao层的相关接口的全限定名保持一致即可-->
<mapper namespace="com.bdqn.dao.EmployeeDao">

<select id="getEmpList" resultType="com.bdqn.entity.Employee">
    SELECT * FROM l_employee WHERE
    <choose>
         <when test="employee.name !=null and employee.name !=''">
              `name` like #{employee.name}
         </when>
         <otherwise>
              salary>#{employee.salary}
         </otherwise>
     </choose>

</select>
</mapper>

5.set:用于动态更新语句。set 元素可以用于动态包含需要更新的列,忽略其它不更新的列。

<?xml version="1.0" encoding="UTF-8" ?>
<!--sql映射文件:约束 sql映射相关的标签   -->
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!-- namespace 和 dao层的相关接口的全限定名保持一致即可-->
<mapper namespace="com.bdqn.dao.EmployeeDao">

    <update id="updateEmp">
        UPDATE l_employee
        <set>
            <if test="`name` != null and `name` !=''" >name=#{name},</if>
            <if test="deptCode != null">dept_code=#{deptCode},</if>
            <if test="hireDate!= null">hire_date=#{hireDate},</if>
            <if test="salary != null">salary=#{salary}</if>
            <if test="phoneNumber != null">phone_number=#{phoneNumber}</if>
            <if test="managerId != null">manager_id=#{managerId}</if>
        </set>
        WHERE
            employee_id =#{employeeId}
    </update>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值