数据库与实体自定义类型转换 BaseTypeHandler

当我们自定义了枚举类型或者其他某个类型、个别值,需要在往数据库中存或者在数据库取出来时转换类型或者对值做某些处理,就可以继承BaseTypeHandler,自己实现各种转换。

看下面源码是Integer类型的转换。一个setxxx方法,表示向PreparedStatement里面设置值。三个getxxx方法,一个是根据列名获取值,一个是根据列索引位置获取值,最后一个是存储过程。

public class IntegerTypeHandler extends BaseTypeHandler {

  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, Integer parameter, JdbcType jdbcType)
      throws SQLException {
    ps.setInt(i, parameter);
  }

  @Override
  public Integer getNullableResult(ResultSet rs, String columnName)
      throws SQLException {
    return rs.getInt(columnName);
  }

  @Override
  public Integer getNullableResult(ResultSet rs, int columnIndex)
      throws SQLException {
    return rs.getInt(columnIndex);
  }

  @Override
  public Integer getNullableResult(CallableStatement cs, int columnIndex)
      throws SQLException {
    return cs.getInt(columnIndex);
  }
}

我自己实现了一个枚举的转换: @MappedJdbcTypes(JdbcType.VARCHAR)注解表示与JdbcType.VARCHAR类型做转换,我试了试,写不写都行。往数据库存储时,走setNonNullParameter(...)这个方法,把enum类中的code设置进去了,在数据库中获取值后与实体类做转换时,会选择下面三个方法执行,把数据中的值转换成了enum类型,设置到实体类中。

package com.xhx.springboot.convert;

import com.xhx.springboot.enums.GenderEnum;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

@MappedJdbcTypes(JdbcType.VARCHAR)
public class GenderEnumTypeHandler extends BaseTypeHandler {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, GenderEnum genderEnum, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i,genderEnum.getCode());
    }

    @Override
    public GenderEnum getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return get(resultSet.getString(s));
    }

    @Override
    public GenderEnum getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return get(resultSet.getString(i));
    }

    @Override
    public GenderEnum getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return get(callableStatement.getString(i));
    }
    private GenderEnum get(String code){
        return Arrays.stream(GenderEnum.values()).filter(gender->gender.getCode().equals(code)).findFirst().orElse(null);
    }
}

我的枚举类:

package com.xhx.springboot.enums;

public enum  GenderEnum {
    MAN("0","男"),WOMAN("1","女");

    GenderEnum(String code,String value){
        this.code = code;
        this.value = value;
    }
    private String code;
    private String value;

    public String getCode() {
        return code;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "GenderEnum{" +
                "code='" + code + '\'' +
                ", value='" + value + '\'' +
                '}';
    }
}

实体类:

package com.xhx.springboot.entity;

import com.xhx.springboot.enums.GenderEnum;

public class User {
    private Integer id;
    private String name;
    private Double money;
    private GenderEnum gender;

    public GenderEnum getGender() {
        return gender;
    }

    public void setGender(GenderEnum gender) {
        this.gender = gender;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                ", gender=" + gender +
                '}';
    }
}

把TypeHandler配置到程序中,有下面三种方法:

1.在Mapper.xml中声明

2.在mybatis配置文件中设置  

    
        
    

3.在spring配置文件中设置,springboot项目如下

mybatis:
  type-handlers-package: com.xhx.springboot.convert

实时内容请关注微信公众号,公众号与博客同时更新:程序员星星

你可能感兴趣的:(mybatis)