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 hh34:mi:ss') </if> <if test="endTime != null and endTime !=''"> and loadtime <![CDATA[<=]]> to_date(#{endTime} , 'yyyy-mm-dd hh34: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 hh34:mi:ss') </if> <if test="endTime != null and endTime !=''"> and loadtime <![CDATA[<=]]> to_date(#{endTime} , 'yyyy-mm-dd hh34:mi:ss') </if> </where> )t where ROWNUM <![CDATA[<=]]> #{end} )tt where tt.rown > #{start} </select>
以上是基於Java怎麼用Mybatis實作oracle批量插入及分頁查詢的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1
好用且免費的程式碼編輯器