枚举类转jdbctype(spring boot下)

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 02:22   19   0

首先是创建一个枚举类(登录类型)

public enum LoginType {
 PASSWORD(0), WECHAT(1), ALIPAY(2);
 
 private int value;
 
 private LoginType(int value){
  this.value = value;
 }
 
 public boolean isHitRequired(LoginType loginType) {
  return (this.value - loginType.value)>=0;
 }
 
 @Override
 public String toString() {
  return Integer.toString(value);
 }
 
 public Integer getValue() {
  return this.value;
 }
 
 public static LoginType getClaim(int value) {
  switch(value) {
  case 0: 
   return PASSWORD;
  case 1: 
   return WECHAT;
  default:
   return ALIPAY;
  }
 }

}

然后写一个转换帮助类

@MappedJdbcTypes(JdbcType.INTEGER)
@MappedTypes(LoginType.class)
public class LoginTypeHandler extends BaseTypeHandler<LoginType>{
 
 @Override
 public void setNonNullParameter(PreparedStatement ps, int i, LoginType parameter, JdbcType jdbcType)
   throws SQLException {
  ps.setInt(i, parameter.getValue());
  
 }

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

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

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

}

最后就是转换拉(在mapper文件里写)

<result property="loginType" column="login_Type" typeHandler="方法类的路径"/>

<select></select>   //和之前一样
<insert>
    insert into LOGIN values (#{loginType,jdbcType=integer,typeHandler=方法类的路径})
</insert>

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP