问题描述:

在使用mybatis-generator时发现数据库类型为tinyint(1)时,映射生成的字段为Boolean类型,然后在网上进行baidu发现由于tinyint(1)时默认为Boolean型,所以便将tinyint类型设置为tinyint(4).但是情况是映射过来后的数据类型为byte型。

数据库中

映射后:

解决方式:

解决这个问题需要进行手动配置相关的类型转换组件(实现JavaTypeResolverDefaultImpl类进行)
解决:
1.扩展 JavaTypeResolverDefaultImpl 并更改 calculateJavaType 方法, 在 TypeResolver 配置自己的实现

/**
* 功能描述:
* mybatis 自动生成代码数据类型转换工具
* @param:
* @author: Eggsy
* @date: 2020-03-09 14:13:18
* @return:
**/
public class JavaTypeResolverImplUtil extends JavaTypeResolverDefaultImpl{

    public JavaTypeResolverImplUtil(){
        super();
        super.typeMap.put(-6,
		new JavaTypeResolverDefaultImpl.JdbcTypeInformation("TINYINT",
			new FullyQualifiedJavaType(Integer.class.getName())));
    }
}

2.在generatorConfig.xml中添加转换类

<javaTypeResolver type="com.prospect.mes.util.JavaTypeResolverImplUtil">
     <property name="forceBigDecimals" value="false"></property>
</javaTypeResolver>

3.运行效果

参考文章

mybatis-generator代码生成(支持自定义类型转换)

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐