记录下IDEA集成自动生成实体类和mapper踩过的坑
码云地址
开发环境
- 开发工具:IntelliJ IDEA 2019.2.3 (Ultimate Edition)
- jdk版本:jdk1.8.0_77
- 工程构建工具:apache-maven-3.3.9
- 数据库:mysql 5.7.16
- 数据库:oracle 11.2.0.4.0 安装参考
- 项目构建:Spring Boot :: (v2.3.3.RELEASE)
pom文件修改配置
新增mybatis-generator-maven-plugin,IDEA工具下方进度条读完之后,在IDEA的Maven窗口中的Plugins有mybatis-generator的选项,包含2条指令。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--配置文件的位置-->
<!-- 可手动修改来适配 -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<!-- 引入自用工具jar,自定义数据库属性映射关系,例如Short->Integer -->
<dependency>
<groupId>com.diy.sigmund</groupId>
<artifactId>common</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
新建generatorConfig.xml配置文件
注意在plugin引入时设置了默认位置:
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
mysql配置文件信息:
需要手动需改的位置已在文件中标出,可搜索“手动修改”
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<!-- 手动修改location -->
<classPathEntry location="F:\project\download\mysql-connector-java-5.1.48-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<!-- 手动修改driverClass、connectionURL、userId、password -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/flower"
userId="root" password="">
</jdbcConnection>
<!-- 依赖一个自定义的数据类型映射配置类(独立jar) -->
<javaTypeResolver type="com.diy.sigmund.config.MyJavaTypeResovler"></javaTypeResolver>
<!-- 生成模型实体类的包名和位置-->
<!-- 手动修改targetPackage、targetProject -->
<javaModelGenerator targetPackage="com.diy.sigmund.mybatismysql.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成mapper映射文件的包名和位置-->
<!-- 手动修改targetPackage、targetProject -->
<sqlMapGenerator targetPackage="main.resources.mapper" targetProject="src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<!-- 手动修改targetPackage、targetProject -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.diy.sigmund.mybatismysql.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<!-- 手动修改tableName、domainObjectName -->
<table tableName="admire" domainObjectName="Admire" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
oracle配置文件信息:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<!-- 手动修改location -->
<classPathEntry location="F:\project\download\ojdbc6.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<!-- 手动修改driverClass、connectionURL、userId、password -->
<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@192.168.92.100:1521:orcl" userId="web" password="123456">
</jdbcConnection>
<!-- 依赖一个自定义的数据类型映射配置类(独立jar) -->
<javaTypeResolver type="com.diy.sigmund.config.MyJavaTypeResovler"></javaTypeResolver>
<!-- 生成模型实体类的包名和位置-->
<!-- 手动修改targetPackage、targetProject -->
<javaModelGenerator targetPackage="com.diy.sigmund.mybatisoracle.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成xml映射文件的包名和位置-->
<!-- 手动修改targetPackage、targetProject -->
<sqlMapGenerator targetPackage="main.resources.mapper" targetProject="src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成mapper的包名和位置-->
<!-- 手动修改targetPackage、targetProject -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.diy.sigmund.mybatisoracle.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<!-- 手动修改tableName、domainObjectName -->
<table tableName="student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="teacher" domainObjectName="Teacher" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
com.diy.sigmund.config.MyJavaTypeResovler获取
注意:mysql-connector-java-5.1.48-bin.jar
- Product Version:选择适当的版本(最新版本8.0.19没有-bin.jar文件)
- Operating System:Platform Independent
- 建议使用chrome浏览器下载
注意:ojdbc6的jar包获取
我个人是在Linux机器上取的oracle安装目录下的文件ojdbc6.jar
[oracle@ylm-100 ~]$ cd /opt/oracle/product/11.2.0/dbhome_1/jdbc/lib/
[oracle@ylm-100 lib]$ ll
总用量 26084
-rw-r--r-- 1 oracle oinstall 3445412 7月 11 2013 ojdbc5dms_g.jar
-rw-r--r-- 1 oracle oinstall 2609739 7月 11 2013 ojdbc5dms.jar
-rw-r--r-- 1 oracle oinstall 3424145 7月 11 2013 ojdbc5_g.jar
-rw-r--r-- 1 oracle oinstall 2091135 7月 11 2013 ojdbc5.jar
-rw-r--r-- 1 oracle oinstall 4517780 7月 11 2013 ojdbc6dms_g.jar
-rw-r--r-- 1 oracle oinstall 3350717 7月 11 2013 ojdbc6dms.jar
-rw-r--r-- 1 oracle oinstall 4494540 7月 11 2013 ojdbc6_g.jar
-rw-r--r-- 1 oracle oinstall 2739616 7月 11 2013 ojdbc6.jar
-rw-r--r-- 1 oracle oinstall 20365 7月 11 2013 simplefan.jar
配置maven启动
IDEA(build project)旁点击下拉按钮 | Edit Configurations | 点击 + 号 | 选择 maven | Command line 输入:mybatis-generator:generate -e | 修改name为 mybatis-generator(可自定义) | Apply | OK
- -e表示显示执行时显示错误信息
运行名为mybatis-generator的程序
在对应包下会生成对应文件
mysql application.yml配置
spring:
# profiles: dev
datasource:
url: jdbc:mysql://localhost:3306/flower?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password:
mvc:
view:
prefix: classpath:/templates/
suffix: .html
mybatis:
mapper-locations: classpath:mapper/*.xml
server:
port: 8080
servlet:
context-path: /api/demo
mysql pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.diy.sigmund</groupId>
<artifactId>mybatis-mysql</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mybatis-mysql</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--配置文件的位置-->
<!-- 可手动修改来适配 -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<!-- 引入自用工具jar,自定义数据库属性映射关系,例如Short->Integer -->
<dependency>
<groupId>com.diy.sigmund</groupId>
<artifactId>common</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
oracle application.yml配置
spring:
datasource:
url: jdbc:oracle:thin:@192.168.92.100:1521:orcl
driver-class-name: oracle.jdbc.OracleDriver
username: web
password: 123456
mvc:
view:
prefix: classpath:/templates/
suffix: .html
mybatis:
mapper-locations: classpath:mapper/*.xml
server:
port: 8080
servlet:
context-path: /api/demo
oracle pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.diy.sigmund</groupId>
<artifactId>mybatis-oracle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mybatis-oracle</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Connect to oracle -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--配置文件的位置-->
<!-- 可手动修改来适配 -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<!-- 引入自用工具jar,自定义数据库属性映射关系,例如Short->Integer -->
<dependency>
<groupId>com.diy.sigmund</groupId>
<artifactId>common</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
生成的文件示例StudentMapper
import com.diy.sigmund.mybatisoracle.entity.Student;
import org.springframework.stereotype.Component;
@Component
public interface StudentMapper {
int deleteByPrimaryKey(Integer userid);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(Integer userid);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}
问题1:URI标红报错,提示URI is not registered
http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd
解决方案:(方案2选1)
1、fetch external resource 会下载到本地文件夹 C:\Users\ylm.IntelliJIdea2019.2\system\extResources\cf63db72_mybatis-generator-config_1_0.dtd
2、File | Settings | Languages & Frameworks | Schemas and DTDs | 点击+号,添加该URI到ignored列表
问题2:java.lang.IllegalArgumentException: Result Maps collection already contains value for xxx.xxx.xxx.BaseResultMap
原因:实体类和mapper二次生成时,日志中打印 [WARNING] Existing file F:\project\diy\2020\mybatis-generator\src\main\java\com\diy\sigmund\mybatisgenerator\entity\AdmireVO.java was overwritten [WARNING] Existing file F:\project\diy\2020\mybatis-generator\src\main\java\com\diy\sigmund\mybatisgenerator\dao\AdmireVOMapper.java was overwritten 你以为是复写了,其实是追加
解决方案:删除mapper.xml追加的内容 或者 删除已生成的文件再重新生成
问题3:java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time
原因:java.sql.SQLException:服务器时区值'。©���ʱ��'无法识别或代表多个时区。 如果要利用时区支持,则必须配置服务器或JDBC驱动程序(通过“ serverTimezone”配置属性)以使用更特定的时区值。
解决方案:配置文件的url需要配置serverTimezone=GMT%2B8,例如:
url: jdbc:mysql://localhost:3306/flower?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
问题4:mapper文件只生成了insert的2个方法
原因:出现问题的oracle的表是采用create table teacher as select * from student;创建的,虽然能看到主键,但实际无法生成
需要按正常流程创建create table...并设置主键
问题5:Short类型无法默认为Integer类型
自定义数据库属性映射关系,例如Short->Integer,implements JavaTypeResolver
import java.math.BigDecimal;
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.JavaTypeResolver;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl;
import org.mybatis.generator.internal.types.Jdbc4Types;
import org.mybatis.generator.internal.util.StringUtility;
/**
* Custom mapping type ; e.g. Short -> Integer
*
* @author ylm-sigmund
* @since 2020/8/22 14:51
*/
public class MyJavaTypeResovler implements JavaTypeResolver {
protected List<String> warnings;
protected Properties properties;
protected Context context;
protected boolean forceBigDecimals;
protected Map<Integer, JavaTypeResolverDefaultImpl.JdbcTypeInformation> typeMap;
public MyJavaTypeResovler() {
super();
properties = new Properties();
typeMap = new HashMap<Integer, JavaTypeResolverDefaultImpl.JdbcTypeInformation>();
typeMap.put(Types.ARRAY, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("ARRAY", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.BIGINT, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("BIGINT", //$NON-NLS-1$
new FullyQualifiedJavaType(Long.class.getName())));
typeMap.put(Types.BINARY, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("BINARY", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.BIT, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("BIT", //$NON-NLS-1$
new FullyQualifiedJavaType(Boolean.class.getName())));
typeMap.put(Types.BLOB, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("BLOB", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.BOOLEAN, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("BOOLEAN", //$NON-NLS-1$
new FullyQualifiedJavaType(Boolean.class.getName())));
typeMap.put(Types.CHAR, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("CHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.CLOB, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("CLOB", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.DATALINK, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("DATALINK", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.DATE, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("DATE", //$NON-NLS-1$
new FullyQualifiedJavaType(Date.class.getName())));
typeMap.put(Types.DISTINCT, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("DISTINCT", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.DOUBLE, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("DOUBLE", //$NON-NLS-1$
new FullyQualifiedJavaType(Double.class.getName())));
typeMap.put(Types.FLOAT, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("FLOAT", //$NON-NLS-1$
new FullyQualifiedJavaType(Double.class.getName())));
typeMap.put(Types.INTEGER, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("INTEGER", //$NON-NLS-1$
new FullyQualifiedJavaType(Integer.class.getName())));
typeMap.put(Types.JAVA_OBJECT, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("JAVA_OBJECT", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Jdbc4Types.LONGNVARCHAR, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("LONGNVARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.LONGVARBINARY, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("LONGVARBINARY", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.LONGVARCHAR, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("LONGVARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Jdbc4Types.NCHAR, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("NCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Jdbc4Types.NCLOB, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("NCLOB", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Jdbc4Types.NVARCHAR, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("NVARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.NULL, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("NULL", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.OTHER, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("OTHER", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.REAL, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("REAL", //$NON-NLS-1$
new FullyQualifiedJavaType(Float.class.getName())));
typeMap.put(Types.REF, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("REF", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.SMALLINT, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("SMALLINT", //$NON-NLS-1$
new FullyQualifiedJavaType(Integer.class.getName())));
typeMap.put(Types.STRUCT, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("STRUCT", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.TIME, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("TIME", //$NON-NLS-1$
new FullyQualifiedJavaType(Date.class.getName())));
typeMap.put(Types.TIMESTAMP, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("TIMESTAMP", //$NON-NLS-1$
new FullyQualifiedJavaType(Date.class.getName())));
typeMap.put(Types.TINYINT, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("TINYINT", //$NON-NLS-1$
new FullyQualifiedJavaType(Byte.class.getName())));
typeMap.put(Types.VARBINARY, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("VARBINARY", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.VARCHAR, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("VARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
}
@Override
public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
forceBigDecimals =
StringUtility.isTrue(properties.getProperty(PropertyRegistry.TYPE_RESOLVER_FORCE_BIG_DECIMALS));
}
@Override
public FullyQualifiedJavaType calculateJavaType(IntrospectedColumn introspectedColumn) {
FullyQualifiedJavaType answer;
JavaTypeResolverDefaultImpl.JdbcTypeInformation jdbcTypeInformation =
typeMap.get(introspectedColumn.getJdbcType());
if (jdbcTypeInformation == null) {
switch (introspectedColumn.getJdbcType()) {
case Types.DECIMAL:
case Types.NUMERIC:
if (introspectedColumn.getScale() > 0 || introspectedColumn.getLength() > 18 || forceBigDecimals) {
answer = new FullyQualifiedJavaType(BigDecimal.class.getName());
} else if (introspectedColumn.getLength() > 9) {
answer = new FullyQualifiedJavaType(Long.class.getName());
} else if (introspectedColumn.getLength() > 4) {
answer = new FullyQualifiedJavaType(Integer.class.getName());
} else {
answer = new FullyQualifiedJavaType(Integer.class.getName());
}
break;
default:
answer = null;
break;
}
} else {
answer = jdbcTypeInformation.getFullyQualifiedJavaType();
}
return answer;
}
@Override
public String calculateJdbcTypeName(IntrospectedColumn introspectedColumn) {
String answer;
JavaTypeResolverDefaultImpl.JdbcTypeInformation jdbcTypeInformation =
typeMap.get(introspectedColumn.getJdbcType());
if (jdbcTypeInformation == null) {
switch (introspectedColumn.getJdbcType()) {
case Types.DECIMAL:
answer = "DECIMAL"; //$NON-NLS-1$
break;
case Types.NUMERIC:
answer = "NUMERIC"; //$NON-NLS-1$
break;
default:
answer = null;
break;
}
} else {
answer = jdbcTypeInformation.getJdbcTypeName();
}
return answer;
}
@Override
public void setWarnings(List<String> warnings) {
this.warnings = warnings;
}
@Override
public void setContext(Context context) {
this.context = context;
}
public static class JdbcTypeInformation {
private String jdbcTypeName;
private FullyQualifiedJavaType fullyQualifiedJavaType;
public JdbcTypeInformation(String jdbcTypeName, FullyQualifiedJavaType fullyQualifiedJavaType) {
this.jdbcTypeName = jdbcTypeName;
this.fullyQualifiedJavaType = fullyQualifiedJavaType;
}
public String getJdbcTypeName() {
return jdbcTypeName;
}
public FullyQualifiedJavaType getFullyQualifiedJavaType() {
return fullyQualifiedJavaType;
}
}
}
资料参考