Java开发中常用记录

寻技术 JAVA编程 2023年07月11日 125

一、编程式事务

1.在执行事务提交或者回滚之前,事务状态不确定时,可以判断一下事务是否已完成,避免重复提交或者回滚出现异常

举例:

TransactionStatus transactionStatus =  platformTransactionManager.getTransaction(transactionDefinition);
if (!transactionStatus.isCompleted()) {
    platformTransactionManager.commit(transactionStatus);
}

 2.由于编程式事务不会自动提交或者回滚,我们可以在try-catch之后加一个finally,判断事务未完成时,进行回滚,保证每个事务一定会结束

举例:

TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
try {
    ... ...
} catch (Exception ex) {
    ... ...
} finally {
    if (!transactionStatus.isCompleted()) {
        platformTransactionManager.rollback(transactionStatus);
    }
}

二、Stream

1.使用到的数据结构及模拟数据

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class Student {
    @ApiModelProperty(value = "学生ID")
    private String studentId;
    @ApiModelProperty(value = "学生姓名")
    private String studentName;
    @ApiModelProperty(value = "学生性别,1-男,2-女")
    private String studentSex;
    @ApiModelProperty(value = "学生年龄Integer")
    private Integer studentAgeInt;
    @ApiModelProperty(value = "学生年龄Double")
    private Double studentAgeDou;
    @ApiModelProperty(value = "学生年龄BigDecimal")
    private BigDecimal studentAgeDec;
}
// 数据列表 List
List<Student> studentList = new ArrayList<>();
Student student = new Student();
student.setStudentId("20220930000001");
student.setStudentName("赵甲");
student.setStudentSex("1");
student.setStudentAgeInt(11);
student.setStudentAgeDou(11.11);
student.setStudentAgeDec(new BigDecimal(11.11));
studentList.add(student);
student = new Student();
student.setStudentId("20220930000002");
student.setStudentName("钱乙");
student.setStudentSex("2");
student.setStudentAgeInt(12);
student.setStudentAgeDou(12.12);
student.setStudentAgeDec(new BigDecimal(12.12));
studentList.add(student);
student = new Student();
student.setStudentId("20220930000003");
student.setStudentName("孙丙");
student.setStudentSex("1");
student.setStudentAgeInt(13);
student.setStudentAgeDou(13.13);
student.setStudentAgeDec(new BigDecimal(13.13));
studentList.add(student);
student = new Student();
student.setStudentId("20220930000004");
student.setStudentName("李丁");
student.setStudentSex("2");
student.setStudentAgeInt(14);
student.setStudentAgeDou(14.14);
student.setStudentAgeDec(new BigDecimal(14.14));
studentList.add(student);
student = new Student();
student.setStudentId("20220930000005");
student.setStudentName("周戊");
student.setStudentSex("1");
student.setStudentAgeInt(15);
student.setStudentAgeDou(15.15);
student.setStudentAgeDec(new BigDecimal(15.15));
studentList.add(student);
// 数据Map
Map<String,Student> studentMap = new HashMap<>(5);
Student student = new Student();
student.setStudentId("20220930000001");
student.setStudentName("赵甲");
student.setStudentSex("1");
student.setStudentAgeInt(11);
student.setStudentAgeDou(11.11);
student.setStudentAgeDec(new BigDecimal(11.11));
studentMap.put(student.getStudentId(),student);
student = new Student();
student.setStudentId("20220930000002");
student.setStudentName("钱乙");
student.setStudentSex("2");
student.setStudentAgeInt(12);
student.setStudentAgeDou(12.12);
student.setStudentAgeDec(new BigDecimal(12.12));
studentMap.put(student.getStudentId(),student);
student = new Student();
student.setStudentId("20220930000003");
student.setStudentName("孙丙");
student.setStudentSex("1");
student.setStudentAgeInt(13);
student.setStudentAgeDou(13.13);
student.setStudentAgeDec(new BigDecimal(13.13));
studentMap.put(student.getStudentId(),student);
student = new Student();
student.setStudentId("20220930000004");
student.setStudentName("李丁");
student.setStudentSex("2");
student.setStudentAgeInt(14);
student.setStudentAgeDou(14.14);
student.setStudentAgeDec(new BigDecimal(14.14));
studentMap.put(student.getStudentId(),student);
student = new Student();
student.setStudentId("20220930000005");
student.setStudentName("周戊");
student.setStudentSex("1");
student.setStudentAgeInt(15);
student.setStudentAgeDou(15.15);
student.setStudentAgeDec(new BigDecimal(15.15));
studentMap.put(student.getStudentId(),student);

 2.过滤-filter

// 过滤所有女生
List<Student> studentListT = studentList.stream()
        .filter(item -> "2".equals(item.getStudentSex())).collect(Collectors.toList());
// 过滤12岁以上学生(3种数据类型示例)
studentListT = studentList.stream().filter(item -> item.getStudentAgeInt() > 12).collect(Collectors.toList());
studentListT = studentList.stream().filter(item -> item.getStudentAgeDou() > 12).collect(Collectors.toList());
studentListT = studentList.stream()
        .filter(item -> item.getStudentAgeDec().compareTo(new BigDecimal(12)) > 0).collect(Collectors.toList());
// 过滤12岁以上男生(2种方法示例)
studentListT = studentList.stream().filter(item -> "1".equals(item.getStudentSex()))
        .filter(item -> item.getStudentAgeInt() > 12).collect(Collectors.toList());
studentListT = studentList.stream()
        .filter(item -> "1".equals(item.getStudentSex()) && item.getStudentAgeInt() > 12).collect(Collectors.toList());

三、Map转对象

最近,研究map与java对象之间的相互转换,总结了5种方法

https://www.wdphp.net/article/190478.htm

四、Linux常用命令

1.查看所有java进程

ps -ef | grep java

2.结束某个进程

kill -9 pid

原文地址:https://blog.csdn.net/weixin_44200996/article/details/127067774
关闭

用微信“扫一扫”