-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
huaiyang
committed
Jan 13, 2020
1 parent
660185a
commit 4384072
Showing
6 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
common/src/main/java/com/skrshop/common/utils/EnumUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
earthsystem/src/main/java/com/skrshop/earthsystem/common/enums/AccountTypeEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...p-mybatisplus/src/main/java/com/skrshop/skrshoptkmybatis/handler/EnumCodeTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mybatis: | ||
type-handlers-package: com.skrshop.skrshoptkmybatis.handler |