Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report