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>
相关推荐
古月居GYH2 小时前
【数据分析】如何在PyCharm中高效配置和使用SQL
ide·sql·pycharm
llwszx3 小时前
深入理解Java锁原理(一):偏向锁的设计原理与性能优化
java·spring··偏向锁
云泽野3 小时前
【Java|集合类】list遍历的6种方式
java·python·list
二进制person4 小时前
Java SE--方法的使用
java·开发语言·算法
小阳拱白菜5 小时前
java异常学习
java
FrankYoou6 小时前
Jenkins 与 GitLab CI/CD 的核心对比
java·docker
计算机毕设定制辅导-无忧学长6 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
麦兜*6 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了7 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
大只鹅7 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch