【106期】面试官问:Java 多线程如何实现批量拆分 List 导入数据库?

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注Java)
img

正文

nation,

secondary_college,

operator,

sex,

is_delete,

account_address,

native_place,

original_place,

used_name,

pictrue,

join_party_date,

political_status,

tel_num,

is_registry,

graduate_school,

create_time,

update_time        

values

(

#{item.id,jdbcType=VARCHAR},

#{item.remark,jdbcType=VARCHAR},

#{item.nemtAspiration,jdbcType=VARCHAR},

#{item.nemtCode,jdbcType=VARCHAR},

#{item.nemtScore,jdbcType=VARCHAR},

#{item.studentId,jdbcType=VARCHAR},

#{item.identityCardId,jdbcType=VARCHAR},

#{item.level,jdbcType=VARCHAR},

#{item.major,jdbcType=VARCHAR},

#{item.name,jdbcType=VARCHAR},

#{item.nation,jdbcType=VARCHAR},

#{item.secondaryCollege,jdbcType=VARCHAR},

#{item.operator,jdbcType=VARCHAR},

#{item.sex,jdbcType=VARCHAR},

0,

#{item.accountAddress,jdbcType=VARCHAR},

#{item.nativePlace,jdbcType=VARCHAR},

#{item.originalPlace,jdbcType=VARCHAR},

#{item.usedName,jdbcType=VARCHAR},

#{item.pictrue,jdbcType=VARCHAR},

#{item.joinPartyDate,jdbcType=VARCHAR},

#{item.politicalStatus,jdbcType=VARCHAR},

#{item.telNum,jdbcType=VARCHAR},

#{item.isRegistry,jdbcType=TINYINT},

#{item.graduateSchool,jdbcType=VARCHAR},

now(),

now()

)

                  

代码说明:

底层的mapper是通过逆向工程来生成的,批量插入如下,是拼接成类似:insert into tb_enroll_student()values (),()…….();

这样的缺点是,数据库一般有一个默认的设置,就是每次sql操作的数据不能超过4M。这样插入,数据多的时候,数据库会报错Packet for query is too large (6071393 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.,虽然我们可以通过

类似 修改 my.ini 加上 max_allowed_packet =6710886467108864=64M,默认大小4194304 也就是4M

修改完成之后要重启mysql服务,如果通过命令行修改就不用重启mysql服务。

完成本次操作,但是我们不能保证项目单次最大的大小是多少,这样是有弊端的。所以可以考虑进行分组导入。

三、分组把list导入Mysql中

同样适用mybatis批量插入,区别是对每次的导入进行分组计算,然后分多次进行导入:

@Transactional(rollbackFor = Exception.class)

public int addFreshStudentsNew2(List list, String schoolNo) {

if (list == null || list.isEmpty()) {

return 0;

}

List studentEntityList = new LinkedList<>();

List enrollStudentEntityList = new LinkedList<>();

List allusersEntityList = new LinkedList<>();

for (FreshStudentAndStudentModel freshStudentAndStudentModel : list) {

EnrollStudentEntity enrollStudentEntity = new EnrollStudentEntity();

StudentEntity studentEntity = new StudentEntity();

BeanUtils.copyProperties(freshStudentAndStudentModel, studentEntity);

BeanUtils.copyProperties(freshStudentAndStudentModel, enrollStudentEntity);

String operator = TenancyContext.UserID.get();

String studentId = BaseUuidUtils.base58Uuid();

enrollStudentEntity.setId(BaseUuidUtils.base58Uuid());

enrollStudentEntity.setStudentId(studentId);

enrollStudentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());

enrollStudentEntity.setOperator(operator);

studentEntity.setId(studentId);

studentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());

studentEntity.setOperator(operator);

studentEntityList.add(studentEntity);

enrollStudentEntityList.add(enrollStudentEntity);

AllusersEntity allusersEntity = new AllusersEntity();

allusersEntity.setId(enrollStudentEntity.getId());

allusersEntity.setUserCode(enrollStudentEntity.getNemtCode());

allusersEntity.setUserName(enrollStudentEntity.getName());

allusersEntity.setSchoolNo(schoolNo);

allusersEntity.setTelNum(enrollStudentEntity.getTelNum());

allusersEntity.setPassword(enrollStudentEntity.getNemtCode());  //密码设置为考生号

allusersEntityList.add(allusersEntity);

}

int c = 100;

int b = enrollStudentEntityList.size() / c;

int d = enrollStudentEntityList.size() % c;

int enResult = 0;

int stuResult = 0;

boolean allResult = false;

for (int e = c; e <= c * b; e = e + c) {

enResult = enrollStudentDao.insertAll(enrollStudentEntityList.subList(e - c, e));

stuResult = studentDao.insertAll(studentEntityList.subList(e - c, e));

allResult = allusersFacade.insertUserList(allusersEntityList.subList(e - c, e));

}

if (d != 0) {

enResult = enrollStudentDao.insertAll(enrollStudentEntityList.subList(c * b, enrollStudentEntityList.size()));

stuResult = studentDao.insertAll(studentEntityList.subList(c * b, studentEntityList.size()));

allResult = allusersFacade.insertUserList(allusersEntityList.subList(c * b, allusersEntityList.size()));

}

if (enResult > 0 && stuResult > 0 && allResult) {

return 10;

}

return -10;

}

代码说明:

这样操作,可以避免上面的错误,但是分多次插入,无形中就增加了操作实践,很容易超时。所以这种方法还是不值得提倡的。

再次改进,使用多线程分批导入。在此推荐一下小程序Java精选面试题,内涵大量初级、中级、高级甚至架构师面试题,支持在线刷题。

四、多线程分批导入Mysql

依然使用mybatis的批量导入,不同的是,根据线程数目进行分组,然后再建立多线程池,进行导入。

@Transactional(rollbackFor = Exception.class)

public int addFreshStudentsNew(List list, String schoolNo) {

if (list == null || list.isEmpty()) {

return 0;

}

List studentEntityList = new LinkedList<>();

List enrollStudentEntityList = new LinkedList<>();

List allusersEntityList = new LinkedList<>();

list.forEach(freshStudentAndStudentModel -> {

EnrollStudentEntity enrollStudentEntity = new EnrollStudentEntity();

StudentEntity studentEntity = new StudentEntity();

BeanUtils.copyProperties(freshStudentAndStudentModel, studentEntity);

BeanUtils.copyProperties(freshStudentAndStudentModel, enrollStudentEntity);

String operator = TenancyContext.UserID.get();

String studentId = BaseUuidUtils.base58Uuid();

enrollStudentEntity.setId(BaseUuidUtils.base58Uuid());

enrollStudentEntity.setStudentId(studentId);

enrollStudentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());

enrollStudentEntity.setOperator(operator);

studentEntity.setId(studentId);

studentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard());

studentEntity.setOperator(operator);

studentEntityList.add(studentEntity);

enrollStudentEntityList.add(enrollStudentEntity);

AllusersEntity allusersEntity = new AllusersEntity();

allusersEntity.setId(enrollStudentEntity.getId());

allusersEntity.setUserCode(enrollStudentEntity.getNemtCode());

allusersEntity.setUserName(enrollStudentEntity.getName());

allusersEntity.setSchoolNo(schoolNo);

allusersEntity.setTelNum(enrollStudentEntity.getTelNum());

allusersEntity.setPassword(enrollStudentEntity.getNemtCode());  //密码设置为考生号

allusersEntityList.add(allusersEntity);

});

int nThreads = 50;

int size = enrollStudentEntityList.size();

ExecutorService executorService = Executors.newFixedThreadPool(nThreads);

List<Future> futures = new ArrayList<Future>(nThreads);

for (int i = 0; i < nThreads; i++) {

final List EnrollStudentEntityImputList = enrollStudentEntityList.subList(size / nThreads * i, size / nThreads * (i + 1));

final List studentEntityImportList = studentEntityList.subList(size / nThreads * i, size / nThreads * (i + 1));

总结

对于面试,一定要有良好的心态,这位小伙伴面试美团的时候没有被前面阿里的面试影响到,发挥也很正常,也就能顺利拿下美团的offer。
小编还整理了大厂java程序员面试涉及到的绝大部分面试题及答案,希望能帮助到大家,

在这里插入图片描述

在这里插入图片描述

最后感谢大家的支持,希望小编整理的资料能够帮助到大家!也祝愿大家都能够升职加薪!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
面试涉及到的绝大部分面试题及答案,希望能帮助到大家,

[外链图片转存中…(img-8eS2F3Vm-1713284291681)]

[外链图片转存中…(img-H76M3ko3-1713284291681)]

最后感谢大家的支持,希望小编整理的资料能够帮助到大家!也祝愿大家都能够升职加薪!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-xQtTZZPS-1713284291682)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值