MyBatis批量插入List数据实现(MySQL),使用注解动态传入表名和参数~

MyBatis批量插入List数据实现(MySQL)

  • 1. xml文件
  • 2. mapper--java
  • 3. 封装成对象
  • 4. 动态传入表名
    • 4.1 AllAccountMapper.xml
    • 4.2 AllAccountMapper.java
  • 5. 程序运行

1. xml文件




    
        insert into error_open_account
        ( id,branchName,branchCode,orgName,orgId,clientId,clientName,account,accountName,openDate,checkDate,closeDate,expireDate,moneyType,subjectId,businessCode,exchangeMark,accountState,accountProperty,safeAccountNature,addTime,folderTime)
        values
        
            (
            #{item.id},
            #{item.branchName},
            #{item.branchCode},
            #{item.orgName},
            #{item.orgId},
            #{item.clientId},
            #{item.clientName},
            #{item.account},
            #{item.accountName},
            #{item.openDate},
            #{item.checkDate},
            #{item.closeDate},
            #{item.expireDate},
            #{item.moneyType},
            #{item.subjectId},
            #{item.businessCode},
            #{item.exchangeMark},
            #{item.accountState},
            #{item.accountProperty},
            #{item.safeAccountNature},
            #{item.addTime},
            #{item.folderTime}
            )
        
    


2. mapper–java

package mapper;

import java.util.List;

public interface ErrorAccountMapper {
    int InsertList (List list);
}

3. 封装成对象

 List reList = new ArrayList<>();
 ErrorAccount errorAccount=new ErrorAccount();
            DataFormatter formatter=new DataFormatter();
            errorAccount.setBranchName(formatter.formatCellValue(row.getCell(0)));
            errorAccount.setBranchCode(formatter.formatCellValue(row.getCell(1)));
            errorAccount.setOrgName(formatter.formatCellValue(row.getCell(2)));
            errorAccount.setOrgId(formatter.formatCellValue(row.getCell(3)));
            errorAccount.setClientId(formatter.formatCellValue(row.getCell(4)));
            errorAccount.setClientName(formatter.formatCellValue(row.getCell(5)));
            errorAccount.setAccount(formatter.formatCellValue(row.getCell(6)));
            errorAccount.setAccountName(formatter.formatCellValue(row.getCell(7)));
            errorAccount.setOpenDate(formatter.formatCellValue(row.getCell(8)));
            errorAccount.setCheckDate(formatter.formatCellValue(row.getCell(9)));
            errorAccount.setCloseDate(formatter.formatCellValue(row.getCell(10)));
            errorAccount.setExpireDate(formatter.formatCellValue(row.getCell(11)));
            errorAccount.setMoneyType(formatter.formatCellValue(row.getCell(12)));
            errorAccount.setSubjectId(formatter.formatCellValue(row.getCell(13)));
            errorAccount.setBusinessCode(formatter.formatCellValue(row.getCell(14)));
            errorAccount.setExchangeMark(formatter.formatCellValue(row.getCell(15)));
            errorAccount.setAccountState(formatter.formatCellValue(row.getCell(16)));
            errorAccount.setAccountProperty(formatter.formatCellValue(row.getCell(17)));
            errorAccount.setSafeAccountNature(formatter.formatCellValue(row.getCell(18)));
            reList.add(errorAccount);

4. 动态传入表名

使用注解动态传入表名,实际上也可以封装成map的形式实现。

4.1 AllAccountMapper.xml




    
        insert into ${tableName}
        ( id,orgName,orgId,accountName,account,accountProperty,openDate,closeDate,firstPaymentDate,remark,addTime,folderTime)
        values
        
            (
            #{item.id},
            #{item.orgName},
            #{item.orgId},
            #{item.accountName},
            #{item.account},
            #{item.accountProperty},
            #{item.openDate},
            #{item.closeDate},
            #{item.firstPaymentDate},
            #{item.remark},
            #{item.addTime},
            #{item.folderTime}
            )
        
    


4.2 AllAccountMapper.java

package mapper;

import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface AllAccountMapper {
    int InsertList (@Param("tableName") String tableName, @Param("list") List list);
}

5. 程序运行

MyBatis批量插入List数据实现(MySQL),使用注解动态传入表名和参数~_第1张图片

程序完美运行哈哈哈~

日志打印采用了log4j。

  • 具体配置链接
  • 错误处理

你可能感兴趣的:(Java学习)