Home > Database > Mysql Tutorial > body text

mybatis使用generator自动生成代码时的类型转换

WBOY
Release: 2016-06-07 16:00:51
Original
1793 people have browsed it

使用mybatis的generator自动生成代码,但是oracle数据库中number(6,2)总是自动转成BigDecimal,我想要转成的是float类型 这样就写了一个类型转换器,需要继承JavaTypeResolver接口 然后在mybaties配置文件generatorConfig.xml中类型转换配置位置添加上即可 j

使用mybatis的generator自动生成代码,但是oracle数据库中number(6,2)总是自动转成BigDecimal,我想要转成的是float类型

这样就写了一个类型转换器,需要继承JavaTypeResolver接口

然后在mybaties配置文件generatorConfig.xml中类型转换配置位置添加上即可

<javaTypeResolver type="com.generator.MyJavaTypeResolver">
	<property name="forceBigDecimals" value="false" />											<!-- 类型解析器 -->
</javaTypeResolver>
Copy after login
类型转换器MyJavaTypeResolver主要代码
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(Float.class.getName());
	            	}else{
	            		if ( 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;
	}
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template