Skip to content

Commit

Permalink
增加mybatis枚举转换器
Browse files Browse the repository at this point in the history
  • Loading branch information
huaiyang committed Jan 13, 2020
1 parent 660185a commit 4384072
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 1 deletion.
6 changes: 6 additions & 0 deletions common/src/main/java/com/skrshop/common/enums/BaseEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.skrshop.common.enums;


public interface BaseEnum {
int getCode();
}
18 changes: 18 additions & 0 deletions common/src/main/java/com/skrshop/common/utils/EnumUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.skrshop.common.utils;

import com.skrshop.common.enums.BaseEnum;

import java.util.Arrays;

/**
* @author huaiyang
* @version 1.0.0
* @date 2020/1/13
* @copyright 本内容仅限于浙江云贸科技有限公司内部传阅,禁止外泄以及用于其他的商业目的
*/
public class EnumUtils {
public static <T extends Enum<?> & BaseEnum> T codeOf(Class<T> enumClass, int code) {
T[] enumConstants = enumClass.getEnumConstants();
return Arrays.stream(enumConstants).filter(e -> e.getCode() == code).findFirst().orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.skrshop.earthsystem.common.enums;

import com.skrshop.common.enums.BaseEnum;

/**
* 账户类型枚举
*/
public enum AccountTypeEnum implements BaseEnum {
STAFF(0, "员工"),
USER(1, "用户");

private int code;
private String desc;

AccountTypeEnum(int code, String desc) {
this.code = code;
this.desc = desc;
}

@Override
public int getCode() {
return code;
}

public String getDesc() {
return desc;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.skrshop.skrshoptkmybatis.handler;

import com.skrshop.common.enums.BaseEnum;
import com.skrshop.common.utils.EnumUtils;
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;

/**
* @author huaiyang
* @version 1.0.0
* @date 2020/1/13
* @copyright 本内容仅限于浙江云贸科技有限公司内部传阅,禁止外泄以及用于其他的商业目的
*/
public class EnumCodeTypeHandler<E extends Enum<E> & BaseEnum> extends BaseTypeHandler<E> {

private final Class<E> type;

public EnumCodeTypeHandler(Class<E> type) {
if (type == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.type = type;
}

/**
* 用于定义设置参数时,该如何把 Java 类型的参数转换为对应的数据库类型
* @param preparedStatement
* @param i
* @param e
* @param jdbcType
* @throws SQLException
*/
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, E e, JdbcType jdbcType) throws SQLException {
preparedStatement.setInt(i, e.getCode());
}

/**
* 用于定义通过字段名称获取字段数据时,如何把数据库类型转换为对应的 Java 类型
* @param resultSet
* @param columnName
* @return
* @throws SQLException
*/
@Override
public E getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
int code = resultSet.getInt(columnName);
return resultSet.wasNull() ? null : EnumUtils.codeOf(this.type, code);
}

/**
* 用于定义通过字段索引获取字段数据时,如何把数据库类型转换为对应的 Java 类型
* @param rs
* @param columnIndex
* @return
* @throws SQLException
*/
@Override
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
int code = rs.getInt(columnIndex);
return rs.wasNull() ? null : EnumUtils.codeOf(this.type, code);
}

/**
* 用定义调用存储过程后,如何把数据库类型转换为对应的 Java 类型
* @param cs
* @param columnIndex
* @return
* @throws SQLException
*/
@Override
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
int code = cs.getInt(columnIndex);
return cs.wasNull() ? null : EnumUtils.codeOf(this.type, code);
}
}

This file was deleted.

2 changes: 2 additions & 0 deletions skrshop-mybatisplus/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mybatis:
type-handlers-package: com.skrshop.skrshoptkmybatis.handler

0 comments on commit 4384072

Please sign in to comment.