问题描述:

在使用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

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐