mybatiesplus实体类设置typeHandler不生效

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

mybatiesplus实体类设置typeHandler不生效

飘零未归人   2022-09-29 我要评论

实体类设置typeHandler不生效问题

实体类:

@Data
@TableName(value = "centre_manage_server_info")
public class ServerEntity {
 
    @TableId(value = "id")
    //@Column(name = "id", isKey = true, isNull = false, comment = "id" )
    private String id;
 
    /**
     * 服务器ip
     */
    @TableField(value = "ip", insertStrategy = FieldStrategy.NOT_EMPTY)
    //@Column(name = "ip", isNull = false, comment = "服务器ip" )
    private String ip;
    /**
     * 服务器port
     */
    @TableField(value = "port",  insertStrategy = FieldStrategy.NOT_NULL)
    //@Column(name = "port", isNull = false, comment = "服务器port" )
    private Integer port;
    /**
     * 服务器登录用户名
     */
    @TableField(value = "authentication_name", typeHandler = AesTypeHandler.class, insertStrategy = FieldStrategy.NOT_EMPTY)
    //@Column(name = "authentication_name", isNull = false, comment = "服务器登录用户名" )
    private String authenticationName;
    /**
     * 服务器登录密码
     */
    @TableField(value = "authentication_pwd", typeHandler = AesTypeHandler.class, insertStrategy = FieldStrategy.NOT_EMPTY)
    //@Column(name = "authentication_pwd", isNull = false, comment = "服务器登录密码" )
    private String authenticationPwd;
 
}

结果插入时时有效的,但是i查询时会出现部分没有解密的问题。

解决

1.实体类注解TableName  补充  autoResultMap = true

@TableName(value = "centre_manage_server_info", autoResultMap = true)

2.mapper.xml的resultMap也需要 

<resultMap id="ServerEntity" type="com.zhong.core.centremanage.dbservice.server.entity.ServerEntity">
        <id column="id" property="id" />
        <result column="ip" property="ip" />
        <result column="port" property="port" />
        <result column="authentication_name" property="authenticationName" typeHandler="xxxx.AesTypeHandler"/>
        <result column="authentication_pwd" property="authenticationPwd" typeHandler="xxxx.AesTypeHandler"/>
    </resultMap>

mybaties中TypeHandler的使用 

在实际项目中,有一个物品表,其中有一个规格的字段,存入了JSON数组的字符串,但是后续在使用实体类的过程中,发现些不方便,到处要转 String 和 String[]很不方便。因此希望可以直接映射。

解决

发现网上有一个TypeHandler可以做到枚举和数据字段的相互对应,那么用来做数组和String的转化应该也是可以的。代码如下:

@MappedTypes({String[].class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class StringArrayTypeHandler implements TypeHandler<String[]> {
    @Override
    public void setParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException {
        if (parameter == null)
            ps.setNull(i, Types.VARCHAR);
        else {
           JSONArray array =  new JSONArray(Arrays.asList(parameter));
            ps.setString(i, array.toString());
        }
    }
    @Override
    public String[] getResult(ResultSet rs, String s) throws SQLException {
        String columnValue = rs.getString(s);
        return this.getStringArray(columnValue);
    }
    @Override
    public String[] getResult(ResultSet rs, int columnIndex) throws SQLException {
        String columnValue = rs.getString(columnIndex);
        return this.getStringArray(columnValue);
    }
    @Override
    public String[] getResult(CallableStatement cs, int i) throws SQLException {
        String columnValue = cs.getString(i);
        return this.getStringArray(columnValue);
    }
    private String[] getStringArray(String columnValue) {
        if (columnValue == null)
            return null;
        JSONArray jsonArr = JSONArray.parseArray(columnValue);
        return  jsonArr.toArray(new String[jsonArr.size()]);
    }

由于在项目中使用了mybatisplus,所以使用如下:

 /**
     * 商品货品的规格列表
     */
    @TableField(value = "specifications",el = "specifications,typeHandler=com.seven.wechatshop.shopapi.typehandle.StringArrayTypeHandler")
    private String[] specifications;

但是上面的代码,并没有生效。后来阅读文档,发现是缺少了一个配置。如下

//注意使用了plus的是添加下面这个
mybatis-plus.type-handlers-package = com.seven.wechatshop.shopapi.typehandle

如果只是mybatis,那么添加的配置为

mybatis.type-handlers-package=com.seven.wechatshop.shopapi.typehandle

由于也是第一次使用mybatis-plus,有很多不熟悉的地方,但是渐渐熟悉以后,确实方便了很多,加快了开发的节奏和步伐。下面附上,一些条件参数说明

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们