zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    随机森林算法参数调优
    BAYES和朴素BAYES
    阿里云 金融接口 token PHP
    PHP mysql 按时间分组 表格table 跨度 rowspan
    MySql按周,按月,按日分组统计数据
    PHP 获取今日、昨日、本周、上周、本月的等等常用的起始时间戳和结束时间戳的时间处理类
    thinkphp5 tp5 会话控制 session 登录 退出 检查检验登录 判断是否应该跳转到上次url
    微信 模板消息
    php 腾讯 地图 api 计算 坐标 两点 距离 微信 网页 WebService API
    php添加http头禁止浏览器缓存
  • 原文地址:https://www.cnblogs.com/z360519549/p/11644362.html
Copyright © 2011-2022 走看看