Mybatis:Oracle批量新增、批量修改

Oracle批量新增

1、创建 SEQUENCE ,用于自增主键

sql 复制代码
CREATE SEQUENCE  TEST_SEQ
    minvalue 1
    maxvalue 9999999999999999999
    start with 1
    increment by 1
    cache 100;

2、创建实体类,用于存数据

java 复制代码
@Data
@Accessors(chain = true)
public class Student {
    /**
     * 主键(自增用TEST_SEQ)
     */
    private Long id;
    /**
     * 名字
     */
    private String name;
    
}

3、造数据,不用造自增id

java 复制代码
List<Student> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Student student = new Student();
            student.setName(String.valueOf(Math.random()));
            list.add(student);
        }
// 获取自增id
List<Student> studentList = studentMapper.selectBatchidBySequence(list);
// 批量新增
Integer studentResults = studentMapper.insertBatchStudent(mpPointsOpenList);

4、使用 SEQUENCE ,获取自增id

xml 复制代码
    <select id="selectBatchidBySequence" resultType="com.zhao.test.pojo.Student">
        select TEST_SEQ.nextval as id, t.* from (
        <foreach collection="list" index="index" item="item" separator="union all">
            select
            #{item.name,jdbcType=VARCHAR} as name
            from dual
        </foreach>
        )t
    </select>

5、批量新增

xml 复制代码
    <insert id="insertBatchStudent">
        insert into STUDENT (ID,NAME)
        select * from (
        <foreach collection="list" item="item" separator="union all">
            select #{item.id,jdbcType=BIGINT},#{item.name,jdbcType=VARCHAR}
            from dual
        </foreach>
            )
    </insert>

Oracle批量修改

还是用之前的实体类做修改

1、造数据

java 复制代码
List<Student> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Student student = new Student();
            student.setName(String.valueOf(Math.random()));
            // 这里应为数据库有的id
            student.setId((long) (Math.random() * 1000));
            list.add(student);
        }
// 批量修改
Integer studentResults = studentMapper.updateBatchStudent(list);

2、批量修改

xml 复制代码
    <update id="updateBatchStudent">
        <foreach collection="list" item="item" separator=";" open="begin" close=";end;">
            update STUDENT
            <trim prefix="set" suffixOverrides=",">
                <if test="item.name != null">NAME=#{item.name},</if>
            </trim>
            where ID = #{item.id}
        </foreach>
    </update>
相关推荐
需要8262 小时前
Spring Bean 生命周期:从实例化到销毁的完整钩子
java·后端·spring
小 黄 鸡2 小时前
HashMap 面试必会知识点详解(JDK 8)
java·面试·职场和发展
骇客野人3 小时前
在麒麟操作系统上 分别安装 jdk1.8, 达梦数据库,redis ,memcache 的操作实施方案和步骤
数据库·redis·memcached
凤山老林3 小时前
Spring Boot 接入国密 SM2/SM3/SM4 的一些实践记录
spring boot·后端·python
美狐美颜SDK开放平台4 小时前
直播APP如何实现美白滤镜功能?视频美颜SDK技术开发方案详解
前端·数据库·人工智能·音视频·美颜sdk·直播美颜sdk·视频美颜sdk
伯恩bourne4 小时前
Spring Boot Bean 注册的两种方式:@Component 家族 vs @Configuration + @Bean
java·spring boot·后端
silvamux_5 小时前
curl 测试 OpenAI 兼容 API 的完整流程
java·人工智能·缓存·ai·oneapi
省钱兄--zs5 小时前
深圳24小时自助健身房软硬件解决方案实战指南与开发流程详解
java·数据仓库·spring boot·小程序·架构
顽疲5 小时前
Java vue 养老系统源码详解:月账单 care_month_bill 与 Quartz 扣费 Job 实践
java·开发语言·vue.js·vue养老·springboot养老·java养老
Wang's Blog6 小时前
Java 项目实战: 交友项目 - 安装开发工具与环境准备
java·开发语言·交友