• MyBatis批量添加、修改和删除


    1、批量添加元素session.insert(String string,Object o)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public void batchInsertStudent(){
    List<Student> ls = new ArrayList<Student>();
    for(int i = 5;i < 8;i++){
    Student student = new Student();
    student.setId(i);
    student.setName("maoyuanjun" + i);
    student.setSex("man" + i);
    student.setTel("tel" + i);
    student.setAddress("浙江省" + i);
    ls.add(student);
    }
    SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
    session.insert("mybatisdemo.domain.Student.batchInsertStudent", ls);
    session.commit();
    session.close();
    }
    <insert id="batchInsertStudent" parameterType="java.util.List">
    INSERT INTO STUDENT (id,name,sex,tel,address)
    VALUES 
    <foreach collection="list" item="item" index="index" separator="," >
    (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})
    </foreach>
    </insert>

    2、批量修改session. insert (String string,Object o)

    实例1:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    public void batchUpdateStudent(){
    List<Integer> ls = new ArrayList<Integer>();
    for(int i = 2;i < 8;i++){
    ls.add(i);
    }
    SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
    session.insert("mybatisdemo.domain.Student.batchUpdateStudent",ls);
    session.commit();
    session.close();
    }
    <update id="batchUpdateStudent" parameterType="java.util.List">
    UPDATE STUDENT SET name = "5566" WHERE id IN
    <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >
    #{item}
    </foreach>
    </update>

    实例2

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    public void batchUpdateStudentWithMap(){
    List<Integer> ls = new ArrayList<Integer>();
    for(int i = 2;i < 8;i++){
    ls.add(i);
    }
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("idList", ls);
    map.put("name""mmao789");
    SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
    session.insert("mybatisdemo.domain.Student.batchUpdateStudentWithMap",map);
    session.commit();
    session.close();
    }
    <update id="batchUpdateStudentWithMap" parameterType="java.util.Map" >
    UPDATE STUDENT SET name = #{name} WHERE id IN 
    <foreach collection="idList" index="index" item="item" open="(" separator="," close=")"
    #{item} 
    </foreach>
    </update>

    3、批量删除session.delete(String string,Object o)

    public void batchDeleteStudent(){
    List<Integer> ls = new ArrayList<Integer>();
    for(int i = 4;i < 8;i++){
    ls.add(i);
    }
    SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
    session.delete("mybatisdemo.domain.Student.batchDeleteStudent",ls);
    session.commit();
    session.close();
    }
    <delete id="batchDeleteStudent" parameterType="java.util.List">
    DELETE FROM STUDENT WHERE id IN
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
    #{item}
    </foreach>
    </delete>
  • 相关阅读:
    025、MySQL字符串大小写转化函数,文本转化大写,文本转化小写
    024、MySQL字符串替换函数,文本替换函数
    023、MySQL取文本长度取字符串长度
    022、MySQL字符串的拼接
    021、MySQL变量的使用,在MySQL中创建存储过程,并添加变量
    020、MySQL创建一个存储过程,显示存储过程,调用存储过程,删除存储过程
    019、MySQL取本季度开始时间和本季度结束时间
    018、MySQL取满足日期在两个日期之间的所有数据
    017、MySQL取第4本季度开始和结束日期
    016、MySQL取本年第一季度开始日期
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/6480530.html
Copyright © 2020-2023  润新知