1.Spring-mybatis-动态SQL-foreach标签

This commit is contained in:
2025-09-26 15:16:44 +08:00
parent 0ec88d4aa3
commit 9cb8722bd4
3 changed files with 27 additions and 1 deletions

View File

@@ -61,4 +61,8 @@ public interface EmpMapper {
/*@Select("select * from emp where name like concat('%',#{name},'%') and gender = #{gender}" +
" and entrydate between #{begin} and #{end} order by update_time desc ;")*/
public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
//批量删除
public void deleteByIds(List<Integer> ids);
}

View File

@@ -3,7 +3,6 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.inmind.mapper.EmpMapper">
<!--条件查询操作-->
<select id="list" resultType="com.inmind.pojo.Emp">
select * from emp
@@ -53,4 +52,21 @@
where id = #{id}
</update>
<!--
批量删除
collection:遍历的集合(接口方法中传入的参数名)
item:集合遍历出来的元素
separator分隔符每个元素之间用什么符号相连
open遍历开始之前拼接的sql片段
close遍历结束之前拼接的sql片段
-->
<delete id="deleteByIds">
<!-- delete from emp where id in (17,18);-->
delete from emp where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
</mapper>

View File

@@ -8,6 +8,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
@SpringBootTest
@@ -90,4 +91,9 @@ class SpringbootMybatisCrudApplicationTests {
emp.setUpdateTime(LocalDateTime.now());
empMapper.update(emp);
}
@Test
void testDeleteByIds(){
empMapper.deleteByIds(Arrays.asList(19,20,21));
}
}