解决Mybatis Generator 将tinyint映射为Byte和Boolean

Mybatis Generator 对于Tinyint映射为Byte和Boolean

1. 对于tinyint(4)映射为Byte

1.1 环境
  • spring boot 2.1.x
  • mybatis
  • tk.mybatis
  • mysql
1.2 自定义类型解析器

(1) 新建以下类

public class CustomerJavaTypeResolver extends JavaTypeResolverDefaultImpl {

    public CustomerJavaTypeResolver() {
        super();
        //把数据库的 TINYINT 映射成 Integer
        super.typeMap.put(Types.TINYINT, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("TINYINT", new FullyQualifiedJavaType(Integer.class.getName())));
    }
}

(2)在pom.xml中加入

<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.29</version>
                    </dependency>
                    <!--<dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.6</version>
                    </dependency>-->
                    <dependency>
                        <groupId>com.taco.cloud</groupId>
                        <artifactId>-generator-core</artifactId>
                        <version>1.0-SNAPSHOT</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>
                </dependencies>
                <!--<executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>-->
                <configuration>
                    <!--允许移动生成的文件 -->
                    <verbose>true</verbose>
                    <!-- 是否覆盖 -->
                    <overwrite>true</overwrite>
                    <!-- 自动生成的配置 -->
                    <configurationFile>
                        src/main/resources/mybatis-generator.xml</configurationFile>
                </configuration>
            </plugin>
        </plugins>
    </build>

(3)修改mybatis-generator.xml文件,加入

<javaTypeResolver type="com.taco.springcloud.generator.CustomerJavaTypeResolver">
            <!--
            true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型
            false:默认,
               scale>0;length>18:使用BigDecimal;
               scale=0;length[10,18]:使用Long;
               scale=0;length[5,9]:使用Integer;
               scale=0;length<5:使用Short;
             -->
            <property name="forceBigDecimals" value="false"/>

        </javaTypeResolver>

2. 对于tinyint(1)映射为Boolean情况

2.1 解决方法
  1. JDBC的URL增加 tinyInt1isBit=false参数,注意参数名区分大小写,否则不生效

    例如:jdbc:mysql://${ucmha.proxy1_2.host}/${db.mysql.db}?tinyInt1isBit=false

  2. 用tinyint(2)代替

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Mybatis Generator生成代码时,默认情况下,MySQL数据库中的tinyint类型会映射JavaBoolean类型。然而,有一些情况下我们可能需要将tinyint映射JavaByte类型。为了实现这个映射,可以使用自定义的类型解析器。在自定义类型解析器中,我们可以将数据库的TINYINT映射JavaInteger类型。具体的做法是,在自定义的JavaTypeResolver中添加如下代码: ```java public class CustomerJavaTypeResolver extends JavaTypeResolverDefaultImpl { public CustomerJavaTypeResolver() { super(); //将数据库的TINYINT映射Integer类型 super.typeMap.put(Types.TINYINT, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("TINYINT", new FullyQualifiedJavaType(Integer.class.getName()))); } } ``` 通过这种方式,我们可以将MySQL中的tinyint类型映射JavaInteger类型。这样,在使用Mybatis Generator生成代码时,tinyint字段就会被映射JavaInteger类型。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Mybaits-mybatis-generator数据库tinyint数据类型类型映射问题](https://blog.csdn.net/qq_19331985/article/details/104790644)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [解决Mybatis Generatortinyint映射ByteBoolean](https://blog.csdn.net/yu542771572/article/details/116607642)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值