MyBatis的BaseTypeHandler用法

MyBatis的BaseTypeHandler用法

有时候我们库里面存的是以逗号分隔的字符串,但返回出去需要成为一个list,若在业务代码中每次都去单独处理会很繁琐,那么我们就可以用BaseTypeHandler统一处理,当然我们也可以用来做一些字段的加密解密过程。

  • 自定义处理器,将表里逗号分隔的字符串转为 Entity 的字符串数组;将Entity 的字符串数组转为表里逗号分隔的字符串
public class StringListTypeHandler extends BaseTypeHandler<List<String>> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, String.join(",", parameter));
    }

    @Override
    public List<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String str = rs.getString(columnName);
        if (rs.wasNull() || StringUtils.isEmpty(str))
            return Collections.emptyList();

        return Arrays.asList(str.split(","));
    }

    @Override
    public List<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String str = rs.getString(columnIndex);
        if (rs.wasNull() || StringUtils.isEmpty(str))
            return Collections.emptyList();

        return Arrays.asList(str.split(","));
    }

    @Override
    public List<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String str = cs.getString(columnIndex);
        if (cs.wasNull() || StringUtils.isEmpty(str))
            return Collections.emptyList();

        return Arrays.asList(str.split(","));
    }
}
  • 自定义处理器,将表里逗号分隔的字符串转为 Entity 的字符串数组;将Entity 的字符串数组转为表里逗号分隔的字符串
public class StringMapTypeHandler extends BaseTypeHandler<Map<String, String>> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Map<String, String> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, FastJsonConvertUtil.convertObjectToJSON(parameter));
    }

    @Override
    public Map<String, String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String str = rs.getString(columnName);
        if (rs.wasNull() || StringUtils.isEmpty(str))
            return Collections.emptyMap();

        return FastJsonConvertUtil.convertJSONToObject(str, Map.class);
    }

    @Override
    public Map<String, String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String str = rs.getString(columnIndex);
        if (rs.wasNull() || StringUtils.isEmpty(str))
            return Collections.emptyMap();

        return FastJsonConvertUtil.convertJSONToObject(str, Map.class);
    }

    @Override
    public Map<String, String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String str = cs.getString(columnIndex);
        if (cs.wasNull() || StringUtils.isEmpty(str))
            return Collections.emptyMap();

        return FastJsonConvertUtil.convertJSONToObject(str, Map.class);
    }
}
  • 然后在properties配置文件中配置handler所在的包路径
mybatis.type-handlers-package=cn.wangoon.fd.tracking.management.console.mybatis.type.handler
  • 在mapper里的使用,加上@Results注解。(注意我这里用的是注解方式,xml方式有所不同)
@Select("<script> " +
        "select appId,appName,ips,members,owner from t_application " +
        "<where>" +
        "<if test=\"applicationMeta.appId != null\" >  and appId = #{applicationMeta.appId} </if>" +
        "<if test=\"applicationMeta.appName != null and applicationMeta.appName !=''\" >  and appName = #{applicationMeta.appName} </if>" +
        "<if test=\"applicationMeta.owner != null and applicationMeta.owner !=''\" >  and owner = #{applicationMeta.owner} </if>" +
        "</where> order by createTime desc limit #{curIndex},#{pageSize}" +
        "</script> ")
@Results({
        @Result(property = "ips", column = "ips", jdbcType = JdbcType.VARCHAR, typeHandler = StringListTypeHandler.class),
        @Result(property = "members", column = "members", jdbcType = JdbcType.VARCHAR, typeHandler = StringListTypeHandler.class),
})
List<ApplicationMeta> query(@Param("applicationMeta") ApplicationMeta applicationMeta, @Param("curIndex") int curIndex, @Param("pageSize") int pageSize);
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis使用自定义的LocalDateTimeTypeHandler需要以下步骤: 1. 首先,确保你已经添加了MyBatis Plus的依赖到你的项目。可以在pom.xml文件添加以下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> ``` 2. 创建一个自定义的LocalDateTimeTypeHandler类,该类需要继承自org.apache.ibatis.type.BaseTypeHandler,并实现其的方法。以下是一个示例: ```java import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.time.LocalDateTime; 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 { return rs.getTimestamp(columnName).toLocalDateTime(); } @Override public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return rs.getTimestamp(columnIndex).toLocalDateTime(); } @Override public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return cs.getTimestamp(columnIndex).toLocalDateTime(); } } ``` 3. 在MyBatis配置文件配置自定义的TypeHandler。在你的mybatis-config.xml文件,添加以下配置: ```xml <typeHandlers> <typeHandler handler="com.example.LocalDateTimeTypeHandler"/> </typeHandlers> ``` 4. 在你的实体类使用@MappedJdbcType注解指定字段的JdbcType,并使用@TypeHandler注解指定字段的TypeHandler。以下是一个示例: ```java import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedJdbcType; import org.apache.ibatis.type.TypeHandler; import java.time.LocalDateTime; public class MyEntity { @MappedJdbcType(jdbcType = JdbcType.TIMESTAMP) @TypeHandler(LocalDateTimeTypeHandler.class) private LocalDateTime createTime; // 其他字段和方法... } ``` 现在你已经完成了自定义的LocalDateTimeTypeHandler配置。在MyBatis使用时,它将自动将数据库的日期类型转换为LocalDateTime类型,并将LocalDateTime类型转换为数据库的日期类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值