mybatis-plus的配置问题

mybatis-plus的FeildStategy来判断参数是否为空作为条件

1
2
3
4
5
# 最新版本策略配置

mybatis-plus.global-config.db-config.insert-strategy=not_empty
mybatis-plus.global-config.db-config.select-strategy=not_empty
mybatis-plus.global-config.db-config.update-strategy=not_empty
1
2
3
4
5
6
mybatis-plus:
global-config:
db-config:
insert-strategy: not_empty
update-strategy: not_empty
select-strategy: not_empty

处理器配置

1
mybatis-plus.type-handlers-package=com.gaer.arch.mybatis.plus.patch.handler.common # 处理器的所在的包
1
2
mybatis-plus:
type-handlers-package: com.gaer.arch.mybatis.plus.patch.handler.common

Postgresql兼容相关问题

数组处理器

  • int数组
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
// 处理Integer数组
@MappedJdbcTypes(JdbcType.ARRAY)
@MappedTypes(Integer[].class)
public class IntArrayTypeHandler extends BaseTypeHandler<Integer[]> {


@Override
public void setNonNullParameter(PreparedStatement ps, int i, Integer[] parameter, JdbcType jdbcType) throws SQLException {
Connection connection = ps.getConnection();
Array array = connection.createArrayOf("integer", parameter);
ps.setArray(i, array);
array.free();
}

@Override
public Integer[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
return getArray(rs.getArray(columnName));
}

@Override
public Integer[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return getArray(rs.getArray(columnIndex));
}

@Override
public Integer[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return getArray(cs.getArray(columnIndex));
}

private Integer[] getArray(Array array) {
if (array == null) {
return null;
}
try {
return (Integer[]) array.getArray();
} catch (Exception e) {
}
return null;
}
}
  • String数组
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
// 处理String 数组
@MappedJdbcTypes(JdbcType.ARRAY)
@MappedTypes(Integer[].class)
public class StringArrayTypeHandler extends BaseTypeHandler<String[]> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException {
Connection connection = ps.getConnection();
Array array = connection.createArrayOf("text", parameter);
ps.setArray(i, array);
array.free();
}

@Override
public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
return getArray(rs.getArray(columnName));
}

@Override
public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return getArray(rs.getArray(columnIndex));
}

@Override
public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return getArray(cs.getArray(columnIndex));
}

private String[] getArray(Array array) {
if (array == null) {
return null;
}
try {
return (String[]) array.getArray();
} catch (Exception e) {
// ignore
}
return null;
}
}

日期时间处理器

  • LocalDateTime
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
public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {

@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType)
throws SQLException {
ps.setTimestamp(i, Timestamp.valueOf(parameter));
}

@Override
public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnName);
return getLocalDateTime(timestamp);
}

@Override
public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnIndex);
return getLocalDateTime(timestamp);
}

@Override
public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Timestamp timestamp = cs.getTimestamp(columnIndex);
return getLocalDateTime(timestamp);
}

private static LocalDateTime getLocalDateTime(Timestamp timestamp) {
if (timestamp != null) {
return timestamp.toLocalDateTime();
}
return null;
}
}
  • LocalDate
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
public class LocalDateTypeHandler extends BaseTypeHandler<LocalDate> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalDate parameter, JdbcType jdbcType) throws SQLException {
ps.setDate(i, Date.valueOf(parameter));
}

@Override
public LocalDate getNullableResult(ResultSet rs, String columnName) throws SQLException {
Date date = rs.getDate(columnName);
return getLocalDate(date);
}

@Override
public LocalDate getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Date date = rs.getDate(columnIndex);
return getLocalDate(date);
}

@Override
public LocalDate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Date date = cs.getDate(columnIndex);
return getLocalDate(date);
}

private LocalDate getLocalDate(Date date) {
if (date != null){
return date.toLocalDate();
}
return null;
}
}
  • LocalTime
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
public class LocalTimeTypeHandler extends BaseTypeHandler<LocalTime> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalTime parameter, JdbcType jdbcType) throws SQLException {
ps.setTime(i, Time.valueOf(parameter));
}

@Override
public LocalTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
Time time = rs.getTime(columnName);

return getLocalTime(time);
}

@Override
public LocalTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Time time = rs.getTime(columnIndex);

return getLocalTime(time);
}

@Override
public LocalTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Time time = cs.getTime(columnIndex);
return getLocalTime(time);
}

private LocalTime getLocalTime(Time time) {
if (time != null) {
return time.toLocalTime();
}
return null;
}
}

JSON处理器 需要放在另一个包中与普通处理器区分

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
public class LocalTimeTypeHandler extends BaseTypeHandler<LocalTime> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, LocalTime parameter, JdbcType jdbcType) throws SQLException {
ps.setTime(i, Time.valueOf(parameter));
}

@Override
public LocalTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
Time time = rs.getTime(columnName);

return getLocalTime(time);
}

@Override
public LocalTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Time time = rs.getTime(columnIndex);

return getLocalTime(time);
}

@Override
public LocalTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Time time = cs.getTime(columnIndex);
return getLocalTime(time);
}

private LocalTime getLocalTime(Time time) {
if (time != null) {
return time.toLocalTime();
}
return null;
}
}