ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

mybatis 动态 sql

mybatis 动态 sql

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

tirme标签

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

<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属性指定
<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接口:

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); }
返回列表