mybatis获取一个list怎么添加到数据库里
发布网友
发布时间:2022-04-27 09:20
我来回答
共3个回答
懂视网
时间:2022-04-10 10:45
- 在数据库中填加新表
- 在总配置文件中加入子mapper地址
-
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.nuanshui.heatedloan.credit.dal.mapper.CreditChangeLogMapper" >//注意此处一定要与添加的类一致
<resultMap id="creditChangeLogInfo" type="com.nuanshui.heatedloan.credit.model.CreditChangeLogInfo" >//注此处model也要与定义处一致
<id column="id" property="id" typeHandler="idHandler" />
<result column="user_id" property="userId" />
<result column="order_id" property="orderId"/>
<result column="type" property="type"/>
<result column="original_credit" property="originalCredit"/>
<result column="new_credit" property="newCredit"/>
<result column="log" property="log"/>
<result column="created_at" property="createdAt" jdbcType="TIMESTAMP" />
<result column="updated_at" property="updatedAt" jdbcType="TIMESTAMP" />
</resultMap>
<insert id="batchInsertCreditChangeLogList" parameterType="java.util.List">
<selectKey resultType="long" keyProperty="idRaw" >
SELECT LAST_INSERT_ID() AS id
</selectKey>
insert into credit_change_log (user_id, order_id, type, original_credit, new_credit, log, created_at, updated_at) values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.userId},#{item.orderId},#{item.type},#{item.originalCredit},#{item.newCredit},
#{item.log},now(),now())
</foreach>
</insert>
</mapper>
mybatis加数据库表
标签:origin red tty dha model sep creat 1.0 3.0
热心网友
时间:2022-04-10 07:53
思路为采用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 all
into 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 的和相关的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.中接口如下定义:
void batchSave(@Param("list") List<BusinessInterestReport> list);
热心网友
时间:2022-04-10 09:11
遍历list,一个一个存入数据库就是了