基于Java 利用Mybatis实现oracle批量插入及分页查询

 更新时间:2022年07月01日 14:26:55   作者:沉淀人生  
这篇文章主要介绍了基于Java 利用Mybatis实现oracle批量插入及分页查询,文章围绕主题展开详细的内容介绍,需要的小伙伴可以参考一下

1、单条数据insert

<!--简单SQL-->
insert into userinfo (USERID, USERNAME, AGE) values(1001,'小明',20);

<!--Mybatis写法1,有序列,主键是自增ID,主键是序列-->
<insert id="insert" parameterType="com.zznode.modules.bean.UserInfo">
    <selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="userid">
      SELECT userinfo_userid_seq.nextval as userid from dual
    </selectKey>
    insert into EPG_ALARM_INFO (USERID, USERNAME, AGE)
    values (#{userid}, #{username}, #{age})
</insert>

<!--Mybatis写法2,无序列,主键是uuid,字符串-->
<insert id="insert" parameterType="com.zznode.modules.bean.UserInfo">
    insert into EPG_ALARM_INFO (USERID, USERNAME, AGE, TIME)
    values (#{userid}, #{username}, #{age}, sysdate)
</insert>

2、批量数据批量insert

insert all into 的方式返回值由最后的select 决定:

<!--简单SQL, 方法1-->
INSERT ALL 
INTO userinfo (USERID, USERNAME, AGE) values(1001,'小明',20)
INTO userinfo (USERID, USERNAME, AGE) values(1002,'小红',18)
INTO userinfo (USERID, USERNAME, AGE) values(1003,'张三',23)
select 3 from dual;
<!--简单SQL, 方法2-->
begin
    insert into userinfo (USERID, USERNAME, AGE) values(1001,'小明',20);
    insert into userinfo (USERID, USERNAME, AGE) values(1001,'小红',18);
    insert into userinfo (USERID, USERNAME, AGE) values(1001,'张三',23);
end;
<!--简单SQL, 方法3-->
insert into userinfo (USERID, USERNAME, AGE) 
select 1001, '小明', 20 from dual union all
select 1002, '小红', 18 from dual union all
select 1003, '张三', 23 from dual
<!--Mybatis写法1,无序列-->
<insert id="insertBatch" parameterType="java.util.List">
    INSERT ALL 
    <foreach collection="list" index="index" item="item">
        INTO userinfo (USERID, USERNAME, AGE)
        VALUES (#{item.userid}, #{item.username}, #{item.age})
    </foreach>
    select list.size from dual
</insert>
<!--Mybatis写法2,无序列-->
<insert id="insertBatch">
    insert into EPG_ALARM_INFO (USERID, USERNAME, AGE)
    <foreach collection="list" item="item" index="index" separator="union all">
        <!-- <foreach collection="list" item="item" index="index" separator="union all" open="(" close=")"> -->
        <!-- (select #{item.userid}, #{item.username}, #{item.age} from dual) -->
        
        <!-- 上面带括号,下面不带括号,都可以,少量数据不带括号效率高 -->
        select #{item.userid}, #{item.username}, #{item.age} from dual
    </foreach>
</insert>    
<!--Mybatis写法3,有序列-->
<insert id="insertBatch">
    insert into EPG_ALARM_INFO (USERID, USERNAME, AGE)
    SELECT userinfo_userid_seq.nextval, m.* FROM (
    <foreach collection="list" item="item" index="index" separator="union all">
        select #{item.username}, #{item.age} from dual
    </foreach>
    ) m
</insert> 

3、创建序列

  • minvalue n (/nominvalue):最小值为n
  • maxvalue n (/nomaxvalue):最大值为n
  • start with n:从n开始计数
  • increment by n:每次增加n
  • cache n (/nocache):缓存n个sequence值 / 不缓存,如果缓存,则会有跳号的危险
  • noorder (/order):不保证序列号按顺序生成请求
  • cycle n (/nocycle):如果到达最大值n后,再次从start with n开始
  • currval:序列的当前值,新序列必须使用一次nextval 才能获取到值,否则会报错
  • nextval:表示序列的下一个值。新序列首次使用时获取的是该序列的初始值,从第二次使用时开始按照设置的步进递增

删除序列语法: drop sequence seq_表名

<!--
create sequence 序列名     
       increment by 1 	--每次增加几个,我这里是每次增加1
       start with 1   	--从1开始计数
       nomaxvalue      	--不设置最大值
       nocycle         	--一直累加,不循环
       nocache;        	--不建缓冲区
在插入语句中调用:序列名.nextval  生成自增主键。
-->
<!--创建序列-->
create sequence SEQ_USERINFO
minvalue 1
maxvalue 9999999999
start with 1
increment by 1
nocache;

<!--删除序列-->
drop sequence SEQ_USERINFO

4、oracle分页查询

前端与后端交互,分页查询

service业务实现:

public List<TBadUserW> queryPageBadUserInfo(TbadUserQuery queryModel) {
    log.info("分页查询请求参数,{}", JSON.toJSONString(queryModel));
    int pageNum = queryModel.getPageNum(); // 开始页
    int pageSize = queryModel.getPageSize(); // 每页数量
    queryModel.setStart((pageNum - 1) * pageSize); // 开始行数 (+1后)
    queryModel.setEnd(pageNum * pageSize); // 结束行数
    List<TBadUserW> beans = badUserWDao.queryPageBadUserInfo(queryModel);
    log.info("最终查询数量:", beans.size());
    return beans;
}

mapper.xml文件:

<select id="queryPageInfo" parameterType="com.zznode.test.bean.TbadUserQuery"
        resultMap="BaseResultMap" >
    SELECT tt.*	FROM
    (
    	<!--前端分页需要 total总记录-->
        SELECT t.*, ROWNUM rown, COUNT (*) OVER () total FROM
        (
            select <include refid="Base_Column_List"/> from T_BAD_USER_W
            <where>
                <if test="city != null and city !=''">
                    and city = #{city}
                </if>
                <if test="county != null and county != ''">
                    and county = #{county}
                </if>
                <if test="startTime != null and startTime !=''">
                    and loadtime >= to_date(#{startTime} , 'yyyy-mm-dd hh24:mi:ss')
                </if>
                <if test="endTime != null and endTime !=''">
                    and loadtime <![CDATA[<=]]> to_date(#{endTime} , 'yyyy-mm-dd hh24:mi:ss')
                </if>
            </where>
        )t
    )tt
    where tt.rown > #{start} and tt.rown <![CDATA[<=]]> #{end}
</select>

后端海量数据导出,批量查询

service业务实现:

public List<TBadUserW> queryPageBadUserInfo(TbadUserQuery queryModel) {
    log.info("分页查询请求参数,{}", JSON.toJSONString(queryModel));
    List<TBadUserW> result = new ArrayList<>();
    int pageNum = queryModel.getPageNum(); // 开始页
    int pageSize = queryModel.getPageSize(); // 每页数量(可以每页设置为200/500/1000),每次查询的条数
    boolean searchAll = true;
    while (searchAll){
        queryModel.setStart((pageNum - 1) * pageSize); // 开始行数 (+1后)
        queryModel.setEnd(pageNum * pageSize); // 结束行数
        List<TBadUserW> beans = badUserWDao.queryPageBadUserInfo(queryModel);
        if (null == beans || beans.size() < pageSize) {
            searchAll = false;
        }
        if (CollectionUtils.isNotEmpty(beans)) {
            result.addAll(beans);
        }
        pageNum++;
    }
    log.info("最终查询数量:", result.size());
    return result;
}

mapper.xml文件编写

<!--这种写法是比较高效的分批查询方法,分批不需要查询total总量,不支持total-->
<select id="queryPageInfo" parameterType="com.zznode.test.bean.TbadUserQuery"
        resultMap="BaseResultMap" >
    SELECT tt.*	FROM
    (
        SELECT t.*, ROWNUM rown FROM
        (
            select <include refid="Base_Column_List"/> from T_BAD_USER_W
            <where>
                <if test="city != null and city !=''">
                    and city = #{city}
                </if>
                <if test="county != null and county != ''">
                    and county = #{county}
                </if>
                <if test="startTime != null and startTime !=''">
                    and loadtime >= to_date(#{startTime} , 'yyyy-mm-dd hh24:mi:ss')
                </if>
                <if test="endTime != null and endTime !=''">
                    and loadtime <![CDATA[<=]]> to_date(#{endTime} , 'yyyy-mm-dd hh24:mi:ss')
                </if>
            </where>
        )t where ROWNUM <![CDATA[<=]]> #{end}
    )tt
    where tt.rown > #{start}
</select>

到此这篇关于基于Java 利用Mybatis实现oracle批量插入及分页查询的文章就介绍到这了,更多相关Mybatis实现oracle批量插入 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 老生常谈spring boot中的定时任务

    老生常谈spring boot中的定时任务

    SpringBoot中的定时任务主要通过@Scheduled注解以及SchedulingConfigurer接口实现,本文给大家介绍spring boot中的定时任务,感兴趣的朋友跟随小编一起看看吧
    2024-05-05
  • 详解Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin)

    详解Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-mav

    这篇文章主要介绍了Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • java秒杀之redis限流操作详解

    java秒杀之redis限流操作详解

    这篇文章主要为大家详细介绍了java秒杀之redis限流操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • SpringCloud高可用配置中心Config详解

    SpringCloud高可用配置中心Config详解

    Spring Cloud Config 是一个解决分布式系统的配置管理方案,它包含了 server 和 client 两个部分,这篇文章主要介绍了SpringCloud之配置中心Config(高可用),需要的朋友可以参考下
    2022-04-04
  • 解析Java的JNI编程中的对象引用与内存泄漏问题

    解析Java的JNI编程中的对象引用与内存泄漏问题

    这篇文章主要介绍了Java的JNI编程中的对象引用与内存泄漏问题,重点讲述了局部和全局引用时一些值得注意的地方,需要的朋友可以参考下
    2015-11-11
  • Springboot+Bootstrap实现增删改查实战

    Springboot+Bootstrap实现增删改查实战

    这篇文章主要介绍了Springboot+Bootstrap实现增删改查实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • 深入理解happens-before和as-if-serial语义

    深入理解happens-before和as-if-serial语义

    本文大部分整理自《Java并发编程的艺术》,温故而知新,加深对基础的理解程度。下面可以和小编来一起学习下
    2019-05-05
  • Java线程等待唤醒几种方法小结

    Java线程等待唤醒几种方法小结

    线程等待和唤醒有三种实现方法,分别是Object类中的wait、notify,Condition类中的await、signal,LockSupport类中的park、unpark方法,感兴趣的可以了解一下
    2023-10-10
  • Java HashMap中除了死循环之外的那些问题

    Java HashMap中除了死循环之外的那些问题

    这篇文章主要介绍了Java HashMap中除了死循环之外的那些问题,这些问题大致可以分为两类,程序问题和业务问题,下面文章我们一个一个来看,需要的小伙伴可以参考一下
    2022-05-05
  • java项目实现统一打印入参出参等日志

    java项目实现统一打印入参出参等日志

    这篇文章主要介绍了java项目实现统一打印入参出参等日志方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03

最新评论