当前位置: 移动技术网 > IT编程>开发语言>Java > mybatis单笔批量保存实体数据的方法

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

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

一,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单笔批量保存实体数据的方法,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网