- package com.lcc.util;
- import java.sql.CallableStatement;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.apache.ibatis.type.BaseTypeHandler;
- import org.apache.ibatis.type.JdbcType;
- import com.lcc.api.domain.DbEnum;
- public classDbEnumTypeHandlerextendsBaseTypeHandler {
- privateClass type;
- publicDbEnumTypeHandler(Class type){
- this.type = type;
- }
- @Override
- public voidsetNonNullParameter(PreparedStatement ps,int i,
- DbEnum parameter, JdbcType jdbcType) throws SQLException {
- ps.setInt(i, parameter.getConstant());
- }
- @Override
- public DbEnum getNullableResult(ResultSet rs, String columnName)
- throws SQLException {
- intconstant = rs.getInt(columnName);
- if(rs.wasNull()){
- return null;
- }
- else{
- return convert(constant);
- }
- }
- @Override
- publicDbEnum getNullableResult(ResultSet rs,int columnIndex)
- throws SQLException {
- intconstant = rs.getInt(columnIndex);
- if(rs.wasNull()){
- return null;
- }
- else{
- return convert(constant);
- }
- }
- @Override
- publicDbEnum getNullableResult(CallableStatement cs,int columnIndex)
- throws SQLException {
- intconstant = cs.getInt(columnIndex);
- if(cs.wasNull()){
- return null;
- }
- else{
- return convert(constant);
- }
- }
- privateDbEnum convert(int constant) {
- DbEnum[] dbEnums = type.getEnumConstants();
- for (DbEnum dbEnum : dbEnums) {
- if(dbEnum.getConstant().equals(constant)){
- return dbEnum;
- }
- }
- return null;
- }
- }
来源: http://www.bubuko.com/infodetail-1989754.html