🌺前言
展示?动态sql标签的用法
1.if标签
xml
<select id="getEmpListByParam" resultType="Emp">
select * from t_emp where 1=1
<if test="empName!=null and empName!=''">
and emp_name = #{empName}
</if>
<if test="age!=null and age!=''">
and age = #{age}
</if>
<if test="gender!=null and gender!=''">
and gender = #{gender}
</if>
</select>
2.where标签往往结合if一起使用
where:
- 如果有if成立则声称where关键字
- 去掉前面多余的and,后面多余的不会去掉
- 如果if都不成立,where不起作用.
xml
<select id="getEmpListByParam" resultType="Emp">
select * from t_emp
<where>
<if test="empName!=null and empName!=''">
emp_name = #{empName}
</if>
<if test="age!=null and age!=''">
and age = #{age}
</if>
<if test="gender!=null and gender!=''">
and gender = #{gender}
</if>
</where>
</select>
3.trim标签
- prefix添加前面的关键字
- prefixOverrides去掉sql前面多余的关键字
- suffix添加后面面的关键字
- suffixOverrides去掉sql后面面多余的关键字
xml
<select id="getEmpListByParam" resultType="Emp">
select * from t_emp
<trim prefix="where" prefixOverrides="and">
<if test="empName!=null and empName!=''">
emp_name = #{empName}
</if>
<if test="age!=null and age!=''">
and age = #{age}
</if>
<if test="gender!=null and gender!=''">
and gender = #{gender}
</if>
</trim>
</select>
4.choose when otherwise
- 相当于if..else if...else
- when最少写一次
- otherwise最多写一次
- 内部不用加and自己会生成
- 只会走一个分支
xml
<select id="getEmpListByChoose" resultType="Emp">
select * from t_emp
<where>
<choose>
//if
<when test="empName!=null and empName!=''">
emp_name = #{empName}
</when>
//else if
<when test="age!=null and age!=''">
age = #{age}
</when>
//else
<otherwise>
gender = #{gender}
</otherwise>
</choose>
</where>
</select>
5.foreach
- collection:循环的list
- item:项,
- separator:分隔符
- open :整个循环以啥开始的
- close :一啥结束
- index:索引
批量添加
java
void batchInsertEmp(@Param("emps") List<Emp> emps);
xml
<insert id="batchInsertEmp">
insert into t_emp values
<foreach collection="emps" item="emp" separator=",">
(null ,#{emp.empName},#{emp.age},#{emp.gender},null )
</foreach>
</insert>
java
public void testBatchInsertEmp(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = new Emp(null,"小明1",34,"男");
Emp emp1 = new Emp(null,"小明2",34,"男");
Emp emp2 = new Emp(null,"小明3",34,"男");
List<Emp> list = Arrays.asList(emp,emp1,emp2);
mapper.batchInsertEmp(list);
}
批量删除
java
void batchDeleteEmpByIds(@Param("ids") Integer [] ids);
java
public void testBatchDeleteEmp(){
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
Integer [] ids = new Integer []{6,7};
mapper.batchDeleteEmpByIds(ids);
}
方法1
xml
<delete id="batchDeleteEmpByIds">
delete from t_emp where emp_id in
(
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>
)
</delete>
方法2
xml
<delete id="batchDeleteEmpByIds">
delete from t_emp where emp_id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
方法3
xml
<delete id="batchDeleteEmpByIds">
delete from t_emp where
<foreach collection="ids" item="id" separator="or">
emp_id = #{id}
</foreach>
</delete>
6.sql,include
目录
文章最后更新于 2024-08-06 20:57:38
作者:徐徐版权声明:转载请注明文章出处