返回 导航

SpringBoot / Cloud

hangge.com

SpringBoot - MyBatis-Plus使用详解16(字段类型处理器TypeHandler)

作者:hangge | 2020-05-28 08:10

十六、字段类型处理器(TypeHandler)

1,准备工作

(1)MyBatis 中的 TypeHandler 类型处理器用于 JavaType JdbcType 之间的转换,假设我们用户表中有一个联系方式字段,类型为字符串:

(2)而对应的实体类代码如下,可以看到实体类中 contact 属性类型为 Map。由于与数据库字段类型不匹配,如果不做任何处理的话无论是查询还是插入都会报错。
    这里我们通过 @TableField 注解将 FastjsonTypeHandler 这个类型处理器快速注入到 mybatis 容器中:
注意:使用字段类型处理器时,必须开启映射注解 @TableName(autoResultMap = true)。否则插入没问题,但查询时该字段会为空。
@Data
@TableName(autoResultMap = true)
public class UserInfo {
    private Integer id;
    private String userName;
    private String passWord;
    private Integer age;
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private Map<String, String> contact;
}

(3)由于这里用到了 Fastjson 这个 JSON 处理器,所以项目中也要添加相关的依赖:
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.56</version>
</dependency>

2,开始测试

(1)首先创建一个对象插入到数据库,可以看到虽然对象里的 contact 是一个 Map,但会自动转成 JSON 字符串存到数据库中:
UserInfo user = new UserInfo();
user.setUserName("hangge");
user.setPassWord("123");
//添加联系方式
Map<String, String> contact = new HashMap<>();
contact.put("phone", "010-1234567");
contact.put("tel", "13388889999");
user.setContact(contact);
userInfoMapper.insert(user);

(2)接着测试下查询,可以发现数据库的 JSON 字符串也会自动转换成 Map
List<UserInfo> users = userInfoMapper.selectList(null);
System.out.println(users);

附:自定义 TypeHandler 类型处理器

1,准备工作

(1)如果我们需要实现一个自定义的 TypeHandler 类型处理器,则需继承 ListTypeHandler 接口,接口作用是用于指定 jdbc java 的数据类型间对应关系处理。接口代码如下:
public interface TypeHandler<T> {
  // 保存操作,数据入库之前时数据处理
  void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
  //下面三个则是,从数据库加载数据后,vo对象封装前的数据处理
  T getResult(ResultSet rs, String columnName) throws SQLException;
  T getResult(ResultSet rs, int columnIndex) throws SQLException;
  T getResult(CallableStatement cs, int columnIndex) throws SQLException;
}

(2)这里我们定义一个 list 类型转 varchar(逗号隔开的字符串)的类型处理器,即 list [1,2,3] ==》varchar 1,2,3
@MappedJdbcTypes(JdbcType.VARCHAR)  //数据库类型
@MappedTypes({List.class})			//java数据类型
@Log
public class ListTypeHandler implements TypeHandler<List<String>> {

    @Override
    public void setParameter(PreparedStatement ps, int i,
                             List<String> parameter, JdbcType jdbcType) throws SQLException {
        log.info("method ====>>> setParameter");
        String hobbys = dealListToOneStr(parameter);
        ps.setString(i , hobbys);
    }

    /**
     * 集合拼接字符串
     * @param parameter
     * @return
     */
    private String dealListToOneStr(List<String> parameter){
        if(parameter == null || parameter.size() <=0)
            return null;
        String res = "";
        for (int i = 0 ;i < parameter.size(); i++) {
            if(i == parameter.size()-1){
                res+=parameter.get(i);
                return res;
            }
            res+=parameter.get(i)+",";
        }
        return null;
    }
    
    @Override
    public List<String> getResult(ResultSet rs, String columnName)
            throws SQLException {
        log.info("method ====>>> getResult(ResultSet rs, String columnName)");
        return Arrays.asList(rs.getString(columnName).split(","));
    }

    @Override
    public List<String> getResult(ResultSet rs, int columnIndex)
            throws SQLException {
        log.info("method ====>>> getResult(ResultSet rs, int columnIndex)");
        return Arrays.asList(rs.getString(columnIndex).split(","));
    }

    @Override
    public List<String> getResult(CallableStatement cs, int columnIndex) throws SQLException{
        log.info("method ====>>> getResult(CallableStatement cs, int columnIndex)");
        String hobbys = cs.getString(columnIndex);
        return Arrays.asList(hobbys.split(","));
    }
}

(3)接着在对应的实例类中使用 @TableField 注解指定使用这个自定义的字段类型处理器:
@Data
@TableName(autoResultMap = true)
public class UserInfo {
    private Integer id;
    private String userName;
    private String passWord;
    private Integer age;
    @TableField(typeHandler = ListTypeHandler.class)
    private List<String> contact;
}

2,开始测试

(1)首先创建一个对象插入到数据库,可以看到虽然对象里的 contact 是一个 List,但会自动转成逗号隔开的字符串存到数据库中:
UserInfo user = new UserInfo();
user.setUserName("hangge");
user.setPassWord("123");
//添加联系方式
List<String> contact = new ArrayList<>();
contact.add("010-1234567");
contact.add("13388889999");
contact.add("10001");
user.setContact(contact);
userInfoMapper.insert(user);

(2)接着测试下查询,可以发现数据库的字符串也会自动还原成 List
List<UserInfo> users = userInfoMapper.selectList(null);
System.out.println(users);
评论

全部评论(0)

回到顶部