您好,登錄后才能下訂單哦!
今天小編給大家分享一下基于Java如何用Mybatis實(shí)現(xiàn)oracle批量插入及分頁(yè)查詢(xún)的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。
<!--簡(jiǎn)單SQL--> insert into userinfo (USERID, USERNAME, AGE) values(1001,'小明',20); <!--Mybatis寫(xiě)法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寫(xiě)法2,無(wú)序列,主鍵是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>
insert all into 的方式返回值由最后的select 決定:
<!--簡(jiǎn)單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; <!--簡(jiǎn)單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; <!--簡(jiǎn)單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寫(xiě)法1,無(wú)序列--> <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寫(xiě)法2,無(wú)序列--> <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) --> <!-- 上面帶括號(hào),下面不帶括號(hào),都可以,少量數(shù)據(jù)不帶括號(hào)效率高 --> select #{item.userid}, #{item.username}, #{item.age} from dual </foreach> </insert> <!--Mybatis寫(xiě)法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>
minvalue n (/nominvalue):最小值為n
maxvalue n (/nomaxvalue):最大值為n
start with n:從n開(kāi)始計(jì)數(shù)
increment by n:每次增加n
cache n (/nocache):緩存n個(gè)sequence值 / 不緩存,如果緩存,則會(huì)有跳號(hào)的危險(xiǎn)
noorder (/order):不保證序列號(hào)按順序生成請(qǐng)求
cycle n (/nocycle):如果到達(dá)最大值n后,再次從start with n開(kāi)始
currval:序列的當(dāng)前值,新序列必須使用一次nextval 才能獲取到值,否則會(huì)報(bào)錯(cuò)
nextval:表示序列的下一個(gè)值。新序列首次使用時(shí)獲取的是該序列的初始值,從第二次使用時(shí)開(kāi)始按照設(shè)置的步進(jìn)遞增
刪除序列語(yǔ)法: drop sequence seq_表名
<!-- create sequence 序列名 increment by 1 --每次增加幾個(gè),我這里是每次增加1 start with 1 --從1開(kāi)始計(jì)數(shù) nomaxvalue --不設(shè)置最大值 nocycle --一直累加,不循環(huán) nocache; --不建緩沖區(qū) 在插入語(yǔ)句中調(diào)用:序列名.nextval 生成自增主鍵。 --> <!--創(chuàng)建序列--> create sequence SEQ_USERINFO minvalue 1 maxvalue 9999999999 start with 1 increment by 1 nocache; <!--刪除序列--> drop sequence SEQ_USERINFO
service業(yè)務(wù)實(shí)現(xiàn):
public List<TBadUserW> queryPageBadUserInfo(TbadUserQuery queryModel) { log.info("分頁(yè)查詢(xún)請(qǐng)求參數(shù),{}", JSON.toJSONString(queryModel)); int pageNum = queryModel.getPageNum(); // 開(kāi)始頁(yè) int pageSize = queryModel.getPageSize(); // 每頁(yè)數(shù)量 queryModel.setStart((pageNum - 1) * pageSize); // 開(kāi)始行數(shù) (+1后) queryModel.setEnd(pageNum * pageSize); // 結(jié)束行數(shù) List<TBadUserW> beans = badUserWDao.queryPageBadUserInfo(queryModel); log.info("最終查詢(xún)數(shù)量:", beans.size()); return beans; }
mapper.xml文件:
<select id="queryPageInfo" parameterType="com.zznode.test.bean.TbadUserQuery" resultMap="BaseResultMap" > SELECT tt.* FROM ( <!--前端分頁(yè)需要 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 hh44:mi:ss') </if> <if test="endTime != null and endTime !=''"> and loadtime <![CDATA[<=]]> to_date(#{endTime} , 'yyyy-mm-dd hh44:mi:ss') </if> </where> )t )tt where tt.rown > #{start} and tt.rown <![CDATA[<=]]> #{end} </select>
service業(yè)務(wù)實(shí)現(xiàn):
public List<TBadUserW> queryPageBadUserInfo(TbadUserQuery queryModel) { log.info("分頁(yè)查詢(xún)請(qǐng)求參數(shù),{}", JSON.toJSONString(queryModel)); List<TBadUserW> result = new ArrayList<>(); int pageNum = queryModel.getPageNum(); // 開(kāi)始頁(yè) int pageSize = queryModel.getPageSize(); // 每頁(yè)數(shù)量(可以每頁(yè)設(shè)置為200/500/1000),每次查詢(xún)的條數(shù) boolean searchAll = true; while (searchAll){ queryModel.setStart((pageNum - 1) * pageSize); // 開(kāi)始行數(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("最終查詢(xún)數(shù)量:", result.size()); return result; }
mapper.xml文件編寫(xiě)
<!--這種寫(xiě)法是比較高效的分批查詢(xún)方法,分批不需要查詢(xún)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 hh44:mi:ss') </if> <if test="endTime != null and endTime !=''"> and loadtime <![CDATA[<=]]> to_date(#{endTime} , 'yyyy-mm-dd hh44:mi:ss') </if> </where> )t where ROWNUM <![CDATA[<=]]> #{end} )tt where tt.rown > #{start} </select>
以上就是“基于Java如何用Mybatis實(shí)現(xiàn)oracle批量插入及分頁(yè)查詢(xún)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。