欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Java?利用Mybatis實現(xiàn)oracle批量插入及分頁查詢

 更新時間:2022年07月01日 14:26:55   作者:沉淀人生  
這篇文章主要介紹了基于Java?利用Mybatis實現(xiàn)oracle批量插入及分頁查詢,文章圍繞主題展開詳細的內(nèi)容介紹,需要的小伙伴可以參考一下

1、單條數(shù)據(jù)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、批量數(shù)據(jù)批量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) -->
? ? ? ??
? ? ? ? <!-- 上面帶括號,下面不帶括號,都可以,少量數(shù)據(jù)不帶括號效率高 -->
? ? ? ? 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、創(chuàng)建序列

  • minvalue n (/nominvalue):最小值為n
  • maxvalue n (/nomaxvalue):最大值為n
  • start with n:從n開始計數(shù)
  • 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開始計數(shù)
       nomaxvalue      	--不設置最大值
       nocycle         	--一直累加,不循環(huán)
       nocache;        	--不建緩沖區(qū)
在插入語句中調(diào)用:序列名.nextval  生成自增主鍵。
-->
<!--創(chuàng)建序列-->
create sequence SEQ_USERINFO
minvalue 1
maxvalue 9999999999
start with 1
increment by 1
nocache;

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

4、oracle分頁查詢

前端與后端交互,分頁查詢

service業(yè)務實現(xiàn):

public List<TBadUserW> queryPageBadUserInfo(TbadUserQuery queryModel) {
    log.info("分頁查詢請求參數(shù),{}", JSON.toJSONString(queryModel));
    int pageNum = queryModel.getPageNum(); // 開始頁
    int pageSize = queryModel.getPageSize(); // 每頁數(shù)量
    queryModel.setStart((pageNum - 1) * pageSize); // 開始行數(shù) (+1后)
    queryModel.setEnd(pageNum * pageSize); // 結(jié)束行數(shù)
    List<TBadUserW> beans = badUserWDao.queryPageBadUserInfo(queryModel);
    log.info("最終查詢數(shù)量:", 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>

后端海量數(shù)據(jù)導出,批量查詢

service業(yè)務實現(xiàn):

public List<TBadUserW> queryPageBadUserInfo(TbadUserQuery queryModel) {
    log.info("分頁查詢請求參數(shù),{}", JSON.toJSONString(queryModel));
    List<TBadUserW> result = new ArrayList<>();
    int pageNum = queryModel.getPageNum(); // 開始頁
    int pageSize = queryModel.getPageSize(); // 每頁數(shù)量(可以每頁設置為200/500/1000),每次查詢的條數(shù)
    boolean searchAll = true;
    while (searchAll){
        queryModel.setStart((pageNum - 1) * pageSize); // 開始行數(shù) (+1后)
        queryModel.setEnd(pageNum * pageSize); // 結(jié)束行數(shù)
        List<TBadUserW> beans = badUserWDao.queryPageBadUserInfo(queryModel);
        if (null == beans || beans.size() < pageSize) {
            searchAll = false;
        }
        if (CollectionUtils.isNotEmpty(beans)) {
            result.addAll(beans);
        }
        pageNum++;
    }
    log.info("最終查詢數(shù)量:", 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實現(xiàn)oracle批量插入及分頁查詢的文章就介紹到這了,更多相關Mybatis實現(xiàn)oracle批量插入 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java 迪杰斯特拉算法實現(xiàn)查找最短距離的實現(xiàn)

    Java 迪杰斯特拉算法實現(xiàn)查找最短距離的實現(xiàn)

    這篇文章主要介紹了Java 迪杰斯特拉算法實現(xiàn)查找最短距離的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • java讀取文件內(nèi)容,解析Json格式數(shù)據(jù)方式

    java讀取文件內(nèi)容,解析Json格式數(shù)據(jù)方式

    這篇文章主要介紹了java讀取文件內(nèi)容,解析Json格式數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • springboot的SpringPropertyAction事務屬性源碼解讀

    springboot的SpringPropertyAction事務屬性源碼解讀

    這篇文章主要介紹了springboot的SpringPropertyAction事務屬性源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • IDEA巧用Postfix Completion讓碼速起飛(小技巧)

    IDEA巧用Postfix Completion讓碼速起飛(小技巧)

    這篇文章主要介紹了IDEA巧用Postfix Completion讓碼速起飛,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • JDBC 使用說明(流程、架構(gòu)、編程)

    JDBC 使用說明(流程、架構(gòu)、編程)

    這篇文章主要介紹了JDBC 使用說明,需要的朋友可以參考下
    2015-08-08
  • springboot 多模塊將dao(mybatis)項目拆分出去

    springboot 多模塊將dao(mybatis)項目拆分出去

    這篇文章主要介紹了springboot 多模塊將dao(mybatis)項目拆分出去,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 詳細全面解析Java泛型

    詳細全面解析Java泛型

    這篇文章主要介紹了詳細全面解析Java泛型,java泛型主要提高了Java 程序的類型安全,通過知道使用泛型定義的變量的類型限制,編譯器可以驗證類型假設,消除源代碼中的許多強制類型轉(zhuǎn)換等多個有點,下面我們進入文章了解更多的詳細內(nèi)容吧
    2022-02-02
  • springboot+mybatis如何屏蔽掉mybatis日志

    springboot+mybatis如何屏蔽掉mybatis日志

    這篇文章主要介紹了springboot+mybatis如何屏蔽掉mybatis日志問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 教你Java中的Lock鎖底層AQS到底是如何實現(xiàn)的

    教你Java中的Lock鎖底層AQS到底是如何實現(xiàn)的

    本文是基于ReentrantLock來講解,ReentrantLock加鎖只是對AQS的api的調(diào)用,底層的鎖的狀態(tài)(state)和其他線程等待(Node雙向鏈表)的過程其實是由AQS來維護的,對Java?Lock鎖AQS實現(xiàn)過程感興趣的朋友一起看看吧
    2022-05-05
  • Elasticsearch 映射參數(shù)詳解 fields

    Elasticsearch 映射參數(shù)詳解 fields

    這篇文章主要介紹了fields Elasticsearch 映射參數(shù)fields,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評論