• myBatis批量操作


    摘自:http://www.cnblogs.com/wuyifu/p/3745258.html

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

    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:
    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:
    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>


    4.mybatis in 参数 使用方法

    1.只有一个参数 参数的类型要声明为List或Array

    <select id="selectProduct" resultMap="Map">

    SELECT *

    FROM PRODUCT

    WHERE PRODUCTNO IN

         <foreach item="productNo" index="index" collection="参数的类型List或array">

                #{productNo}

        </foreach>

    </select>

    2.多个参数

    首先要将多个参数写入同一个map,将map作为一个参数传入mapper

    Sql配置如下:

    <select id="selectProduct" resultMap="Map">

    SELECT *

    FROM PRODUCT

    WHERE PRODUCTNO IN

         <foreach item="productNo" index="index" collection="map中集合参数的名称">

                #{productNo}

        </foreach>

    </select>

  • 相关阅读:
    解决流氓软件的工具,做个记录
    目前使用较好的网盘搜索引擎
    网页截图分段保存
    国外统计学课程主页Statistical Books, Manuals and Journals
    notepad++ TextFX替代
    只用 4 个月打造机器学习必备技能,这位工程师成功翻转职涯人生
    时序差分学习
    来自NVIDIA开源的pix2pixHD,将Image-to-Image Translation带到了另一个境界
    关于HTML5,最牛逼的10本书!
    IDEA配置和插件
  • 原文地址:https://www.cnblogs.com/zhangmu126/p/5394097.html
Copyright © 2020-2023  润新知