mybatis单笔批量保存实体数据的方法

 更新时间:2018年01月19日 09:06:12   作者:T-M  
这篇文章主要介绍了mybatis单笔批量保存实体数据的相关知识,非常不错,具有参考借鉴价值,需要的朋友可以参考下

这一篇主要介绍,如何保存实体数据。具体内容如下所示;

一,xml样例

<?xml version="1.0" encoding="GBK"?>
<!-- 1..1 -->
<cisReports batNo="查询批次号" unitName="查询单位名称" subOrgan="分支机构名称" queryUserID="查询操作员登录名" queryCount="查询请求数量" receiveTime="查询申请时间,格式YYYYMMDD HH24:mm:ss">
  <!-- 以下为每个查询申请的查询结果 1..n -->
  <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查询条件信息 1..1 -->
    <queryConditions>
      <!-- 1..n -->
      <item>
        <name>查询条件英文名称</name>
        <caption>查询条件中文名称</caption>
        <value>查询条件值</value>
      </item>
    </queryConditions>
    <!--2身份认证 1..1 -->
    <policeCheckInfo subReportType="10602" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode ="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
        <item>          
          <name>被查询者姓名</name>
          <documentNo>被查询者证件号码</documentNo>
          <!--1,一致 2,不一致 treatResult为3时为空-->
          <result>身份认证结果,1,一致 2,不一致</result>
        </item>
    </policeCheckInfo>
    <!-- 3个人手机号码核查 1..1 -->
    <mobileCheckInfo subReportType="13612" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
        <!--treatresult为2、3时没有以下节点-->
        <item>
          <nameCheckResult>姓名核查结果,有:一致、基本一致、不一致、无法核查 </nameCheckResult>
          <documentNoCheckResult>证件号码核查结果,有:一致、不一致、无法核查 </documentNoCheckResult>
          <phoneCheckResult>手机号码核查结果:一致、无法核查 </phoneCheckResult>          
          <areaInfo>号码归属地</areaInfo>
          <operator>运营商 1:中国电信 2:中国移动 3:中国联通 </operator>
        </item>
    </mobileCheckInfo>
    <!--4个人反欺诈风险综述信息 1..1 -->
    <personAntiSpoofingDescInfo subReportType="14225" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatsult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
      <!--treatresult为2时没有以下节点-->
      <!--
      个人反欺诈综述信息。如以下例子,可能为空,可能存在其中0条或n条,根据具体结果返回。
      1、 反欺诈风险评分为100分,风险等级为高,建议拒绝。
      2、 电信手机号码131****1542使用不足1个月。
      3、 命中羊毛党名单。
      4、 命中欺诈风险名单。
      5、 命中高风险人员名单。
      6、 存在7笔逾期的信贷记录。
      7、 存在40条个人风险概要信息。
      8、 在近两年被机构查询过23次个人信息。      
      -->      
      <personAntiSpoofingDesc>个人反欺诈综述信息</personAntiSpoofingDesc>
    </personAntiSpoofingDescInfo>
    <!--5个人反欺诈风险评分信息 1..1 -->
    <personAntiSpoofingInfo subReportType="14224" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
      <!--treatresult为2、3时没有以下节点-->
      <riskScore>风险评分</riskScore>
      <riskLevel>风险等级,取值:高,中度,低</riskLevel>
      <suggest>风险建议,取值:建议拒绝,建议复核,建议通过</suggest>
      <hitTypes>命中类型:高风险名单、个人风险信息、信贷逾期名单、被机构查询信息、手机号码状态异常、手机号码低网龄、羊毛党名单、欺诈风险名单等等,可能为1个或多个</hitTypes>
    </personAntiSpoofingInfo>
    <!-- 6手机号码状态信息 1..1 -->
    <mobileStatusInfo subReportType="13611" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
        <!--treatresult为2、3时没有以下节点-->
        <item>
          <operator>运营商 1:中国电信 2:中国移动 3:中国联通 </operator>
          <areaInfo>手机号码归属地 </areaInfo>
          <phoneStatus>手机状态 1:正常在用 2: 停机 3:未启用 4:已销号 5-其他 6:预销号</phoneStatus>
          <timeLength>手机号码在网时长,如:在网时长不足1个月,1-2个月,3-6个月,7-12个月 </timeLength>
          <cancelTime>号码注销时间</cancelTime>
        </item>
    </mobileStatusInfo>
    <!--7是否命中羊毛党名单 1..1-->
    <econnoisserurInfo subReportType="14228" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
      <!--treatresult为2、3时没有以下节点-->
      <state>是否命中羊毛党名单,取值:0/1,0表示未命中羊毛党名单,1表示命中羊毛党名单</state>
    </econnoisserurInfo>
    <!--8是否命中欺诈风险名单 1..1-->
    <fraudRiskInfo subReportType="14229" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
      <!--treatresult为2、3时没有以下节点-->
      <state>是否命中欺诈风险名单,取值:0/1,0表示未命中欺诈风险名单,1表示命中欺诈风险名单</state>
    </fraudRiskInfo>
    <!--9是否命中高风险名单 1..1 -->
    <personRiskAssess subReportType="14226" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
        <!--treatresult为2、3时没有以下节点-->
        <checkResult>是否命中高风险名单 1:是,2:否</checkResult>
    </personRiskAssess>
    <!-- 10网贷逾期信息 1..1 -->
    <microNearlyThreeYearsOverdueInfo subReportType="13204" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
       <!--treatresult为2、3时没有以下节点-->
       <!--逾期汇总信息 0..1-->
        <stat>
          <overdueAmount>逾期本金,单位:以贷款基本信息的币种为准</overdueAmount>
          <overdueDays>逾期天数区间,取值区间:逾期1天(含)以上</overdueDays>
          <overdueCount>逾期天数区间的总笔数</overdueCount>
        </stat>
        <!--0..1-->
        <items>
          <!--逾期概要信息 1..5 多个记录-->
          <item>
            <overdueAmount>逾期本金,单位:以贷款基本信息的币种为准</overdueAmount>
            <overdueDays>逾期天数区间,取值区间:逾期1-6天,逾期7-29天,逾期30-59天,逾期60-89天,逾期90天(含)以上</overdueDays>
            <overdueCount>逾期天数区间的笔数</overdueCount>
          </item>
        </items>
    </microNearlyThreeYearsOverdueInfo>
    <!-- 11风险信息 1..1 -->
    <personRiskInfo subReportType="14227" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
      <!--treatresult为2、3时没有以下节点-->
      <!-- 汇总信息 0..1-->
      <stat>
        <totleCount>概要汇总条数</totleCount>
        <alCount>司法案例信息条数</alCount>
        <zxCount>司法执行信息条数</zxCount>
        <sxCount>司法失信信息条数</sxCount>
        <swCount>税务行政执法信息条数</swCount>
        <cqggCount>催欠公告信息条数</cqggCount>
        <wdyqCount>网贷逾期信息条数</wdyqCount>
      </stat>
      <!-- 汇总信息 end-->
      <!-- 概要信息 0..1-->
      <summary>
        <!-- 司法案例信息概要信息 0..1 -->
        <als>
          <!-- 司法案例信息概要信息内容 1..n -->
          <item>
            <recordId>记录编号</recordId>
            <bt>标题</bt>
            <ajlx>案件类型</ajlx>
            <sjnf>审结年份,格式:YYYY</sjnf>
            <dsrlx>当事人类型</dsrlx>
          </item>
        </als>
        <!-- 司法执行信息概要信息 0..1 -->
        <zxs>
          <!-- 司法执行信息概要信息内容 1..n -->
          <item>
            <recordId>记录编号</recordId>
            <bt>标题</bt>
            <zxbd>执行标的,单位:以币种为准</zxbd>
            <larq>立案日期,格式YYYY-MM-DD</larq>
          </item>
        </zxs>
        <!-- 司法失信信息概要信息 0..1 -->
        <sxs>
          <!-- 司法失信信息概要信息内容 1..n -->
          <item>
            <recordId>记录编号</recordId>
            <bt>标题</bt>
            <larq>立案日期,格式YYYY-MM-DD</larq>
            <fbrq>发布日期,格式YYYY-MM-DD</fbrq>
          </item>
        </sxs>
        <!-- 税务行政执法信息概要信息 0..1 -->
        <sws>
          <!-- 税务行政执法信息概要信息内容 1..n -->
          <item>
            <recordId>记录编号</recordId>
            <bt>标题</bt>
            <ggrq>公告日期,格式YYYY-MM-DD</ggrq>
          </item>
        </sws>
        <!-- >催欠公告信息概要信息 0..1 -->
        <cqs>
          <!-- 催欠公告信息概要信息内容 1..n -->
          <item>
            <recordId>记录编号</recordId>
            <bt>标题</bt>
            <fbrq>发布日期,格式YYYY-MM-DD</fbrq>
          </item>
        </cqs>
        <!-- 网贷逾期信息概要信息 0..1 -->
        <wdyqs>
          <!-- 网贷逾期信息概要信息内容 1..n -->
          <item>
            <recordId>记录编号</recordId>
            <bt>标题</bt>
            <fbrq>发布日期,格式YYYY-MM-DD</fbrq>
          </item>
        </wdyqs>
      </summary>
    </personRiskInfo>    
    <!--12个人近两年历史查询记录 1..1 -->
    <historySimpleQueryInfo subReportType="19902" subReportTypeCost="96040" treatResult="子报告查询状态,1:查得,2:未查得,3:其他原因未查得" treatErrorCode="treatResult=3时的错误代码,详见数据字典,treatResult!=3时,该属性不存在" errorMessage="treatResult=3时的错误描述信息,treatResult!=3时,该属性的值为空">
      <!--treatresult为2、3时没有以下节点-->
      <!--0..1-->
      <items>
        <!-- 1..n -->
        <item>
          <unitMember>单位类型名称</unitMember>
          <last1Month>近1个月查询记录数</last1Month>
          <last3Month>近3个月查询记录数</last3Month>
          <last6Month>近6个月查询记录数</last6Month>
          <last12Month>近12个月查询记录数</last12Month>
          <last18Month>近18月查询记录数</last18Month>
          <last24Month>近24月查询记录数</last24Month>
        </item>
      </items>
      <!--0..1-->
      <count>
        <last1Month>近1个月各单位类型查询记录总数</last1Month>
        <last3Month>近3个月各单位类型查询记录总数</last3Month>
        <last6Month>近6个月各单位类型查询记录总数</last6Month>
        <last12Month>近12个月各单位类型查询记录总数</last12Month>
        <last18Month>近18月各单位类型查询记录总数</last18Month>
        <last24Month>近24月各单位类型查询记录总数</last24Month>      
      </count>
    </historySimpleQueryInfo>
  </cisReport>
</cisReports>

二,表结构设计

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<String, Object> map = new HashMap<>();
    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 id="savePersonRiskInfo" parameterType="java.util.Map">
    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>
  <!--逾期详细记录 -->
   //批量
  <insert id="saveMicroNearlyThreeYearsOverdueInfoList" parameterType="java.util.Map">
    INSERT INTO TABLE_MNTYOI (BATNO,OVERDUEAMOUNT,OVERDUEDAYS,OVERDUECOUNT)
    <foreach collection="list" item="item" separator="union all">
      (
      SELECT #{batNo,jdbcType=VARCHAR},
      #{item.overdueAmount,jdbcType=VARCHAR},
      #{item.overdueDays,jdbcType=VARCHAR},
      #{item.overdueCount,jdbcType=NUMERIC}
      FROM DUAL
      )
    </foreach>
  </insert>

 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<String, Object> map = new HashMap<>();
          //(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 id="saveCisReport" parameterType="java.util.Map">
    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 id="saveHistorySimpleQueryInfoList" parameterType="java.util.Map">
    INSERT INTO
    TABLE_HISTORY_SIMPLE_QUERY
    (BATNO,UNIT_MEMBER,LAST1MONTH,LAST3MONTH,LAST6MONTH,LAST12MONTH,LAST18MONTH,LAST24MONTH)
    <foreach collection="list" item="item" separator="union all">
      (
      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
      )
    </foreach>
  </insert>

<2>单笔,批量需要插入主键

单笔含主键

  DataServiceDetailResult dataServiceDetailResult = tdService.getDataServiceDetailResult(tdReportRoot);
    int n = tdDataServiceMapper.insertDataServiceDetailResult(dataServiceDetailResult);

 <!--信息核验接口-->
  <!--DATASERVICE-->
  <insert id="insertDataServiceDetailResult" parameterType="DataServiceDetailResult">
    <selectKey keyProperty="id" resultType="BigDecimal" order="BEFORE">
      select SEQ_ICS_TD_DATASERVICE.nextval as id from dual
    </selectKey>
    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})
  </insert>

批量保存包含主键

 if (!ListUtil.isEmpty(interfacesList)) {
      map.put("recordId", id);
      map.put("list", interfacesList);
      tdDataServiceMapper.addBatchInterfaces(map);
    }

sql: 

<!--批量保存interfaces-->
  <insert id="addBatchInterfaces" parameterType="java.util.List">
    <!--
    注释为mysql插入方式,可以插入但是导致数据回滚不了
    下面为oracle插入方式:注意parameterType:java.util.List或java.util.Map都可以
    BEGIN
    <foreach collection="list" item="item" index="index" separator="">
      INSERT INTO ICS_TD_INTERFACES(ID, RECORDID, TYPE)
      VALUES
      (
      BLOANCRDTDATA.SEQ_ICS_TD_INTERFACES.nextval,
      #{recordId,jdbcType=DECIMAL},
      #{item.type,jdbcType=VARCHAR}
      );
    </foreach>
    COMMIT;
    END;-->
    insert into ICS_TD_INTERFACES (ID,RECORDID, TYPE)
    SELECT SEQ_ICS_TD_INTERFACES.nextval ID, A.*
    FROM(
    <foreach collection="list" item="item" index="index" separator="union all">
      SELECT
      #{recordId,jdbcType=DECIMAL},
      #{item.type,jdbcType=VARCHAR}
      FROM DUAL
    </foreach>
    ) A
  </insert>

总结

以上所述是小编给大家介绍的mybatis单笔批量保存实体数据的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Java设计模式之java中介者模式详解

    Java设计模式之java中介者模式详解

    这篇文章主要为大家详细介绍了23种设计模式之java中介者模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-09-09
  • SpringCache缓存自定义配置的实现

    SpringCache缓存自定义配置的实现

    本文主要介绍了SpringCache缓存自定义配置的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • springBoot使用JdbcTemplate代码实例

    springBoot使用JdbcTemplate代码实例

    这篇文章主要介绍了springBoot使用JdbcTemplate代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • Java中Jackson的序列化与反序列化详解

    Java中Jackson的序列化与反序列化详解

    这篇文章主要介绍了Java中Jackson的序列化与反序列化详解,Jackson被认为是"Java JSON库"或"Java最好的JSON解析器",Jackson 还是一套用于 Java(和 JVM 平台)的数据处理工具,需要的朋友可以参考下
    2024-01-01
  • Java实现中序表达式的实例代码

    Java实现中序表达式的实例代码

    这篇文章主要介绍了Java实现中序表达式的实例代码,需要的朋友可以参考下
    2018-08-08
  • nacos中的配置使用@Value注解获取不到值的原因及解决方案

    nacos中的配置使用@Value注解获取不到值的原因及解决方案

    这篇文章主要介绍了nacos中的配置使用@Value注解获取不到值的原因分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • 从0构建Oauth2Server服务之Refreshing-access-tokens

    从0构建Oauth2Server服务之Refreshing-access-tokens

    这篇文章主要为大家介绍了从0构建Oauth2Server服务之Refreshing-access-tokens刷新令牌示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • spring @Component注解原理解析

    spring @Component注解原理解析

    这篇文章主要介绍了spring @Component注解原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Eclipse引用XSD实现XML配置文件提示标签的方法

    Eclipse引用XSD实现XML配置文件提示标签的方法

    今天小编就为大家分享一篇关于Eclipse引用XSD实现XML配置文件提示标签的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • mybatis的test坑及解决(不等于‘‘ 且 不等于0)

    mybatis的test坑及解决(不等于‘‘ 且 不等于0)

    这篇文章主要介绍了mybatis的test坑及解决(不等于‘‘ 且 不等于0),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03

最新评论