因为默认mybatis generator自动生成的,带小数的都转成了bigdecimal了,而且长度不同的整数转成了不同的类型。

但是我想要带小数的转成double,整数转成integer。

所有自定义类型转换

代码如下:

/*
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.generator.internal.types; 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.util.StringUtility; /**
*
* @author Jeff Butler
*/
public class MyJavaTypeResolver implements JavaTypeResolver { protected List<String> warnings; protected Properties properties; protected Context context; protected boolean forceBigDecimals; protected Map<Integer, JdbcTypeInformation> typeMap; public MyJavaTypeResolver() {
super();
properties = new Properties();
typeMap = new HashMap<Integer, JdbcTypeInformation>(); typeMap.put(Types.ARRAY, new JdbcTypeInformation("ARRAY", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.BIGINT, new JdbcTypeInformation("BIGINT", //$NON-NLS-1$
new FullyQualifiedJavaType(Long.class.getName())));
typeMap.put(Types.BINARY, new JdbcTypeInformation("BINARY", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.BIT, new JdbcTypeInformation("BIT", //$NON-NLS-1$
new FullyQualifiedJavaType(Boolean.class.getName())));
typeMap.put(Types.BLOB, new JdbcTypeInformation("BLOB", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.BOOLEAN, new JdbcTypeInformation("BOOLEAN", //$NON-NLS-1$
new FullyQualifiedJavaType(Boolean.class.getName())));
typeMap.put(Types.CHAR, new JdbcTypeInformation("CHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.CLOB, new JdbcTypeInformation("CLOB", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.DATALINK, new JdbcTypeInformation("DATALINK", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.DATE, new JdbcTypeInformation("DATE", //$NON-NLS-1$
new FullyQualifiedJavaType(Date.class.getName())));
typeMap.put(Types.DISTINCT, new JdbcTypeInformation("DISTINCT", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.DOUBLE, new JdbcTypeInformation("DOUBLE", //$NON-NLS-1$
new FullyQualifiedJavaType(Double.class.getName())));
typeMap.put(Types.FLOAT, new JdbcTypeInformation("FLOAT", //$NON-NLS-1$
new FullyQualifiedJavaType(Double.class.getName())));
typeMap.put(Types.INTEGER, new JdbcTypeInformation("INTEGER", //$NON-NLS-1$
new FullyQualifiedJavaType(Integer.class.getName())));
typeMap.put(Types.JAVA_OBJECT, new JdbcTypeInformation("JAVA_OBJECT", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.LONGNVARCHAR, new JdbcTypeInformation("LONGNVARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.LONGVARBINARY, new JdbcTypeInformation(
"LONGVARBINARY", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.LONGVARCHAR, new JdbcTypeInformation("LONGVARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.NCHAR, new JdbcTypeInformation("NCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.NCLOB, new JdbcTypeInformation("NCLOB", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.NVARCHAR, new JdbcTypeInformation("NVARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName())));
typeMap.put(Types.NULL, new JdbcTypeInformation("NULL", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.OTHER, new JdbcTypeInformation("OTHER", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.REAL, new JdbcTypeInformation("REAL", //$NON-NLS-1$
new FullyQualifiedJavaType(Float.class.getName())));
typeMap.put(Types.REF, new JdbcTypeInformation("REF", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.SMALLINT, new JdbcTypeInformation("SMALLINT", //$NON-NLS-1$
new FullyQualifiedJavaType(Short.class.getName())));
typeMap.put(Types.STRUCT, new JdbcTypeInformation("STRUCT", //$NON-NLS-1$
new FullyQualifiedJavaType(Object.class.getName())));
typeMap.put(Types.TIME, new JdbcTypeInformation("TIME", //$NON-NLS-1$
new FullyQualifiedJavaType(Date.class.getName())));
typeMap.put(Types.TIMESTAMP, new JdbcTypeInformation("TIMESTAMP", //$NON-NLS-1$
new FullyQualifiedJavaType(Date.class.getName())));
typeMap.put(Types.TINYINT, new JdbcTypeInformation("TINYINT", //$NON-NLS-1$
new FullyQualifiedJavaType(Byte.class.getName())));
typeMap.put(Types.VARBINARY, new JdbcTypeInformation("VARBINARY", //$NON-NLS-1$
new FullyQualifiedJavaType("byte[]"))); //$NON-NLS-1$
typeMap.put(Types.VARCHAR, new JdbcTypeInformation("VARCHAR", //$NON-NLS-1$
new FullyQualifiedJavaType(String.class.getName()))); } public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
forceBigDecimals = StringUtility
.isTrue(properties
.getProperty(PropertyRegistry.TYPE_RESOLVER_FORCE_BIG_DECIMALS));
} public FullyQualifiedJavaType calculateJavaType(
IntrospectedColumn introspectedColumn) {
// TODO Auto-generated method stub
FullyQualifiedJavaType answer;
JdbcTypeInformation jdbcTypeInformation = typeMap
.get(introspectedColumn.getJdbcType()); if (jdbcTypeInformation == null) {
switch (introspectedColumn.getJdbcType()) {
case Types.DECIMAL:
case Types.NUMERIC:
if(introspectedColumn.getScale() > 0)
{//如果包含小数点则转换成float
answer = new FullyQualifiedJavaType(Double.class.getName());
}else{
if ( introspectedColumn.getLength() > 18
|| forceBigDecimals) {
answer = new FullyQualifiedJavaType(Integer.class
.getName());
} else if (introspectedColumn.getLength() > 9) {
answer = new FullyQualifiedJavaType(Integer.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;
} // public FullyQualifiedJavaType calculateJavaType(
// IntrospectedColumn introspectedColumn) {
// FullyQualifiedJavaType answer;
// 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(Short.class.getName());
// }
// break;
//
// default:
// answer = null;
// break;
// }
// } else {
// answer = jdbcTypeInformation.getFullyQualifiedJavaType();
// }
//
// return answer;
// } public String calculateJdbcTypeName(IntrospectedColumn introspectedColumn) {
String answer;
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;
} public void setWarnings(List<String> warnings) {
this.warnings = warnings;
} 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;
}
}
}

  将这个文件,编译后放到org.mybatis.generator.interal.types目录下

MyJavaTypeResolver.class

MyJavaTypeResolver$JdbcTypeInformation.class

这两个class文件都要放

mybatis generator 生成javabean自定义类型转换的更多相关文章

  1. Mybatis generator生成工具简单介绍

    Mybatis generator  其主要的功能就是方便,快捷的创建好Dao,entry,xml 加快了开发速度,使用方面根据其提供的规则配置好就OK 这里还有一个重要的开发场景,开发过程中,对数据 ...

  2. 【记录】Mybatis Generator生成数据对象Date/TimeStamp 查询时间格式化

    Mybatis Generator是很好的工具帮助我们生成表映射关联代码,最近博主遇到一个问题,找了很久才解决, 就是用Mybatis Generator生成实体类的时候,Date 时间无法格式化输出 ...

  3. mybatis Generator生成代码及使用方式

    本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5889312.html 为什么要有mybatis mybatis 是一个 Java 的 ORM 框架,OR ...

  4. Maven下用MyBatis Generator生成文件

    使用Maven命令用MyBatis Generator生成MyBatis的文件步骤如下: 1.在mop文件内添加plugin <build> <finalName>KenShr ...

  5. MyBatis Generator生成DAO——序列化

    MyBatis Generator生成DAO 的时候,生成的类都是没有序列化的. 还以为要手工加入(開始是手工加入的),今天遇到分页的问题,才发现生成的时候能够加入插件. 既然分页能够有插件.序列化是 ...

  6. 利用org.mybatis.generator生成实体类

    springboot+maven+mybatis+mysql 利用org.mybatis.generator生成实体类 1.添加pom依赖:   2.编写generatorConfig.xml文件 ( ...

  7. MyBatis Generator 生成的example 使用 and or 简单混合查询

    MyBatis Generator 生成的example 使用 and or 简单混合查询 参考博客:https://www.cnblogs.com/kangping/p/6001519.html 简 ...

  8. Mybatis Generator生成数据库自带的中文注释

    1.相关jar包 <!-- mybatis生成 jar包 --> <dependency> <groupId>org.mybatis.generator</g ...

  9. MyBatis Generator 生成数据库自带中文注释

    1. maven依赖 <!-- mybatis生成 jar包 --> <dependency> <groupId>org.mybatis.generator< ...

随机推荐

  1. C++ 学习笔记之——输入和输出

    在 C++ 中,我们通过调用输入输出流库中的流对象 cin 和 cout 来实现输入和输出. #include <iostream> using namespace std; int ma ...

  2. 山科SDUST OJ Problem J :连分数

    Problem J: 连分数 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 2723  Solved: 801[Submit][Status][Web B ...

  3. 关于ubuntu上无法运行cmd markdown

    环境:ubuntu18.04 直接解压完的cmd markdown,直接点击可执行文件运行,没有反应,在通过终端运行,提示:Cannot find required executable ifconf ...

  4. python中通过string类名获得实例

    原文:https://bytes.com/topic/python/answers/42866-how-create-object-instance-string Ksenia Marasanova的 ...

  5. mysql 数据包太小会引发错误信息

    Error querying database.  Cause: com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for quer ...

  6. ob_flush()和flush()的区别

    最近写定时任务,遇到ob_flush()和flush()混淆的问题... ob_flush/flush在手册中的描述, 都是刷新输出缓冲区, 并且还需要配套使用, 所以会导致很多人迷惑- 其实, 他们 ...

  7. BZOJ4419 SHOI2013发微博(平衡树)

    好友状态的变化次数不会超过m,于是考虑暴力,对每个人记录其好友关系的变化,通过前缀和计算贡献.这需要查询一段前缀时间内某人发的微博数量,可以离线建一棵绝对平衡的平衡树.事实上完全可以线性. #incl ...

  8. 容器(vector)、数组、new创建的动态数组,你到底用哪一个(执行效率分析)

    1.问题的提出 在没有了解vector之前,动态数组一般都是又new创建的.在了解vector后发现vector竟是那样方便好用,但方便的同时却是以牺牲执行效率为代价的.网上对vector和array ...

  9. CF858F Wizard's Tour 解题报告

    题目描述 给定一张 \(n\) 个点 \(m\) 条边的无向图,每条边连接两个顶点,保证无重边自环,不保证连通. 你想在这张图上进行若干次旅游,每次旅游可以任选一个点 \(x\) 作为起点,再走到一个 ...

  10. 洛谷 P1415 拆分数列 解题报告

    拆分数列 题目背景 [为了响应党中央勤节俭.反铺张的精神,题目背景描述故事部分略去^-^] 题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数. 如果有多组解,则输出使得最后一个 ...