因为默认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. Fast-RCNN论文总结整理

    此篇博客写作思路是一边翻译英文原文一边总结博主在阅读过程中遇到的问题及一些思考,因为博主本人阅读英文论文水平不高,所以还请大家在看此篇博客的过程中带着批判的眼神阅读!小墨镜带好,有什么不对的地方请在留 ...

  2. springMVC笔记一

    第一章 回顾JavaWeb中的MVC设计模式 1)MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域 2)今天说的MVC特指一种表现层设计模式,不限于Java语言 第二章 回顾stru ...

  3. 【bzoj2815】[ZJOI2012]灾难 拓扑排序+倍增LCA

    题目描述(转自洛谷) 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. 学过生物 ...

  4. BZOJ4690 Never Wait for Weights(并查集)

    带权并查集按秩合并即可维护. #include<iostream> #include<cstdio> #include<cmath> #include<cst ...

  5. 【心情】HNOI2018游记

    Day 0. 全机房的人好像都在做题.然而下午是社团节的游园会,身为社干的我风风雨雨在外面各种搬凳子搬椅子换场地招待外校同学……就这样我好像什么都没有复习. 晚上就一起去酒店了.大概因为是高一的缘故, ...

  6. [洛谷P4568][JLOI2011]飞行路线

    题目大意:最短路,可以有$k$条边无费用 题解:分层图最短路,建成$k$层,层与层之间的边费用为$0$ 卡点:空间计算出错,建边写错 C++ Code: #include <cstdio> ...

  7. 洛谷 P1268 树的重量 解题报告

    P1268 树的重量 题目描述 树可以用来表示物种之间的进化关系.一棵"进化树"是一个带边权的树,其叶节点表示一个物种,两个叶节点之间的距离表示两个物种的差异.现在,一个重要的问题 ...

  8. spring中Constructor、@Autowired、@PostConstruct的顺序【转】

    其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象p与对象a,才能执行注入.所以,如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入 ...

  9. 洛谷P1346 电车

    P1346 电车 236通过 757提交 题目提供者yeszy 标签图论福建省历届夏令营 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 解不好啊,快疯了!!哪位大… 求解:为何除了-1的点之 ...

  10. 使用babel把es6代码转成es5代码

    第一步:创建一个web项目 使用命令:npm init 这个命令的目的是生成package.json. 执行第二步中的命令后生成的package.json的文件的内容是: { "name&q ...