zoukankan      html  css  js  c++  java
  • 170515、mybatis批量操作

    //Java代码

    public void batchAdd(){
    SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
    StudentMappper mapper = sqlSession.getMapper(StudentMappper.class);
    List<Student> list = new ArrayList<Student>();
    Student student1 = new Student();
    student1.setName("新增" + new Random().nextInt(10));
    student1.setAge(16);

    Student student2 = new Student();
    student2.setName("新增" + new Random().nextInt(10));
    student2.setAge(16);
    list.add(student1);
    list.add(student2);

    int result = mapper.batchAdd(list);
    sqlSession.commit();
    if (result > 0) {
    System.out.println("----------batchAdd success-----");
    } else {
    System.out.println("----------batchAdd fail---------");
    }
    }


    <!-- 批量新增 --> <insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" > <selectKey resultType="int" keyProperty="id" order="AFTER"> select LAST_INSERT_ID() </selectKey> insert into t_student (name,age) values <foreach collection="list" item="item" index="index" separator=","> (#{item.name},${item.age}) </foreach> </insert>
    //Java代码

    public void batchDelList(){
    SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
    StudentMappper mapper = sqlSession.getMapper(StudentMappper.class);
    List<Integer> list = new ArrayList<Integer>();
    list.add(23);
    list.add(24);
    int result = mapper.batchDelStr(list);
    sqlSession.commit();
    if (result > 0) {
    System.out.println("----------batchDel success-----");
    } else {
    System.out.println("----------batchDel fail---------");
    }
    }

        <!-- 批量删除-list -->
        <delete id="batchDelStr" parameterType="java.util.List">
            delete from t_student 
            where id in
            <foreach collection="list" item="item" open="(" close=")" separator=",">
                #{item} 
            </foreach>
        </delete>
        //Java代码

    public void batchDelArray(){
    SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
    StudentMappper mapper = sqlSession.getMapper(StudentMappper.class);
    int[] ids = new int[]{27,28};
    int result = mapper.batchDelArrays(ids);
    sqlSession.commit();
    if (result > 0) {
    System.out.println("----------batchDel success-----");
    } else {
    System.out.println("----------batchDel fail---------");
    }
    }

        <!-- 批量删除-array -->
        <delete id="batchDelArrays" parameterType="int">
            delete from t_student
            where id in
            <foreach collection="array" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </delete>
  • 相关阅读:
    PBRT笔记(3)——KD树
    PBRT笔记(2)——BVH
    PBRT笔记(1)——主循环、浮点误差
    《Ray Tracing in One Weekend》、《Ray Tracing from the Ground Up》读后感以及光线追踪学习推荐
    在Node.js中使用ffi调用dll
    Node.js c++ 扩展之HelloWorld
    在Qt中配置TBB以及简单实用
    对《将Unreal4打包后的工程嵌入到Qt或者桌面中》一文的补充
    QtQuick大坑笔记之Http的Get与Post操作(带cookie)
    QtQuick自定义主题以及控件样式指引
  • 原文地址:https://www.cnblogs.com/zrbfree/p/7262108.html
Copyright © 2011-2022 走看看