Mybatis之插入List<T>

对于List<T>  我们先来一个简单的说明

List<T> :表示的是List集合中的元素都为T类型,具体类型在运行期决定;List<T>可以进行诸如add、remove等操作,因为它的类型是固定的T类型,在编码期 不需要进行任何的转型操作。

想更多了解:转载:Java中List<T>和List<?>的区别

在这里 这个T是一个对象:

public class ContractErrorLog implements Serializable {
    private java.lang.Long id;//   
    private java.lang.Long borrowId;//   
    private java.lang.Long debtId;//   
    private java.lang.String docTitle;//
    private java.lang.String customerId;//   
    private java.lang.Object json;//   
    private java.lang.String status;//   
    private java.lang.String isDel;//   
    private java.lang.String createTime;//  
        //省略get和set  
}    

Controller层

//注入
@Autowired 
private IContractErrorLogService contractErrorLogService;
//插入日志集合
@RequestMapping("xxx_copy")
@ResponseBody
public JSONObject autoSign(HttpServletRequest request,HttpServletResponse response){
   List<ContractErrorLog> errorLogs = new ArrayList<ContractErrorLog>();
   ContractErrorLog errorLog1 = new ContractErrorLog();
   .....
   ContractErrorLog errorLog2 = new ContractErrorLog();
   .....
   ContractErrorLog errorLog3 = new ContractErrorLog();
   .....
   ContractErrorLog errorLog4 = new ContractErrorLog();
   .....
   ContractErrorLog errorLog5 = new ContractErrorLog();
   .....//set值的部分省略    
   contractErrorLogService.insertList(errorLogs);
}

接口IContractErrorLogService 

public interface IContractErrorLogService extends IBaseService<ContractErrorLog>{
    public Integer insertList(List<ContractErrorLog> list);
}

IContractErrorLogService的实现类

package com.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;import com.mapper.ContractErrorLogMapper;
import com.service.IContractErrorLogService;

@Service(interfaceClass=IContractErrorLogService.class, timeout=30000, retries = 1)
public class ContractErrorLogServiceImpl extends BaseServiceImpl<ContractErrorLog> implements IContractErrorLogService{
    
    @Autowired
    private ContractErrorLogMapper mapper;

    @Override
    public Integer insertList(List<ContractErrorLog> arg0) {
        // TODO Auto-generated method stub
        return mapper.insertList(arg0);
    }
}

MyBatis 接口对应的方法

@MyBatisRepository
public interface ContractErrorLogMapper extends BaseMapper<ContractErrorLog> {
    public Integer insertList(List<ContractErrorLog> list);
}

ContractErrorLogMapper.xml

<!--批量 插入记录 -->
<insert id="insertList" parameterType="java.util.ArrayList">
  insert into T_CONTRACT_ERROR_LOG(id,borrow_id,debt_id,doc_title,customer_id,json,status,is_del,create_time) values
  <foreach collection="list" item="item" index="index" separator=",">
      (#{item.id},#{item.borrowId},#{item.debtId},#{item.docTitle},#{item.customerId},#{item.json},#{item.status},#{item.isDel},now())
  </foreach>
</insert>
<resultMap id="BaseResultMap" type="com.domain.ContractErrorLog" >
    <result column="id" property="id"/>
    <result column="borrow_id" property="borrowId"/>
    <result column="debt_id" property="debtId"/>
    <result column="doc_title" property="docTitle"/>
    <result column="customer_id" property="customerId"/>
    <result column="json" property="json"/>
    <result column="status" property="status"/>
    <result column="is_del" property="isDel"/>
    <result column="create_time" property="createTime"/>
</resultMap>

 想要知道更多的黑科技吗,那赶紧来扫描吧

原文地址:https://www.cnblogs.com/gjq1126-web/p/11381226.html