概述
数据库 varchar 转换 Java 数组
typeHandler 除了处理数组转换还可以做很多事情,例如数据加密,插入数据库的时候加密,查询解密
entity类
/**
* <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 字符串和数组互转
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 数组
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 自定义字段处理器所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复