您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)基于Java SSM如何實現(xiàn)Excel數(shù)據(jù)批量導(dǎo)入,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>${easyexcel.version}</version> </dependency>
UserMapper.java
@Mapper public interface UserMapper { int batchInsert(@Param("list") List<User> list); }
UserMapper.xml
<?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.hc.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.hc.domain.User"> <!--@mbg.generated--> <!--@Table tb_user--> <id column="id" jdbcType="BIGINT" property="id" /> <result column="nickname" jdbcType="VARCHAR" property="nickname" /> <result column="avatar" jdbcType="VARCHAR" property="avatar" /> <result column="account" jdbcType="VARCHAR" property="account" /> <result column="password" jdbcType="VARCHAR" property="password" /> <result column="gender" jdbcType="TINYINT" property="gender" /> <result column="tel" jdbcType="VARCHAR" property="tel" /> <result column="email" jdbcType="VARCHAR" property="email" /> <result column="qq" jdbcType="VARCHAR" property="qq" /> <result column="wechat" jdbcType="VARCHAR" property="wechat" /> <result column="salt" jdbcType="VARCHAR" property="salt" /> <result column="info" jdbcType="VARCHAR" property="info" /> <result column="status" jdbcType="TINYINT" property="status" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" /> </resultMap> <sql id="Base_Column_List"> <!--@mbg.generated--> id, nickname, avatar, account, `password`, gender, tel, email, qq, wechat, salt, info, `status`, create_time, update_time </sql> <insert id="batchInsert" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true"> <!--@mbg.generated--> insert into tb_user (nickname, avatar, account, `password`, gender, tel, email, qq, wechat, salt, info, `status`, create_time, update_time) values <foreach collection="list" item="item" separator=","> (#{item.nickname,jdbcType=VARCHAR}, #{item.avatar,jdbcType=VARCHAR}, #{item.account,jdbcType=VARCHAR}, #{item.password,jdbcType=VARCHAR}, #{item.gender,jdbcType=TINYINT}, #{item.tel,jdbcType=VARCHAR}, #{item.email,jdbcType=VARCHAR}, #{item.qq,jdbcType=VARCHAR}, #{item.wechat,jdbcType=VARCHAR}, #{item.salt,jdbcType=VARCHAR}, #{item.info,jdbcType=VARCHAR}, #{item.status,jdbcType=TINYINT}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.updateTime,jdbcType=TIMESTAMP}) </foreach> </insert> </mapper>
Excel監(jiān)聽器
@Log4j2 @Service public class UserExcelListener extends AnalysisEventListener<User> { @Resource private UserMapper userMapper; /** * 批處理閾值 */ private static final int BATCH_COUNT = 250; @Getter List<User> list = new ArrayList<>(BATCH_COUNT); @Override public void invoke(User user, AnalysisContext analysisContext) { //逐行讀取數(shù)據(jù) log.info("********** 解析到一條數(shù)據(jù):{}", JSON.toJSONString(user)); list.add(user); if (list.size() >= BATCH_COUNT) { System.out.println("已經(jīng)解析"+list.size()+"條數(shù)據(jù)"); //每250條,往數(shù)據(jù)庫中存一次 int batchInsertRes = userMapper.batchInsert(list); System.out.println(batchInsertRes); list.clear(); } } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { log.info("**********所有數(shù)據(jù)解析完成!"); } }
@ExtendWith(SpringExtension.class) @ContextConfiguration("/applicationContext.xml") public class ExcelUtilTest { @Resource private UserExcelListener userExcelListener; @Test void userListener(){ EasyExcel.read("E:\\Projects\\WorksDisplay\\data\\users.xlsx", User.class, userExcelListener) .sheet() .doRead(); } }
看完上述內(nèi)容,你們對基于Java SSM如何實現(xiàn)Excel數(shù)據(jù)批量導(dǎo)入有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。