Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize batch Import of Excel data based on Java SSM

Shulou Source: shulou.com Published: 2022-06-02 11:17:22 10月01日 Update

Today, I will talk to you about how to achieve batch import of Excel data based on Java SSM, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Import Maven dependent com.alibaba easyexcel ${easyexcel.version} Mapper and mapping file

UserMapper.java

Mapperpublic interface UserMapper {int batchInsert (@ Param ("list") List list);}

UserMapper.xml

Id, nickname, avatar, account, `password`, gender, tel, email, qq, wechat, salt, info, `status`, create_time, update_time insert into tb_user (nickname, avatar, account, `password`, gender, tel, email, qq, wechat, salt, info, `status`, create_time, update_time) values (# {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})

Excel listener

@ Log4j2@Servicepublic class UserExcelListener extends AnalysisEventListener {@ Resource private UserMapper userMapper; / * batch threshold * / private static final int BATCH_COUNT = 250; @ Getter List list = new ArrayList (BATCH_COUNT) @ Override public void invoke (User user, AnalysisContext analysisContext) {/ / read data line by line log.info ("* parse to a piece of data: {}", JSON.toJSONString (user)); list.add (user) If (list.size () > = BATCH_COUNT) {System.out.println ("parsed" + list.size () + "data"); / / every 250th, deposit int batchInsertRes = userMapper.batchInsert (list) in the database once; System.out.println (batchInsertRes) List.clear ();}} @ Override public void doAfterAllAnalysed (AnalysisContext analysisContext) {log.info ("* all data parsing is complete!") ;}} Test @ 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 () }} after reading the above, do you have any further understanding of how to implement batch import of Excel data based on Java SSM? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Data content databases files more listeners knowledge articles industry information information channels thresholds channels one support related testing monitoring Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi Shulou Information Shulou Tech Info OPPO Reno