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>
  • 相关阅读:
    范仁义js课程---26、循环结构(while循环)
    解决Failed to parse SourceMap: http:xxx 问题
    范仁义js课程---25、switch选择结构
    范仁义js课程---24、条件运算符
    范仁义js课程---23、if选择结构小实例
    范仁义js课程---22、选择结构(if)
    javascript疑难问题---4、NaN的相等性判断
    范仁义js课程---21、js运算符优先级
    android L新控件RecyclerView详解与DeMo[转]
    Color Cube – 国产的优秀配色取色工具
  • 原文地址:https://www.cnblogs.com/zrbfree/p/7262108.html
Copyright © 2011-2022 走看看