摘要:本文主要向大家介绍了JAVA语言的mybatis单笔批量保存实体数据的方法,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
本文主要向大家介绍了JAVA语言的mybatis单笔批量保存实体数据的方法,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
一,xml样例
<cisReport reportID="报告编号" buildEndTime="报告生成结束时间,格式YYYY-MM-DD HH24:mm:ss" queryReasonID="查询原因ID,详见数据字典" subReportTypes="查询的收费子报告ID,多个收费子报告ID用逗号分隔" treatResult="对应的收费子报告收费次数,与subReportTypes一一对应,为大于等于0的值的集合,用逗号分隔"
subReportTypesShortCaption="查询的收费子报告中文名称 " refID="引用ID,为查询申请条件中的引用ID" hasSystemError="有否系统错误,true:有错误,false:无错误"
isFrozen="该客户是否被冻结,true:被冻结,false:未被冻结">
复制代码
二,表结构设计
1,批次号:各表之间用最外层批次号关联。
2,主表:即把各层字段全部存在一个表中。
三,实体设计
分为三层:CisReportRoot ,CisReportChild ,ReportElement
public class CisReportRoot extends BasePojo implements Serializable {
private String docId;
private String batNo;//查询批次号
@JSONField(serialize = false)
private String xmlId;
private CisReportChild cisReportChild;
/**
* 2017/7/21.
* 报告节点属性以及子节点
*/
public class CisReportChild extends BasePojo {
/**
* 报告生成结束时间
**/
private String buildEndTime;
/**
* 是否有系统错误 true表示有 false表示没有
**/
private String hasSystemError;
/**
* 该客户是否被冻结
**/
private String isFrozen;
private ReportElement reportElement;
public class ReportElement extends BasePojo {
private String phone;
private PoliceCheckInfo policeCheckInfo;//身份认证
private MobileCheckInfo mobileCheckInfo;//个人手机号码核查
private PersonAntiSpoofingDescInfo personAntiSpoofingDescInfo;//个人反欺诈风险综述信息
private PersonAntiSpoofingInfo personAntiSpoofingInfo;//个人反欺诈风险评分信息
private MobileStatusInfo mobileStatusInfo;//手机号码状态信息
private EconnoisserurInfo econnoisserurInfo;//是否命中羊毛党名单
private FraudRiskInfo fraudRiskInfo;//是否命中欺诈风险名单
private PersonRiskAssess personRiskAssess;//是否命中高风险名单
private MicroNearlyThreeYearsOverdueInfo microNearlyThreeYearsOverdueInfo;//网贷逾期信息
private PersonRiskInfo personRiskInfo;//风险信息
private HistorySimpleQueryInfo historySimpleQueryInfo;//个人近两年历史查询记录
复制代码
四,代码
<1>单笔,批量无需主键
无论单笔还是批量都可以把数据存在map中,sql中通过map的键获取数据。
如:
private void savePersonRiskInfo(String batNo, PersonRiskInfo personRiskInfo) throws Exception {
Map
map.put("batNo", batNo);
//单笔
map.put("item", personRiskInfo);
pyMapper.savePersonRiskInfo(map);
if (!ListUtil.isEmpty(personRiskInfo.getAlList())) {
//批量
map.put("list", personRiskInfo.getAlList());
pyMapper.saveAlInfoList(map);
}
复制代码
sql:
//单笔
INSERT INTO TABLE_PERSON_RISK_INFO(BATNO,TOTLECOUNT,ALCOUNT,ZXCOUNT,SXCOUNT,SWCOUNT,CQGGCOUNT,WDYQCOUNT)
VALUES (#{batNo,jdbcType=VARCHAR},
#{item.totleCount,jdbcType=NUMERIC},
#{item.alCount,jdbcType=NUMERIC},#{item.zxCount,jdbcType=NUMERIC},#{item.sxCount,jdbcType=NUMERIC},
#{item.swCount,jdbcType=NUMERIC},#{item.cqggCount,jdbcType=NUMERIC},#{item.wdyqCount,jdbcType=NUMERIC})
//批量
INSERT INTO TABLE_MNTYOI (BATNO,OVERDUEAMOUNT,OVERDUEDAYS,OVERDUECOUNT)
(
SELECT #{batNo,jdbcType=VARCHAR},
#{item.overdueAmount,jdbcType=VARCHAR},
#{item.overdueDays,jdbcType=VARCHAR},
#{item.overdueCount,jdbcType=NUMERIC}
FROM DUAL
)
private CisReportRoot invokePy(PyQueryBean pyQueryBean) throws Exception {
pyQueryBean.setQueryFromPy(QUERY_FROM_PY);
//call py service
CisReportRoot cisReportRoot = queryCisReportFromPyServer(pyQueryBean);
cisReportRoot.getCisReportChild().getReportElement().setPhone(pyQueryBean.getPhone());
//cacheService.save(cisReportRoot);
//(1)保存数据
saveCisReport(cisReportRoot);
return cisReportRoot;
}
复制代码
(1)保存数据
@Transactional(rollbackFor = Exception.class)
@Override
public CisReportRoot saveCisReport(CisReportRoot cisReportRoot) throws Exception {
try {
ReportElement reportElement = cisReportRoot.getCisReportChild().getReportElement();
PersonRiskInfo personRiskInfo = reportElement.getPersonRiskInfo();
//(2)主表,保存xml所有节点信息(不包含个人风险信息),保存单笔数据
pyMapper.saveCisReport(cisReportRoot);
if (SUCCESS.equals(cisReportRoot.getCisReportChild().getReportElement().getPersonRiskInfo().getTreatResult())) {
savePersonRiskInfo(cisReportRoot.getBatNo(), personRiskInfo);
}
if (cisReportRoot.getCisReportChild().getReportElement().getHistorySimpleQueryInfo().getTreatResult().equals(SUCCESS)) {
Map
//(3)保存批量数据
map.put("batNo", cisReportRoot.getBatNo());
if (!ListUtil.isEmpty(cisReportRoot.getCisReportChild().getReportElement().getHistorySimpleQueryInfo().getItemList())) {
map.put("list", cisReportRoot.getCisReportChild().getReportElement().getHistorySimpleQueryInfo().getItemList());
pyMapper.saveHistorySimpleQueryInfoList(map);
}
if (!ListUtil.isEmpty(cisReportRoot.getCisReportChild().getReportElement().getMicroNearlyThreeYearsOverdueInfo().getList())) {
map.put("list", cisReportRoot.getCisReportChild().getReportElement().getMicroNearlyThreeYearsOverdueInfo().getList());
pyMapper.saveMicroNearlyThreeYearsOverdueInfoList(map);
}
}
return cisReportRoot;
} catch (Exception e) {
logger.error("saveCisReport@PyserviceImpl_Exception", e);
throw e;
}
}
复制代码
(2)保存主表
INSERT INTO TABLE_CISREPORT
(BATNO,BUILDENDTIME,ISFROZEN,
MCI_NAMECHECKRESULT,MCI_AREAINFO,MCI_DOCUMENTNOCHECKRESULT,MCI_OPERATOR,MCI_PHONECHECKRESULT,
PCI_DOCUMENTNO,PCI_NAME,PCI_RESULT,
PASDI_DESC,
PASI_HITTYPES,PASI_RISKLEVEL,PASI_RISKSCORE,PASI_RISKSUGGEST,
MS_AREAINFO,MS_CAnCELTIME,MS_OPERATOR,MS_PHONESTATUS,MS_TIMELENGTH,
ECONNOISSERUR,
FRAUDRISK,
PRA_CHECKRESULT,
MNTYOI_OVERDUEAMOUNT,MNTYOI_OVERDUECOUNT,MNTYOI_OVERDUEDAYS,
HS_LAST1MONTH,HS_LAST3MONTH,HS_LAST6MONTH,HS_LAST12MONTH,HS_LAST18MONTH,HS_LAST24MONTH,
PHONE,XMLID)
VALUES
(#{batNo,jdbcType=VARCHAR},
to_date(#{cisReportChild.buildEndTime},'yyyy-MM-dd hh24:mi:ss'),
#{cisReportChild.isFrozen,jdbcType=VARCHAR},
#{cisReportChild.reportElement.mobileCheckInfo.nameCheckResult,jdbcType=VARCHAR},
#{cisReportChild.reportElement.mobileCheckInfo.areaInfo,jdbcType=VARCHAR},
#{cisReportChild.reportElement.mobileCheckInfo.documentNoCheckResult,jdbcType=VARCHAR},
#{cisReportChild.reportElement.mobileCheckInfo.operator,jdbcType=NUMERIC},
#{cisReportChild.reportElement.mobileCheckInfo.phoneCheckResult,jdbcType=VARCHAR},
#{cisReportChild.reportElement.policeCheckInfo.documentNo,jdbcType=VARCHAR},
#{cisReportChild.reportElement.policeCheckInfo.name,jdbcType=VARCHAR},
#{cisReportChild.reportElement.policeCheckInfo.result,jdbcType=NUMERIC},
#{cisReportChild.reportElement.personAntiSpoofingDescInfo.personAntiSpoofingDesc,jdbcType=VARCHAR},
#{cisReportChild.reportElement.personAntiSpoofingInfo.hitTypes,jdbcType=VARCHAR},
#{cisReportChild.reportElement.personAntiSpoofingInfo.riskLevel,jdbcType=VARCHAR},
#{cisReportChild.reportElement.personAntiSpoofingInfo.riskScore,jdbcType=VARCHAR},
#{cisReportChild.reportElement.personAntiSpoofingInfo.suggest,jdbcType=VARCHAR},
#{cisReportChild.reportElement.mobileStatusInfo.areaInfo,jdbcType=VARCHAR},
#{cisReportChild.reportElement.mobileStatusInfo.cancelTime,jdbcType=VARCHAR},
#{cisReportChild.reportElement.mobileStatusInfo.operator,jdbcType=NUMERIC},
#{cisReportChild.reportElement.mobileStatusInfo.phoneStatus,jdbcType=NUMERIC},
#{cisReportChild.reportElement.mobileStatusInfo.timeLength,jdbcType=VARCHAR},
#{cisReportChild.reportElement.econnoisserurInfo.state,jdbcType=NUMERIC},
#{cisReportChild.reportElement.fraudRiskInfo.state,jdbcType=NUMERIC},
#{cisReportChild.reportElement.personRiskAssess.checkResult,jdbcType=NUMERIC},
#{cisReportChild.reportElement.microNearlyThreeYearsOverdueInfo.overdueAmount,jdbcType=VARCHAR},
#{cisReportChild.reportElement.microNearlyThreeYearsOverdueInfo.overdueCount,jdbcType=NUMERIC},
#{cisReportChild.reportElement.microNearlyThreeYearsOverdueInfo.overdueDays,jdbcType=VARCHAR},
#{cisReportChild.reportElement.historySimpleQueryInfo.totalCount.last1Month,jdbcType=NUMERIC},
#{cisReportChild.reportElement.historySimpleQueryInfo.totalCount.last3Month,jdbcType=NUMERIC},
#{cisReportChild.reportElement.historySimpleQueryInfo.totalCount.last6Month,jdbcType=NUMERIC},
#{cisReportChild.reportElement.historySimpleQueryInfo.totalCount.last12Month,jdbcType=NUMERIC},
#{cisReportChild.reportElement.historySimpleQueryInfo.totalCount.last18Month,jdbcType=NUMERIC},
#{cisReportChild.reportElement.historySimpleQueryInfo.totalCount.last24Month,jdbcType=NUMERIC},
#{cisReportChild.reportElement.phone,jdbcType=VARCHAR},
#{xmlId,jdbcType=VARCHAR}
)
复制代码
(3)保存批量数据(不自动生成主键)
INSERT INTO
TABLE_HISTORY_SIMPLE_QUERY
(BATNO,UNIT_MEMBER,LAST1MONTH,LAST3MONTH,LAST6MONTH,LAST12MONTH,LAST18MONTH,LAST24MONTH)
(
SELECT
#{batNo,jdbcType=VARCHAR},
#{item.unitMember,jdbcType=VARCHAR},
#{item.last1Month,jdbcType=NUMERIC},#{item.last3Month,jdbcType=NUMERIC},
#{item.last6Month,jdbcType=NUMERIC},#{item.last12Month,jdbcType=NUMERIC},
#{item.last18Month,jdbcType=NUMERIC},#{item.last24Month,jdbcType=NUMERIC}
FROM DUAL
)
复制代码
<2>单笔,批量需要插入主键
单笔含主键
DataServiceDetailResult dataServiceDetailResult = tdService.getDataServiceDetailResult(tdReportRoot);
int n = tdDataServiceMapper.insertDataServiceDetailResult(dataServiceDetailResult);
select SEQ_ICS_TD_DATASERVICE.nextval as id from dual
insert into ICS_TD_DATASERVICE (ID, RECORDID, REASON_DESC, REASON_CODE,
SUCCESS)
values (#{id,jdbcType=DECIMAL}, #{recordId,jdbcType=DECIMAL}, #{reasonDesc,jdbcType=VARCHAR},
#{reasonCode,jdbcType=VARCHAR},
#{success,jdbcType=DECIMAL})
复制代码
批量保存包含主键
if (!ListUtil.isEmpty(interfacesList)) {
map.put("recordId", id);
map.put("list", interfacesList);
tdDataServiceMapper.addBatchInterfaces(map);
}
复制代码
sql:
insert into ICS_TD_INTERFACES (ID,RECORDID, TYPE)
SELECT SEQ_ICS_TD_INTERFACES.nextval ID, A.*
FROM(
SELECT
#{recordId,jdbcType=DECIMAL},
#{item.type,jdbcType=VARCHAR}
FROM DUAL
) A
复制代码
总结
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!
擅长针对企业软件开发的产品设计及开发的细节与流程设计课程内容。座右铭:大道至简!
已有23人表明态度,87%喜欢该老师!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号