自定义全局变量在SpringBoot的应用
本文介绍了java使用全据变量的方法。当查询列表需要转义时,for循环内可以减少数据库频繁查询操作,sql查询语句可以减少join连接查询操作,可以提高数据库查询效率和程序效率。
全局变量类主要采用单例模式,定义业务字典全局变量(通常为<id, name>,也可根据需要自定义)
一、全局变量类GlobalBeanPropertyUtil.java
java全局变量文件存储字典数据(业务自定义)和转化方法。使用时使用单例模式,传入业务列表并用javaStream过滤
common-module模块
文件:GlobalBeanPropertyUtil.java
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Data
public class GlobalBeanPropertyUtil {
//单例模式
private static GlobalBeanPropertyUtil instance;
//全局变量(字典)
private Map<Long, String> sysUsersMap;
private Map<Long, String> factoryDeviceMap;
private GlobalBeanPropertyUtil() {
}
public static GlobalBeanPropertyUtil getInstance() {
if (instance == null) {
instance = new GlobalBeanPropertyUtil();
}
return instance;
}
//获取用户名称
public String getUserNames(String userIds){
if(StringUtils.isEmpty(userIds)){
return null;
}
List<String> userIdsList = Arrays.asList(userIds.split(","));
StringBuilder res = new StringBuilder();
for(String id : userIdsList){
String userName = this.sysUsersMap.get(Long.parseLong(id));
if(userName!=null){
res.append(userName);
res.append(",");
}
}
if(!"".equals(res.toString())) {
res.deleteCharAt(res.lastIndexOf(","));
}
return res.toString();
}
//获取设备名称
public String getDeviceNames(List<Long> deviceIdsList){
StringBuilder res = new StringBuilder();
for(Long id : deviceIdsList){
String deviceName = this.factoryDeviceMap.get(id);
if(deviceName!=null){
res.append(deviceName);
res.append(",");
}
}
if(!"".equals(res.toString())) {
res.deleteCharAt(res.lastIndexOf(","));
}
return res.toString();
}
}
二、使用示例
在for循环外,查询所有列表,并通过javaStream8转化<ley, value>(通常为<id, name> )形式的字典。
GlobalBeanPropertyUtil globalBeanPropertyUtil = GlobalBeanPropertyUtil.getInstance();
//设备-转义映射
List<FactoryDevice> listDevice = factoryDeviceService.selectFactoryDeviceList(new FactoryDevice());
Map<Long, String> factoryDeviceMap = listDevice.stream().collect(Collectors.toMap(FactoryDevice::getId,FactoryDevice::getDeviceName));
globalBeanPropertyUtil.setFactoryDeviceMap(factoryDeviceMap);
//人员-转义映射
List<SysUser> sysUsers = userService.selectAllUserList(new SysUser());
Map<Long, String> sysUsersMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId,SysUser::getUserName));
globalBeanPropertyUtil.setSysUsersMap(sysUsersMap);
for(FactoryCheckPlan plan:list){
plan.setDeviceList(factoryCheckPlanDeviceMapper.selectDeviceByPlanId(plan.getId()));
plan.setProductLineList(factoryCheckPlanDeviceMapper.selectLineByPlanId(plan.getId()));
plan.setCheckUser(globalBeanPropertyUtil.getUserNames(plan.getCheckUser()));
plan.setRummager(globalBeanPropertyUtil.getUserNames(plan.getRummager()));
if(plan.getScanLevel().equals("1") && StringUtils.isNotNull(plan.getProductLineId()))
plan.setProductLineName(String.join(",",factoryProductLineService.selectNameByIds(plan.getProductLineId().split(","))));
if(plan.getScanLevel().equals("2") && StringUtils.isNotNull(plan.getDeviceId())){
plan.setDeviceName(globalBeanPropertyUtil.getDeviceNames1(plan.getDeviceId()));
}
}