mybatis 动态 sql

where标签;sql片段;trim标签;choose标签;set标签;foreach标签。

tirme标签

trim 一般用于去除 SQL 语句中多余的 AND 关键字、逗号,或者给 SQL 语句前拼接 where、set 等后缀,可用于选择性插入、更新、删除或者条件查询等操作。trim 语法格式如下。

XML 复制代码
<trim prefix="前缀" suffix="后缀" prefixOverrides="忽略前缀字符" suffixOverrides="忽略后缀字符">
    SQL语句
</trim>

trim标签有四个属性:prefix,prefixOverrides,suffix,suffixOverrides

|-----------------|-----------------------------------------------------------------------------------------------|
| prefix | 给sql语句拼接的前缀 |
| suffix | 给sql语句拼接的后缀 |
| prefixOverrides | 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为"AND",当sql语句的开头为"AND",trim标签将会去除该"AND" |
| suffixOverrides | 去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定 |

XML 复制代码
<mapper namespace="com.gcy.mapper.EmployeeMapper">

    <select id="queryByNameAndSalary" resultType="employee">
        select * from t_emp

<!--        where标签 的作用有两个:
                1.自动添加where 关键字 where 内部有任何一个if满足,自动添加where关键字,都不满足则去掉where
                2.自动去掉多余的and 和 or关键字
-->
        <where>
            <if test="name != null">
                emp_name = #{name}
            </if>
            <if test="salary != null">
                and emp_salary >= #{salary}
            </if>
        </where>
    </select>


<!-- sql 片段-->
    <sql id="selectSql">
        select * from t_emp
    </sql>

    <select id="queryByNameAndSalaryTrim" resultType="employee">
<!--        引用sql片段 -->
        <include refid="selectSql"/>
        <trim prefix="where" prefixOverrides="and|or">
            <if test="name != null">
                emp_name = #{name}
            </if>
            <if test="salary != null">
                and emp_salary >= #{salary}
            </if>
        </trim>
    </select>


    <select id="queryByNameAndSalaryChoose" resultType="employee">
        select * from t_emp
        where
        <choose>
            <when test="name != null">
                emp_name = #{name}
            </when>
            <when test="salary != null">
                emp_salary = #{salary}
            </when>
            <otherwise>1=1</otherwise>
        </choose>
    </select>
    
    <!--    <set> :1.自动去掉多余的逗号
                    2.自动添加set关键字 -->
    <update id="updateEmp">
        update t_emp
        <set>
            <if test="empName != null">
                emp_name = #{empName}
            </if>
            <if test="empSalary!=null">
                emp_salary = #{empSalary}
            </if>
        </set>
        where emp_id = #{empId}
    </update>


    <update id="updateEmpTrim">
        update t_emp
        <trim prefix="set" suffixOverrides=",">
            <if test="empName != null">
                emp_name = #{empName},
            </if>
            <if test="empSalary!=null">
                emp_salary = #{empSalary}
            </if>
        </trim>
        where emp_id = #{empId}
    </update>



<!--    根据id 批量查询-->
    <select id="queryBatch" resultType="employee">
        select * from t_emp where emp_id in
<!--        collection="ids(方法参数别名|arg0|list)"
             open 遍历之前要追加的字符串
             close 遍历结束需要添加的字符串
             separator 每次遍历的分割符号!如果是最后一次,不会追加
             item 获取每个遍历项
-->
          <foreach collection="ids" open="(" separator="," close=")" item="id">
<!--              遍历的内容, #{遍历项 item指定的key}-->
              #{id}
          </foreach>
    </select>


    <insert id="insertBatch">
        insert into t_emp (emp_name, emp_salary) values
        <foreach collection="list" separator="," item="employee">
            (#{employee.empName},#{employee.empSalary})
        </foreach>
    </insert>

<!--    如果一个标签涉及多个语句,需要在数据库连接url上添加参数:allowMultiQueries=true 允许多语句-->
    <update id="updateBatch">
        <foreach collection="list" item="emp">
            update t_emp set emp_name=#{emp.empName}, emp_salary=#{emp.empSalary} where emp_id = #{emp.empId};
        </foreach>
    </update>
    
</mapper>

mapper接口:

java 复制代码
public interface EmployeeMapper {

    List<Employee> queryByNameAndSalary(@Param("name") String name, @Param("salary") Double salary);

    List<Employee> queryByNameAndSalaryTrim(@Param("name") String name, @Param("salary") Double salary);


    //根据员工id更新员工的数据,我们要求传入的name和salary不为null时才更新
    int updateEmp(Employee employee);

    int updateEmpTrim(Employee employee);

    //根据两个条件查询,如果姓名不为null使用姓名查询,如果姓名为null,薪水不为null就使用薪水查询!都为null就查询全部。
    List<Employee> queryByNameAndSalaryChoose(@Param("name") String name, @Param("salary") Double salary);


    //根据id 批量查询
    List<Employee> queryBatch(@Param("ids") List<Integer> ids);

    //根据id 批量删除
    int deleteBatch(@Param("ids") List<Integer> ids);

    //批量插入
    int insertBatch(@Param("list") List<Employee> employeeList);

    //批量更新
    int updateBatch(@Param("list") List<Employee> employeeList);

}
相关推荐
瑞码空间2 小时前
JSP与Servlet核心开发与CRUD实战笔记
java·笔记·servlet·jsp
xcLeigh2 小时前
KingbaseES数据库教程:初识数据库,从安装到跑通第一条SQL
android·数据库·sql·安装·数据库教程·kes
知守观2 小时前
技术考古:iBATIS 2.3.4 时代的持久层,是如何用 Map 玩转 307 个存储过程的?
java·后端·mybatis
captain3763 小时前
MyBatis Plus(1)
java·服务器·mybatis
香蕉大香蕉(06敬畏市场)4 小时前
MySQL 单点详解(08)
java·数据库·mysql·面试·八股
thefool1122664 小时前
HTTPS协议
java
acd120094 小时前
Nginx 反向代理的缓冲区坑:后端响应大时接口偶发 502
java·运维·nginx
装不满的克莱因瓶4 小时前
支付订单成功异步MQ权益发放设计和开发
java·架构·系统架构·rabbitmq·硬件架构
高冷小鱼殿下4 小时前
内部类Lambda与枚举
java·开发语言