/** * The default constructor. */ publicTypeHandlerRegistry(){ this(new Configuration()); }
/** * The constructor that pass the MyBatis configuration. * * @param configuration a MyBatis configuration * @since 3.5.4 */ publicTypeHandlerRegistry(Configuration configuration){ this.unknownTypeHandler = new UnknownTypeHandler(configuration);
register(Boolean.class, new BooleanTypeHandler()); register(boolean.class, new BooleanTypeHandler()); register(JdbcType.BOOLEAN, new BooleanTypeHandler()); register(JdbcType.BIT, new BooleanTypeHandler());
register(Byte.class, new ByteTypeHandler()); register(byte.class, new ByteTypeHandler()); register(JdbcType.TINYINT, new ByteTypeHandler()); //.........省略.......... }
<selectid="queryById"parameterType="long"resultType="actor"> SELECT * FROM actor WHERE actor_id = #{actor_id} </select>
<selectid="queryById"resultMap="actorMap"> SELECT * FROM actor </select>
<selectid="findActor"resultMap="actorMap"> SELECT * FROM actor WHERE first_name LIKE concat('%', #{firstName javaType=string jdbcType=VARCHAR typeHadler=com.gxitsky.config.MyStringTypeHandler} , '%'); </select>
/** * @author Clinton Begin */ publicclassEnumTypeHandler<EextendsEnum<E>> extendsBaseTypeHandler<E> {
privatefinal Class<E> type;
publicEnumTypeHandler(Class<E> type){ if (type == null) { thrownew IllegalArgumentException("Type argument cannot be null"); } this.type = type; }
@Override publicvoidsetNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType)throws SQLException { if (jdbcType == null) { // 取的是字典的 name 属性 ps.setString(i, parameter.name()); } else { ps.setObject(i, parameter.name(), jdbcType.TYPE_CODE); // see r3589 } }
@Override public E getNullableResult(ResultSet rs, String columnName)throws SQLException { String s = rs.getString(columnName); // 返回枚举值 return s == null ? null : Enum.valueOf(type, s); }
@Override public E getNullableResult(ResultSet rs, int columnIndex)throws SQLException { String s = rs.getString(columnIndex); // 返回枚举值 return s == null ? null : Enum.valueOf(type, s); }
@Override public E getNullableResult(CallableStatement cs, int columnIndex)throws SQLException { // 返回枚举值 String s = cs.getString(columnIndex); return s == null ? null : Enum.valueOf(type, s); } }
publicstaticvoidmain(String[] args){ String name = SexEnum.MALE.name(); System.out.println(name);// name = MALE 字符串 SexEnum male = Enum.valueOf(SexEnum.class, "MALE"); System.out.println(male); // male = MALE 枚举值 } }
publicEnumOrdinalTypeHandler(Class<E> type){ if (type == null) { thrownew IllegalArgumentException("Type argument cannot be null"); } // 枚举class this.type = type; // 拿到所有元素 this.enums = type.getEnumConstants(); if (this.enums == null) { thrownew IllegalArgumentException(type.getSimpleName() + " does not represent an enum type."); } }
@Override publicvoidsetNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType)throws SQLException { // 入参取枚举的下标,存储的是枚举的下标 ps.setInt(i, parameter.ordinal()); }
@Override public E getNullableResult(ResultSet rs, String columnName)throws SQLException { int ordinal = rs.getInt(columnName); if (ordinal == 0 && rs.wasNull()) { returnnull; } // 取出枚举的下标, 返回枚举 return toOrdinalEnum(ordinal); }
@Override public E getNullableResult(ResultSet rs, int columnIndex)throws SQLException { int ordinal = rs.getInt(columnIndex); if (ordinal == 0 && rs.wasNull()) { returnnull; } return toOrdinalEnum(ordinal); }
@Override public E getNullableResult(CallableStatement cs, int columnIndex)throws SQLException { int ordinal = cs.getInt(columnIndex); if (ordinal == 0 && cs.wasNull()) { returnnull; } return toOrdinalEnum(ordinal); }