java中手机号隐藏4位
2022-03-01
像这种需求可以在java代码中进行处理,也可以在mysql查询时进行处理,但数据量大了可能就会影响查询性能。所以实际开发中可能根据实际情况来进行选择。下面我就讲讲这两种方法。
1在java代码中进行处理
我这里以在service层为例子:我们先查询出整个list然后在遍历将手机号码隐藏四位
使用replaceAll方法
List<People> peoples= peopleMapper.selectList();
for (People people: peoples) {
String phone = people.getPHONE().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
designer.setPHONE(phone);
}
使用substring对手机号进行处理
List<People> peoples= peopleMapper.selectList();
for (People people: peoples) {
String phone = people.getPHONE().substring(0,people.getPHONE().length()-(people.getPHONE().substring(3)).length())+"****"+people.getPHONE().substring(7);
designer.setPHONE(phone);
}
2 直接在数据库查询的时候进行处理
select CONCAT_WS('****', substring(phone, 1, 3), substring(phone, 8, 4))
from people