mybatis 批量将list数据插入到数据库

随着业务需要,有时我们需要将数据批量添加到数据库,mybatis提供了将list集合循环添加到数据库的方法。具体实现代码如下:

1、mapper层中创建 insertForeach(List < Fund > list) 方法,返回值是批量添加的数据条数
	package com.center.manager.mapper;
	import java.util.List;
	import org.apache.ibatis.annotations.Mapper;
	import com.center.manager.entity.Fund;
	@Mapper
	public interface FundMapper {
	
		int insertForeach(List<Fund> list);
	}

Fund类代码如下:

	package com.center.manager.entity;
	import java.util.Date;

	public class Fund {

	    private  String id;
	
		private String fundName;      
	
		private String fundCode;      
	
		private String dateX;         
	
		private String dataY;         

	    private String remarks; 
	
		private String createBy; 
	
		private Date createDate; 
	
		private String updateBy; 
	
		private Date updateDate; 
	
		private String delFlag; 

	    public String getId() {
			return id;
		}

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

		public void setFundName(String fundName) {
			this.fundName = fundName;
		}

		public String getFundCode() {
			return fundCode;
		}

		public void setFundCode(String fundCode) {
			this.fundCode = fundCode;
		}

		public String getDateX() {
			return dateX;
		}

		public void setDateX(String dateX) {
			this.dateX = dateX;
		}

		public String getDataY() {
			return dataY;
		}

		public void setDataY(String dataY) {
			this.dataY = dataY;
		}

	    public String getRemarks() {
			return remarks;
		}

		public void setRemarks(String remarks) {
			this.remarks = remarks;
		}

		public String getCreateBy() {
			return createBy;
		}

		public void setCreateBy(String createBy) {
			this.createBy = createBy;
		}

		public Date getCreateDate() {
			return createDate;
		}

		public void setCreateDate(Date createDate) {
			this.createDate = createDate;
		}

		public String getUpdateBy() {
			return updateBy;
		}

		public void setUpdateBy(String updateBy) {
			this.updateBy = updateBy;
		}

		public Date getUpdateDate() {
			return updateDate;
		}

		public void setUpdateDate(Date updateDate) {
			this.updateDate = updateDate;
		}

		public String getDelFlag() {
			return delFlag;
		}

		public void setDelFlag(String delFlag) {
			this.delFlag = delFlag;
		}

	}

2、mybatis的xml文件中的insert语句如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.center.manager.mapper.FundMapper">

	<insert id="insertForeach" parameterType="java.util.List" useGeneratedKeys="false">
    			insert into fund
    			( id,fund_name,fund_code,date_x,data_y,create_by,create_date,update_by,update_date,remarks,del_flag)
    			values
    			<foreach collection="list" item="item" index="index" separator=",">
    				(
    					#{item.id},
    					#{item.fundName},
    					#{item.fundCode},
    					#{item.dateX},
    					#{item.dataY},
    					#{item.createBy},
    					#{item.createDate},
    					#{item.updateBy},
    					#{item.updateDate},
    					#{item.remarks},
    					#{item.delFlag}
    				)
    		     </foreach>		
    </insert>    
</mapper>
  • 31
    点赞
  • 213
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 29
    评论
您可以使用以下步骤来使用 MyBatis 代码批量导入 Excel 数据数据库: 1. 创建一个 POJO 类来表示 Excel 表格的每一行数据。 2. 使用 Apache POI 库来读取 Excel 文件并将数据存储到 POJO 对象。 3. 创建一个 MyBatis Mapper 接口和对应的 XML 文件,用于将 POJO 对象插入数据库。 4. 在 Mapper 接口定义一个批量插入的方法,使用 MyBatis 提供的批量操作 API 批量插入数据。 5. 在代码调用批量插入方法,将读取到的 Excel 数据批量插入数据库。 以下是一个示例代码: // 定义 POJO 类 public class ExcelData { private String name; private int age; // 省略 getter 和 setter 方法 } // 使用 Apache POI 读取 Excel 文件 List<ExcelData> dataList = new ArrayList<>(); Workbook workbook = WorkbookFactory.create(new File("data.xlsx")); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { ExcelData data = new ExcelData(); data.setName(row.getCell(0).getStringCellValue()); data.setAge((int) row.getCell(1).getNumericCellValue()); dataList.add(data); } // 定义 MyBatis Mapper 接口和 XML 文件 public interface ExcelDataMapper { void insertBatch(List<ExcelData> dataList); } <!-- ExcelDataMapper.xml --> <mapper namespace="com.example.mapper.ExcelDataMapper"> <insert id="insertBatch" parameterType="java.util.List"> INSERT INTO excel_data (name, age) VALUES <foreach collection="list" item="item" separator=","> (#{item.name}, #{item.age}) </foreach> </insert> </mapper> // 调用批量插入方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); try (SqlSession session = sessionFactory.openSession(ExecutorType.BATCH)) { ExcelDataMapper mapper = session.getMapper(ExcelDataMapper.class); mapper.insertBatch(dataList); session.commit(); }
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟世君子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值