1
0
md/Java_3-5.md
2025-09-11 10:10:26 +08:00

52 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### 问题1考察多线程
假设你现在需要写1个电商APP的用户订单详情接口这个详情页包含
- 关联的商品信息,比如商品名称、尺码、图片、文字介绍等等
- 订单的基础信息,比如创建时间、支付金额
- 订单的快递信息,比如从上海运往杭州,目前运输车辆的位置
<p style="color: red">
提问这三种数据彼此之间不耦合假定每一个查询耗时1秒单线程的情况下同步执行三次查询则需要3秒如何加速这个查询
</p>
<p style="color: green">
答案:多线程并行查询
</p>
<p style="color: red">
追问假定有3个Runnable对象分别是task1、task2、task3作为查询函数对象如何调度或者编排这3个task
</p>
> 如果没反应过来,引导一下:比如说用 CompletableFuture、Future 或者 FutureTask 说一下关键,要调用哪几个函数。
<p style="color: green">
方案一、使用 CompletableFuture 的 runAsync 函数包装3个task。再通过 allOf 函数添加3个task调用 get 函数阻塞等待3个任务执行完毕。
</p>
<p style="color: green">
方案二、使用Future把3个task提交给1个自定义线程池并接收Future对象再使用get函数等待3个任务执行完毕
</p>
**如果回答的是CompletableFuture继续延伸以下问题**
<p style="color: red">CompletableFuture的默认线程池是什么</p>
<p style="color: green">答案ForkJoinPool</p>
<p style="color: red">ForkJoinPool适合CPU密集型还是IO密集型任务</p>
<p style="color: green">答案CPU密集型</p>
<p style="color: red">用CompletableFuture处理IO密集型的任务应该怎么做</p>
<p style="color: green">答案自定义一个IO密集型的线程池</p>
### 问题2考察对Mybatis-Plus或者Mybatis框架的熟练度
假设现在需要针对用户表里的**手机号、身份证号码**加密存储,业务上还需要根据*手机号后4位*和*身份证号后6位*的筛选。
<p style="color: red">
提问:请基于 Spring Boot 和 Mybatis 或 Mybatis Plus说一下*手机号*和*身份证号*写入时加密,查询时解密,并且支持*手机号后4位*和*身份证号后6位*的筛选的实现思路。
</p>
<p style="color: green">
答案:声明一个自定义注解,修饰实体类里的*手机号和身份证号码*。
创建一个类,实现 Mybatis Plus 的 MetaObjectHandler 接口,覆写 **insertFill****updateFill** 处理数据的加密。
再创建一个类,实现 Mybatis 的 Interceptor 接口,拦截 ResultSetHandler 的 handleResultSets 函数,处理数据的解密。
</p>