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 解决方法
-
JDBC的URL增加
tinyInt1isBit=false
参数,注意参数名区分大小写,否则不生效例如:
jdbc:mysql://${ucmha.proxy1_2.host}/${db.mysql.db}?tinyInt1isBit=false
-
用tinyint(2)代替