美文网首页
mybatis for mysql数据库 批量操作(insert

mybatis for mysql数据库 批量操作(insert

作者: 阿杰_96c5 | 来源:发表于2022-02-10 15:16 被阅读0次

    批量添加

    int addDepartmentList(List<DepartmentPO> list);
    
    insert into t_department (
        department_id, department_name,department_desc,hospital_id,insert_dt,update_dt)
    values
    <foreach collection ="list" item="department" index= "index" separator =",">
        (
         #{department.departmentId},
         #{department.departmentName},
         #{department.departmentDesc},
         #{department.hospitalId},
         #{department.insertDt},
         #{department.updateDt}
        )
    </foreach >
    

    批量修改

    int updateDepartmentList(List<DepartmentPO> list);
    
    <foreach collection ="list" item="department" index= "index" separator =";">
                UPDATE t_department
                SET department_name = #{department.departmentName}, update_dt = #{department.updateDt}, department_desc = #{department.departmentDesc}
                WHERE department_id = #{department.departmentId}
    </foreach >
    

    in/or 操作优化(mysql)

    原始查询
    select * from user where age > 30 or age < 18
    优化后查询
    select * from user where (age > 30) + (age < 18) > 0
    
    原始查询
    select  * from t_department where department_id in ('0','1')
    优化后查询
    select  * from t_department where  (department_id = 0) + (department_id = 1)  > 0
    

    mybatis 实现 in操作优化

    List<DepartmentPO> listDepartmentByIds(List<String> ids);
    select  * from  t_department
    <where>
      <foreach collection ="ids" item="id" index= "index" separator ="+">
        (department_id = #{id})
      </foreach >
        > 0
    </where>
    
    
    <foreach collection ="ids" item="id" index= "index" separator ="UNION">
      select  * from  t_doctor where id = #{id}
    </foreach >
    

    相关文章

      网友评论

          本文标题:mybatis for mysql数据库 批量操作(insert

          本文链接:https://www.haomeiwen.com/subject/lzwnkrtx.html