使用mybatis的generator自己主动生成代码,可是oracle数据库中number(6,2)总是自己主动转成BigDecimal。我想要转成的是float类型
这样就写了一个类型转换器,须要继承JavaTypeResolver接口
然后在mybaties配置文件generatorConfig.xml中类型转换配置位置加入上就可以
<javaTypeResolver type="com.generator.MyJavaTypeResolver"> <property name="forceBigDecimals" value="false" /> <!-- 类型解析器 --> </javaTypeResolver>
类型转换器MyJavaTypeResolver主要代码
public FullyQualifiedJavaType calculateJavaType( IntrospectedColumn introspectedColumn) { // TODO Auto-generated method stub FullyQualifiedJavaType answer; JdbcTypeInformation jdbcTypeInformation = typeMap .get(introspectedColumn.getJdbcType()); if (jdbcTypeInformation == null) { switch (introspectedColumn.getJdbcType()) { case Types.DECIMAL: case Types.NUMERIC: if(introspectedColumn.getScale() > 0) {//假设包括小数点则转换成float answer = new FullyQualifiedJavaType(Float.class.getName()); }else{ if ( introspectedColumn.getLength() > 18 || forceBigDecimals) { answer = new FullyQualifiedJavaType(BigDecimal.class .getName()); } else if (introspectedColumn.getLength() > 9) { answer = new FullyQualifiedJavaType(Long.class.getName()); } else if (introspectedColumn.getLength() > 4) { answer = new FullyQualifiedJavaType(Integer.class.getName()); } else { answer = new FullyQualifiedJavaType(Short.class.getName()); } } break; default: answer = null; break; } } else { answer = jdbcTypeInformation.getFullyQualifiedJavaType(); } return answer; }
时间: 2024-10-10 12:01:37