Commonly used records in Java development

1. Programmatic transactions

        1. Before executing transaction commit or rollback, when the transaction status is uncertain, you can judge whether the transaction has been completed to avoid repeated commit or rollback exceptions

        Example:

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

        2. Since the programmatic transaction will not be automatically committed or rolled back, we can add a finally after the try-catch to judge that the transaction is not completed, and roll back to ensure that each transaction will end

        Example:

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

2. Stream

        1. The data structure and simulation data used

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-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());

3. Map to object

Four, Linux common commands

        1. View all java processes

           ps -ef | grabbed java

        2. End a process

           kill -9 pid

Guess you like

Origin blog.csdn.net/weixin_44200996/article/details/127067774