我是靠谱客的博主 平淡夕阳,这篇文章主要介绍Mybatis-PLUS 自定义字段处理器,现在分享给大家,希望可以做个参考。

数据库 varchar 转换 Java 数组

typeHandler 除了处理数组转换还可以做很多事情,例如数据加密,插入数据库的时候加密,查询解密

entity类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/** * <p> * * </p> * * @author Lizr * @since 2022-01-12 */ @TableName(value = "t_merchants_project", autoResultMap = true) @ApiModel(value = "TMerchantsProject对象", description = "") public class TMerchantsProject implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty("需要装换的属性") @TableField(typeHandler = JsonStringArrayTypeHandler.class) private String[] doc; }

JsonStringArrayTypeHandler 字符串和数组互转

复制代码
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
public class JsonStringArrayTypeHandler extends BaseTypeHandler<String[]> { private static final ObjectMapper mapper = new ObjectMapper(); @Override public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, toJson(parameter)); } @Override public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException { return this.toObject(rs.getString(columnName)); } @Override public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return this.toObject(rs.getString(columnIndex)); } @Override public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return this.toObject(cs.getString(columnIndex)); } private String toJson(String[] params) { try { return mapper.writeValueAsString(params); } catch (Exception e) { e.printStackTrace(); } return "[]"; } private String[] toObject(String content) { if (content != null && !content.isEmpty()) { try { return (String[]) mapper.readValue(content, String[].class); } catch (Exception e) { throw new RuntimeException(e); } } else { return null; } } }

JsonIntegerArrayTypeHandler ,字符串 转Integer 数组

复制代码
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
javapublic class JsonIntegerArrayTypeHandler extends BaseTypeHandler<Integer[]> { private static final ObjectMapper mapper = new ObjectMapper(); @Override public void setNonNullParameter(PreparedStatement ps, int i, Integer[] parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, toJson(parameter)); } @Override public Integer[] getNullableResult(ResultSet rs, String columnName) throws SQLException { return this.toObject(rs.getString(columnName)); } @Override public Integer[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return this.toObject(rs.getString(columnIndex)); } @Override public Integer[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return this.toObject(cs.getString(columnIndex)); } private String toJson(Integer[] params) { try { return mapper.writeValueAsString(params); } catch (Exception e) { e.printStackTrace(); } return "[]"; } private Integer[] toObject(String content) { if (content != null && !content.isEmpty()) { try { return (Integer[]) mapper.readValue(content, Integer[].class); } catch (Exception e) { throw new RuntimeException(e); } } else { return null; } } }

最后

以上就是平淡夕阳最近收集整理的关于Mybatis-PLUS 自定义字段处理器的全部内容,更多相关Mybatis-PLUS内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部