一、編程式事務
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.likecs.net/article/190478.htm
四、Linux常用命令
1.查看所有java進程
ps -ef | grep java
2.結束某個進程
kill -9 pid
原文地址:https://blog.csdn.net/weixin_44200996/article/details/127067774