Mybatis批量插入注解形式(Mysql和Oracle)

数据库:PostgreSQL,MYSQL

@Mapper
public interface CheckQualificationInfoMapper {
    // 方法一
    @Insert({"<script>" +
            "insert into check_qualification_info " +
            "(id_mp_check_qualification_info," +
            "name," +
            "id_no,"+
            "mobile,"+
            "apply_no,"+
            "cust_no,"+
            "batch_id," +
            "reason)"+
            "values " +
            "<foreach collection='poList' item='item' index='index' separator =','> " +
            "(#{item.id}," +
            " <choose>" +
            " <when test='item.name != null'>" +
            "  #{item.name}," +
            " </when>" +
            " <otherwise> null, </otherwise>" +
            " </choose>"+
            " <choose>" +
            " <when test='item.idNo != null'>" +
            "  #{item.idNo}," +
            " </when>" +
            " <otherwise> null, </otherwise>" +
            " </choose>"+
            " <choose>" +
            " <when test='item.phone != null'>" +
            "  #{item.phone}," +
            " </when>" +
            " <otherwise> null, </otherwise>" +
            " </choose>"+
            " <choose>" +
            " <when test='item.applyNo != null'>" +
            "  #{item.applyNo}," +
            " </when>" +
            " <otherwise> null, </otherwise>" +
            " </choose>"+
            " <choose>" +
            " <when test='item.custNo != null'>" +
            "  #{item.custNo}," +
            " </when>" +
            " <otherwise> null, </otherwise>" +
            " </choose>"+
            " <choose>" +
            " <when test='item.batchId != null'>" +
            "  #{item.batchId}," +
            " </when>" +
            " <otherwise> null, </otherwise>" +
            " </choose>"+
            "#{item.reason})"+
            "</foreach>" +
            "</script>"})
    void batchSaveInfo(@Param("poList") List<CheckQualificationInfoPO> poList);

    //方法二:
    @InsertProvider(type = BatchSaveCheckDTO.class,method = "insertAll" )
    public void insertAll(@Param("poList") List<CheckQualificationInfoPO> poList);

    public  class BatchSaveCheckDTO {
        public String insertAll(@Param("poList") List<CheckQualificationInfoPO> poList) {          
            StringBuilder sb = new StringBuilder();
            sb.append("INSERT INTO check_qualification_info ");
            sb.append("( name, id_no, mobile,apply_no ,cust_no ,batch_id, reason) ");
            sb.append("VALUES ");
            MessageFormat mf = new MessageFormat("(#'{'poList[{0}].name},#'{'poList[{0}].idNo},#'{'poList[{0}].phone},#'{'poList[{0}].applyNo}" +
                    ",#'{'poList[{0}].custNo},#'{'poList[{0}].batchId},#'{'poList[{0}].reason})");
            for (int i = 0; i < poList.size(); i++) {
                sb.append(mf.format(new Object[]{i}));
                if (i < poList.size() - 1) {
                    sb.append(",");
                }
            }
            return sb.toString();
        }
    }

    //方法三:
@Insert("<script> INSERT INTO check_qualification_info "
        + "(name,id_no,mobile,apply_no,cust_no,batch_id,reason) "
        + "VALUES "
        + "<foreach collection = 'infoList' item='record' separator=',' > "
        + " (#{record.name}, "
        + " #{record.idNo},"
        + " #{record.mobile},"
        + " #{record.applyNo},"
        + " #{record.custNo}, "
        + " #{record.batchId}, "     
        + " #{record.reason}) "
        + "</foreach> "
        + "</script>")
boolean bathInsertStatitisticsInfo(@Param("infoList")  List<CheckQualificationInfoPO> infoList);

}

public class CheckQualificationInfoPO {
    private String id;
    
    private String name;

    private String phone;

    private String idNo;

    private String applyNo;

    private String reason;

    private String custNo;
    
    private String batchId;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    public String getApplyNo() {
        return applyNo;
    }

    public void setApplyNo(String applyNo) {
        this.applyNo = applyNo;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getCustNo() {
        return custNo;
    }

    public void setCustNo(String custNo) {
        this.custNo = custNo;
    }

    public String getBatchId() {
        return batchId;
    }

    public void setBatchId(String batchId) {
        this.batchId = batchId;
    }

    @Override
    public String toString() {
        return "CheckQualificationInfoPO{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", idNo='" + idNo + '\'' +
                ", applyNo='" + applyNo + '\'' +
                ", reason='" + reason + '\'' +
                ", custNo='" + custNo + '\'' +
                ", batchId='" + batchId + '\'' +
                '}';
    }
}

 

数据库:Oracle

import org.apache.ibatis.annotations.*;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;


@Mapper
public interface ContactsMapper {

    /***
    * @Description: 方法一;loanType和 applyNo为每条数据的公共参数
    * @Date: 2019-10-16
    */
    @Insert("<script>" +
            "insert all  " +
            "<foreach collection='map.contactList' item='item' index='index'  separator =''  > " +
            "into csp_appl_tel_list ( LOAN_TYPE ,APPLY_NO, NAME, TEL_NO,  RELATION,DATE_CREATED )\n" +
            " values (#{map.loanType},#{map.applyNo},#{item.name},#{item.telNo},#{item.relation},#{item.dateCreated}) " +
            "</foreach> " +
            "select 1 from dual" +
            "</script>")
    public void insertTelList(@Param("map") Map<String, Object> map);


    /**
    * @Description:  方法二;loanType和 applyNo为每条数据的公共参数;设置到每个Map里面(比较死板,不如方法三)
    * @Date: 2019-10-16
    */
    @InsertProvider(type = InsertContactsAll.class , method = "insertContactsAll")
    int inserAll(@Param("contactList") List<Map<String,Object>> list );

    public  class  InsertContactsAll{
        public String insertContactsAll( @Param("contactList") List<Map<String,Object>> list ){
            StringBuilder sb = new StringBuilder();
            sb.append(" INSERT ALL ");
            String inVal=" INTO csp_appl_tel_list ( LOAN_TYPE , APPLY_NO,NAME, TEL_NO,  RELATION, DATE_CREATED ) VALUES ";
            sb.append(inVal);
            MessageFormat mf = new MessageFormat("("
                    + " #'{'contactList[{0}].loanType,jdbcType=VARCHAR},  #'{'contactList[{0}].applyNo,jdbcType=VARCHAR}, #'{'contactList[{0}].name,jdbcType=VARCHAR}, #'{'contactList[{0}].telNo,jdbcType=VARCHAR}"
                    + ", #'{'contactList[{0}].relation,jdbcType=VARCHAR}, #'{'contactList[{0}].dateCreated,jdbcType=DATE} )" );
            for (int i = 0; i < list.size(); i++) {
                sb.append(mf.format(new Object[] { i+"" }));
                if (i < list.size() - 1) {
                    sb.append(inVal);
                }
            }
            sb.append(" select 1 from dual");
            return sb.toString();
        }
    }

    /**
    * @Description: 方法三;这里考虑的每条数据具有公共参数,公共参数无需封装在每一条数据里面
    * @Param: [map]  注意!!!此处无需再加注解@Param,其一会导致内部类方法的入参出现相同的两份,其二会导致MessageFormat里面参数的取值层级发生变化
    * @Date: 2019-10-16
    */
    @InsertProvider(type = InsertContactsAllData.class , method = "insertContactsAllData")
    int inserAllData( Map map );

    public  class  InsertContactsAllData{
        public String insertContactsAllData(  Map map  ){
            StringBuilder sb = new StringBuilder();
            List<Map<String,Object>> list = (List<Map<String, Object>>) map.get("contactList");
            sb.append(" INSERT ALL ");
            String inVal=" INTO csp_appl_tel_list ( LOAN_TYPE ,APPLY_NO, NAME , TEL_NO , RELATION , DATE_CREATED) VALUES ";
            sb.append(inVal);
            MessageFormat mf = new MessageFormat("("
                    + " #'{'loanType,jdbcType=VARCHAR},  #'{'applyNo,jdbcType=VARCHAR}, #'{'contactList[{0}].name,jdbcType=VARCHAR}, #'{'contactList[{0}].telNo,jdbcType=VARCHAR},"
                    + "#'{'contactList[{0}].relation,jdbcType=VARCHAR},#'{'contactList[{0}].dateCreated,jdbcType=DATE} )" );
            for (int i = 0; i < list.size(); i++) {
                sb.append(mf.format(new Object[] { i+"" }));
                if (i < list.size() - 1) {
                    sb.append(inVal);
                }
            }
            sb.append(" select 1 from dual");
            return sb.toString();
        }
    }
}


如下粗略的测试:
@Component
@RefreshScope
public class MyTestJob {
    private static Logger logger = LoggerFactory.getLogger(MyTestJob.class);

    @Autowired
    ContactsMapper contactsMapper;

    @Scheduled(cron = "0 */1 * * * ?")
    public void push() {
        try {
            // 方法一
            Map telListMap = new HashMap();
            List<Map<String,Object>> contactList = new ArrayList<Map<String,Object>>();
            int i = 1000;
            for (int n =0; n<i;n++){
                Map contactMap = new HashMap();
                contactMap.put("name","name");
                contactMap.put("telNo","telNo");
                contactMap.put("relation","relation");
                contactMap.put("dateCreated",new Date());
                contactList.add(contactMap);

            }
            telListMap.put("contactList",contactList);
            telListMap.put("applyNo","方法1");
            telListMap.put("loanType","loanType1");
            contactsMapper.insertTelList(telListMap);

            // 方法二
            List<Map<String,Object>> contactList2 = new ArrayList<Map<String,Object>>();
            for (int n =0; n<i;n++){
                Map contactMap = new HashMap();
                contactMap.put("name","name");
                contactMap.put("telNo","telNo");
                contactMap.put("relation","relation");
                contactMap.put("dateCreated",new Date());
                contactMap.put("applyNo","方法2");
                contactMap.put("loanType","loanType2");
                contactList2.add(contactMap);
            }
            contactsMapper.inserAll(contactList2);

            // 方法三
            Map telListMap3 = new HashMap();
            List<Map<String,Object>> contactList3 = new ArrayList<Map<String,Object>>();
            for (int n =0; n<i;n++){
                Map contactMap = new HashMap();
                contactMap.put("name","name");
                contactMap.put("telNo","telNo");
                contactMap.put("relation","relation");
                contactMap.put("dateCreated",new Date());
                contactList3.add(contactMap);
            }
            telListMap3.put("contactList",contactList3);
            telListMap3.put("applyNo","方法3");
            telListMap3.put("loanType","loanType3");
            contactsMapper.inserAllData(telListMap3);

          
        } catch (Exception e) {
            logger.error(" MyTestJob ERROR :", e);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis Plus是基于MyBatis的一个增强工具,通过提供更简洁、更方便的API,简化了对数据库的操作。相比于原生的MyBatisMyBatis Plus提供了更丰富的功能和更高效的性能。而OracleMySQL都是常见的关系型数据库,它们在底层实现原理和语法特性上有一些不同。 对于双数据源的配置,可以通过MyBatis Plus的多数据源配置来实现。首先,需要在配置文件中定义两个不同的数据源,分别对应OracleMySQL。可以使用不同的dataSource和transactionManager配置来指定每个数据源的连接和事务管理。 然后,在代码中使用@DS注解来指定具体是使用哪个数据源。@DS注解可以标记在类级别和方法级别,用来指定使用的数据源。例如,@DS("oracle")标记在类级别上,表示该类中的所有方法都使用Oracle数据源,而@DS("mysql")标记在方法级别上,表示该方法使用MySQL数据源。 通过这样的配置,就可以实现在同一个应用中同时使用OracleMySQL双数据源。在使用的过程中,可以根据业务需求灵活选择具体使用的数据库。同时,MyBatis Plus还提供了很多便捷的方法和功能,可以更方便地进行数据库操作。 总之,通过MyBatis Plus的多数据源配置,可以很方便地实现OracleMySQL双数据源的使用。这样的配置对于一些需要同时操作多个数据库的应用来说非常有用,可以满足不同业务需求的多样性。使用MyBatis Plus可以提高开发效率和代码质量,使数据库操作变得更简单和高效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值