思路为采用Oracle 中insert语句的高级用法:INSERT ALL ,批量插入数据:
INSERT ALL举例:
1、建测试表
CREATE TABLE EDW_INT(
AGMT_NO VARCHAR2(40 BYTE) NOT NULL,
AGMT_SUB_NO VARCHAR2(4 BYTE) NOT NULL,
NEED_REPAY_INT NUMBER(22,2),
CURR_PERIOD NUMBER(4) NOT NULL
)
CREATE TABLE EDW_INT_1
(
AGMT_NO VARCHAR2(40 BYTE) NOT NULL,
AGMT_SUB_NO VARCHAR2(4 BYTE) NOT NULL,
NEED_REPAY_INT NUMBER(22,2),
CURR_PERIOD NUMBER(4) NOT NULL
)
CREATE TABLE EDW_INT_2
(
AGMT_NO VARCHAR2(40 BYTE) NOT NULL,
AGMT_SUB_NO VARCHAR2(4 BYTE) NOT NULL,
NEED_REPAY_INT NUMBER(22,2),
CURR_PERIOD NUMBER(4) NOT NULL
)
2.插入测试数据
INSERT INTO EDW_INT(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20003874', '2104', 3126.5, 7)
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20003874', '2104', 3290.76, 6)
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20003874', '2104', 3454.06, 5)
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20003874', '2104', 3616.41, 4)
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20017143', '2104', 2350.86, 0)
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20017143', '2104', 3566.55, 0)
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20018273', '2104', 1639.46, 0)
INSERT INTO EDW_INT
(AGMT_NO, AGMT_SUB_NO, NEED_REPAY_INT, CURR_PERIOD)
VALUES
('20018273', '2104', 2080.49, 0)
COMMIT
3.insert all-不带条件
insert allinto edw_int_1(agmt_no,agmt_sub_no,need_repay_int,curr_period)
values(agmt_no,agmt_sub_no,need_repay_int,curr_period)
into edw_int_2(agmt_no,agmt_sub_no,curr_period)
values(agmt_no,'1234',curr_period)
select agmt_no,agmt_sub_no,need_repay_int,curr_period from edw_int
commit
结合该问题具体分析:
mybatis 的和dao相关的sql写法如下:
<insert id="batchSave">INSERT ALL
<foreach collection="list" item="item">
INTO FSP_BUSINESS_INTEREST_REPORT
(
ID,
BUSINESS_DAY,
LOAN_NO,
CIF_NO,
CIF_NAME,
LOAN_TYPE,
REPAY_WAY,
TERM_TYPE,
VOUCH_WAY,
CHANNEL_TYPE,
LOAN_BEGIN_DATE,
LOAN_END_DATE,
RATE_BEGIN_DATE,
RATE_END_DATE,
AMT,
RATE_TYPE,
RATE,
RATE_AMT,
PAY_TYPE,
REPORT_OUT_FLAG,
REPORT_OUT_DATE,
REPORT_OUT_AMT,
CREATOR,
CREATE_TIME,
UPDATOR,
UPDATE_TIME
) VALUES (
getSeqByName('SEQ_FSP_BUS_INT_REPORT'),
#{businessDay, jdbcType=VARCHAR},
#{loanNo, jdbcType=VARCHAR},
#{cifNo, jdbcType=VARCHAR},
#{cifName, jdbcType=VARCHAR},
#{loanType, jdbcType=VARCHAR},
#{repayWay, jdbcType=VARCHAR},
#{termType, jdbcType=VARCHAR},
#{vouchWay, jdbcType=VARCHAR},
#{channelType, jdbcType=VARCHAR},
#{loanBeginDate, jdbcType=DATE},
#{loanEndDate, jdbcType=DATE},
#{rateBeginDate, jdbcType=DATE},
#{rateEndDate, jdbcType=DATE},
#{amt, jdbcType=NUMERIC},
#{rateType, jdbcType=VARCHAR},
#{rate, jdbcType=NUMERIC},
#{rateAmt, jdbcType=NUMERIC},
#{payType, jdbcType=VARCHAR},
#{reportOutFlag, jdbcType=VARCHAR},
#{reportOutDate, jdbcType=DATE},
#{reportOutAmt, jdbcType=NUMERIC},
#{creator, jdbcType=VARCHAR},
#{createTime, jdbcType=DATE},
#{updator, jdbcType=VARCHAR},
#{updateTime, jdbcType=DATE}
)
</foreach>
SELECT 1 FROM DUAL
</insert>
2.dao中接口如下定义:
void batchSave(@Param("list") List<BusinessInterestReport> list)-- Oracle SEQUENCE 序列语法:CREATE SEQUENCE 序列名
[INCREMENT BY n]
[START WITH n]
[{MAXVALUE/ MINVALUE n|NOMAXVALUE}]
[{CYCLE|NOCYCLE}]
[{CACHE n|NOCACHE}]
示例:
CREATE SEQUENCE sq_test -- 序列名
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
MAXVALUE 9999999 -- 最大值9999999
NOCYCLE -- 一直累加,不循环
NOCACHE
SELECT sq_test.NEXTVAL FROM DUAL
可以使用sequence的地方:
-- 不包含子查询、snapshot、VIEW的 SELECT 语句
-- INSERT语句的子查询中
-- NSERT语句的VALUES中
-- UPDATE 的 SET中
可以看如下例子:
INSERT INTO table_name VALUES (sq_test.nextval, 'CLERK',1200, SYSDATE)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)