好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java开发中常用记录

一、编程式事务

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

举例:

?

1

2

3

4

TransactionStatus transactionStatus =  platformTransactionManager.getTransaction(transactionDefinition);

if (!transactionStatus.isCompleted()) {

     platformTransactionManager测试数据mit(transactionStatus);

}

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

举例:

?

1

2

3

4

5

6

7

8

9

10

TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);

try {

     ... ...

} catch (Exception ex) {

     ... ...

} finally {

     if (!transactionStatus.isCompleted()) {

         platformTransactionManager.rollback(transactionStatus);

     }

}

二、Stream

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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;

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

// 数据列表 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);

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

// 数据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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

// 过滤所有女生

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()测试数据pareTo( 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种方法

http://HdhCmsTesttuohang.net/article/2735.html

四、Linux常用命令

1.查看所有java进程

ps -ef | grep java

2.结束某个进程

kill -9 pid

到此这篇关于Java开发中常用记录的文章就介绍到这了,更多相关Java开发记录内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/weixin_44200996/article/details/127067774

查看更多关于Java开发中常用记录的详细内容...

  阅读:12次