mybatis plus foreach 的用法

一: foreach 用于 select * from tablename where colname in (A,B,C……);

1:service 层:

Set<String> teacherNums = new HashSet<>();
Set<String> departments = new HashSet<>();
list.stream().forEach(s->{
teacherNums.add(s.getTeacherNumber());
departments.add(s.getAcademeName());
});

List<GxyTeacherDto> gxyTeacherDtos = gxyTeacherMapper.selectTeaNumberBySchool(user.getOrgJson().getSchoolId(),user.getOrgJson().getSnowFlakeId(),teacherNums);
Set<String> allTeacherNum = gxyTeacherDtos.stream().map(GxyTeacherDto::getTeacherNumber).collect(Collectors.toSet());

2: mapper 层:
List<GxyTeacherDto> selectTeaNumberBySchool(@Param("schoolId")String schoolId,@Param("snowFlakeId")Long SnowFlakeId,@Param("list") Set<String> teaNumbers);

3:xml:
<select id="selectTeaNumberBySchool" resultType="com.zhangtao.moguding.practiceservice.dto.GxyTeacherDto" >
SELECT * FROM gxy_teacher WHERE is_deleted = 0
<if test="schoolId != null and schoolId != ''">
AND school_id = #{schoolId}
</if>
<if test="snowFlakeId != null ">
AND snow_flake_id = #{snowFlakeId}
</if>
<if test="list != null and list.size >0">
AND teacher_number in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</if>
</select>

二: update 多条sql
mapper 层:
int updateBatchByDefault(@Param("list") List<GxyPlanTeacherStudentEntity> teacherStudentEntities,@Param("tableName")String tableName);
xml:
<update id="updateBatchByDefault" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update <choose><when test="tableName != null and tableName !=''">${tableName}</when><otherwise>gxy_job</otherwise></choose>
<set>
plan_id = #{item.planId}
</set>
where student_id = #{item.studentId} and plan_id = #{item.defaultPlanId} and snow_flake_id = #{item.snowFlakeId} and is_deleted=0
</foreach>
</update>
item指 List<GxyPlanTeacherStudentEntity> 中的一个 GxyPlanTeacherStudentEntity 实例对象。

三: 多个查询sql union all:
impl层:
List<GxyJobEntity> jobEntitys = gxyJobMapper.selectJob(jobs);
mapper 层:
List<GxyJobEntity> selectJob(@Param("list") List<GxyJobEntity> jobs);
xml:

<resultMap type="com.zhangtao.moguding.practiceservice.entity.GxyJobEntity" id="GxyQuartersMap">
<result property="jobId" column="job_id"/>
<result property="planId" column="plan_id"/>
<result property="companyId" column="company_id"/>
<result property="jobName" column="job_name"/>
<result property="jobContent" column="job_content"/>
<result property="sector" column="sector"/>
<result property="category" column="category"/>
<result property="quartersIntroduce" column="quarters_introduce"/>
<result property="startTime" column="start_time"/>
<result property="endTime" column="end_time"/>
<result property="isMajorRight" column="is_major_right"/>
<result property="salary" column="salary"/>
<result property="state" column="state"/>
<result property="applyState" column="apply_state"/>
<result property="applyTeacherId" column="apply_teacher_id"/>
<result property="isAuto" column="is_auto"/>
<result property="oldJobId" column="old_job_id"/>
</resultMap>
<select id="selectJob" resultType="com.zhangtao.moguding.practiceservice.entity.GxyJobEntity" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=" union all">
select t1.*
from <choose><when test="item.tableName1 != null and item.tableName1 !=''">${item.tableName1}</when><otherwise>gxy_job</otherwise></choose> AS t1
<where>
<if test="item.studentId != null and item.studentId != ''">
AND t1.student_id = #{item.studentId}
</if>
<if test="item.planId != null and item.planId != ''">
AND t1.plan_id = #{item.planId}
</if>
<if test="item.snowFlakeId != null">
AND t1.snow_flake_id = #{item.snowFlakeId}
</if>
and t1.state=1 and t1.is_deleted=0
</where>
</foreach>
</select>
原文地址:https://www.cnblogs.com/z360519549/p/11644362.html