平时积累的java经验
经验累积
1 mybatis plus 实体类的忽略字段不是用@Transient 用@TableField(exist=false)
2 用service来调用mapper.xml方法,不用mapper层来怎删改查
3命名规范,做到见文知意,不怕名字长 哪个实体类,就用哪个实体类的mapper,就算是vo 也不要乱写,不然会出问题
4 分页查询遇到去重 DISTINCT 先把去重的查询当做子查询,包起来
select n.* from
( select distinct mf.LZ_NO,mf.LZ_DD,mf.INV_NO
from MF_LZ1 mf left join TF_LZ1 tf on mf.lz_no = tf.lz_no) n
order by n.LZ_DD desc
5 @JsonSerialize(using= ToStringSerializer.class) 可以在不改变实体类字段类型的情况下,把处理好的小数位数字类型,以小数点的形式,传给前段
6 sqlserver查询,如果是一对一的表,必须拿到两张表的连个主键来关联,才能确认一对一的关系,都不用去重,只有一对多的时候才需要去重.用left
join关联表时候,除了表头表身关联外,必须找到两张表的唯一关联,哪怕用很多字段来确定唯一放在on后面
7. list转成map,Map<String, Cust> collect = custList.stream().collect(Collectors.toMap(Cust::getCusNo, itm -> itm));
8.update save方法 用updateBatchById saveBatch 数据set值以后,保存到list中,然后一总更新和保存
9 lamda 更新语句 wapper.eq是where条件中 new 出来的实体类放在 mapper.update(use,wapper)。 new 出来的实体类,里面的set进去的属性
会出现在set语句中,如果字段少,可以use的地方传null,直接在 wapper.eq().set()直接写,就不用直接new实体了.有实体适用于update字段很多的情况
10 解决查询sql语句太长的问题 if (bilVoListAll != null) {
int singleCount = 500, num, remainder, listSize, runSize;
listSize = bilVoListAll.size();
num = (listSize / singleCount);
remainder = (listSize % singleCount);
runSize = remainder > 0 ? num + 1 : num;
for (int i = 0; i < runSize; i++) {
List newList = ListUtil(hutool包工具).page(i, singleCount, bilVoListAll);
bilVoListAldFb = this.baseMapper.findAldFbForSqlserver(mfFbSlVo,newList); // 查出已发布
}
}
11 mybatis plus 查询限制500条 前段 size传-1
12 vo里面时间参数加@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)才能接受前段传来的时间参数,不然报错
13 实体类list多字段去重bilVoListAldFb.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(o -> o.getBilUsr() + “” +o.getBilId()+ “” + o.getBilNo() + “” + o.getBilItm()))),
ArrayList::new));
14 实体类list 某项值相加BigDecimal totalQuantity = mfBjSls.stream().map(MfBjSl::getQtyBj).reduce(BigDecimal.ZERO, BigdecimalUtilSum::sum);
15 实体类list 多字段分组求某项的和值Map<String, Double> map = bjDetialList.stream().collect(Collectors.groupingBy(k -> k.getBilId() + k.getBilNo() + k.getBilItm(),
Collectors.summingDouble(v -> Double.parseDouble(v.getQtyBj().toString()))));
16 Encountered unexpected token. case when语句报错,navicat可执行,mybatis报错,Mapper.java层@SqlParser(filter=true)加注解
17 list转成map 可以是bean拼接 value未list
Map<String, List> dataMap = dataList.stream().collect(
Collectors.toMap(x -> String.format("%s:%s", x.getId(), x.getName()),
obj -> Lists.newArrayList(obj),
(List newValueList, List oldValueList) ->
{
oldValueList.addAll(newValueList);
return oldValueList;
}));
18 list转成map key是bean某个属性 value未list
Map<String, List> sumXbMinusQtylistMap = sumXbMinusQtyQty.stream().collect(
Collectors.toMap(LlDifVo::getPrdNo, Lists::newArrayList,
(List newValueList, List oldValueList) ->
{
oldValueList.addAll(newValueList);
return oldValueList;
}));
19 剪切 string 用 逗号隔开 变成list bilNoList = Arrays.asList(bilNos.split(","));
20.去除数据库空格在实体里 加 getter setter
private String prdNo;
public void setPrdNo(String prdNo) {
if (StrUtil.isNotBlank(prdNo)) {
this.prdNo = prdNo.replaceAll("\r|\n", "");
} else {
this.prdNo = null;
}
}
public String getPrdNo() {
if (StrUtil.isNotBlank(this.prdNo)) {
return this.prdNo.replaceAll("\r|\n", "");
}
return null;
}
- // 使用lambda表达式forEach遍历
map.forEach((k, v) -> System.out.println(k+"===="+“1231233”));
22.Comp comp = compServe.get(SecurityConstants.FROM, String.valueOf(TenantContextHolder.getTenantId())); 一句话拿到公司
23 在对应的bean日期属性上增加注解 @JsonFormat 注解 解决日期转换异常 JSON parse error: Cannot deserialize value of typejava.util.Date
from String
24.处理BigDecimal保留小数位的工具 public class BigdecimalUtil {
public static BigDecimal format(BigDecimal big, Integer num) {
if (big == null ) {
big = BigDecimal.ZERO.setScale(num, RoundingMode.HALF_UP);
} else if (big != null) {
big = big.setScale(num, RoundingMode.HALF_UP);
}
return big;
}
}
25.Arrays.asList 返回的 List 并不是我们期望的 java.util.ArrayList,而是 Arrays 的内部类ArrayList。内部类的ArrayList没有实现add方法,而是父类的add方法的实现,是会抛出异常的呢
26.金额数值计算要使用BigDecimal
27.如果变量的初值一定会被覆盖,就没有必要给变量赋初值。
28.list变map,key重复是=时,直接接到后面
Map<String, List> collect = saveNotfbList.stream().collect(Collectors.toMap(MfXjFbSlVo :: getPreKey,
// 此时的value 为集合,方便重复时操作
s -> {
List studentNameList = new ArrayList<>();
studentNameList.add(s.getCusName());
return studentNameList;
},
// 重复时将现在的值全部加入到之前的值内
(List value1, List value2) -> {
value1.addAll(value2);
return value1;
}
));
29 mybatisplus 用LambdaUpdateChainWrapper , new LambdaUpdateChainWrapper(mfPssMapper).
eq(MfPss::getPsId, tfPss.getPsId()).
eq(MfPss::getPsNo, tfLzVo.getLzBilNo()).
set(MfPss::getLzClsId, ‘T’).
update();
30 mybatisplus实体类只更新某个字段 LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(MfLz1::getLzNo, lzNo).set(MfLz1::getRem, rem);
mfLz1Mapper.update(null, lambdaUpdateWrapper);
31 mybatisplus实体类只更新多个字段 LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(MfArp::getBilNo, lzNo);
updateWrapper.eq(MfArp::getBilId, lzId);
mfArpMapper.update(mfArp, updateWrapper);
32list转map 当key重复时,把key相同的bean整成list
Map<String, List> collect = cusVoList.stream().collect(Collectors.toMap(CusVo :: getPreKey,
// 此时的value 为集合,方便重复时操作
s -> {
List studentNameList = new ArrayList<>();
studentNameList.add(s);
return studentNameList;
},
// 重复时将现在的值全部加入到之前的值内
(List value1, List value2) -> {
value1.addAll(value2);
return value1;
}
));
33.list转map 当key重复时,把key相同的某一属性整成list
Map<String, List> collect = mysqlList.stream().collect(Collectors.toMap(MfXjFbSlVo :: getPreKey,
// 此时的value 为集合,方便重复时操作
s -> {
List studentNameList = new ArrayList<>();
studentNameList.add(s.getCusName());
return studentNameList;
},
// 重复时将现在的值全部加入到之前的值内
(List value1, List value2) -> {
value1.addAll(value2);
return value1;
}
));
34事务注解 @Transactional(
propagation = Propagation.REQUIRED,
isolation = Isolation.DEFAULT,
readOnly = false,
timeout = 60,
rollbackFor = Exception.class)
35 模糊查询 防注入 select * from news where tile like concat(’%’,#{form.hzNo}, ‘%’)
36 端口占用 netstat -ano|findstr 8088()被占用端口 taskkill /f /t /im 4736 (pid)
37 @AllArgsConstructor 与 @Value 注解不能同时使用。多余@Autowired 不然会报错Consider defining a bean of type ‘java.lang.String’ in your configuration.
38 拿到全路径截取最后文件名加.文件类型 String fileName = vo.getContract(); D:/ftp/srm 接口文档.doc
fileName = fileName.trim();
fileName = fileName.substring(fileName.lastIndexOf("\")+1); srm 接口文档.doc
39 zookeeper 修改端口号 配置文件zoo.cfg 添加 admin.serverPort=8888 dubbo-admin-server项目的application.properties文件中添加server.port=2399
40 list 处理某些字段 records.stream().forEach(e -> {
e.setQty(BigdecimalUtil.format(e.getQty(), compVo.getPoiQty()));
e.setAmtn(BigdecimalUtil.format(e.getAmtn(), compVo.getPoiAmt()));
e.setUp(BigdecimalUtil.format(e.getUp(), compVo.getPoiUpr()));
e.setQtyBj(BigdecimalUtil.format(e.getQtyBj(), compVo.getPoiQty()));
});
41 分页查询和排序冲突以后 可以在查询前就规定排序方式
com.baomidou.mybatisplus.extension.plugins.pagination.Page
page.addOrder(OrderItem.desc(“create_time”));
page.addOrder(OrderItem.asc(“bil_no”));
page.addOrder(OrderItem.desc(“bil_itm”));
42 xml foreach 写法
“${item}”
43 集合计算
List list1 = new ArrayList();
list1.add(“1”);
list1.add(“2”);
list1.add(“3”);
list1.add(“5”);
list1.add(“6”);
List<String> list2 = new ArrayList<String>();
list2.add("2");
list2.add("3");
list2.add("7");
list2.add("8");
// 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
System.out.println("---差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
System.out.println("---差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);
// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---并集 listAll---");
listAll.parallelStream().forEachOrdered(System.out :: println);
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List1---");
list1.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List2---");
list2.parallelStream().forEachOrdered(System.out :: println);