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 小时前
2026 后台管理系统选型:若依、JeecgBoot、芋道、NocoBase 和 GewuMind Boot 怎么选?
java·人工智能·后端·ai·开源
码云社区5 小时前
Java家政服务系统源码:人员技能、服务范围与多商家业务落地
java·开发语言·家政服务·家政小程序·家政系统
摇滚侠6 小时前
《On Java 中文版 基础卷》阅读笔记 接口 18
java·笔记·python
CoderYanger6 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
ZhengEnCi9 小时前
覆盖索引是什么:用借书和外卖看懂回表这件事
sql·mysql·postgresql
jaysee-sjc9 小时前
【黑马点评】Day02:优惠券秒杀与分布式锁
java·分布式·nginx·spring·node.js·tomcat·maven
秋天的怀念_David10 小时前
算法一千零一夜 · 第三十五夜 · 恢复风暴
java·程序人生·算法
考虑考虑10 小时前
JDK27中的加密对象的 PEM 编码
java·后端·java ee
行者-全栈开发10 小时前
SpringBoot + GraalVM 原生编译实战:启动时间从8秒到0.5秒的极致优化
java·spring boot·后端
CoderYanger11 小时前
Java EE 进阶:10. MyBatis-Plus使用(第一弹)
java·spring boot·后端·sql·spring·java-ee·mybatis