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>
  • 相关阅读:
    EasyHook库系列使用教程之四钩子的启动与停止
    inputclean插件的使用方法
    机器学习(十三)——机器学习中的矩阵方法(3)病态矩阵、协同过滤的ALS算法(1)
    &lt;LeetCode OJ&gt; 204. Count Primes
    leetcode 235: Lowest Common Ancestor of a Binary Search Tree
    数据结构经常使用算法
    调侃物联网开源框架,我们什么时候也来开源一个?
    字符编码简单介绍
    PriorityQueue ,ArrayList , 数组排序
    从struts2.1开始Convention零配置
  • 原文地址:https://www.cnblogs.com/zrbfree/p/7262108.html
Copyright © 2011-2022 走看看