因为mybatis的tinyint默认映射为bit类型,代码不方便操作和转换。因此需要对类型进行转化
插件地址:(https://github.com/suyin58/mybatis-generator-tddl/blob/master/generator-plugin/src/main/java/com/toolplat/generator/plugins/JavaTypeResolverMybatisDefaultImpl.java)
插件需要继承JavaTypeResolverDefaultImpl,并重写overrideDefaultType方法。针对需要转化的数据类型进行特殊处理
public class JavaTypeResolverMybatisDefaultImpl extends JavaTypeResolverDefaultImpl { @Override protected FullyQualifiedJavaType overrideDefaultType(IntrospectedColumn column, FullyQualifiedJavaType defaultType) { FullyQualifiedJavaType answer = defaultType; switch (column.getJdbcType()) { case Types.BIT: answer = this.calculateBitReplacement(column, defaultType); break; case Types.NUMERIC: case Types.DECIMAL: answer = this.calculateBigDecimalReplacement(column, defaultType); break; case Types.TINYINT: answer = new FullyQualifiedJavaType(Integer.class.getName()); break; default: break; } return answer; } }
配置文件中添加该配置:(https://github.com/suyin58/mybatis-generator-tddl/blob/master/generator-test/src/test/resources/generatorConfigMyBatis3.xml)
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver type="com.toolplat.generator.plugins.JavaTypeResolverMybatisDefaultImpl"> <property name="forceBigDecimals" value="true" /> </javaTypeResolver>