mybatis使用generator自動產生代碼時的類型轉換,mybatisgenerator

來源:互聯網
上載者:User

mybatis使用generator自動產生代碼時的類型轉換,mybatisgenerator

使用mybatis的generator自動產生代碼,但是oracle資料庫中number(6,2)總是自動轉成BigDecimal,我想要轉成的是float類型

這樣就寫了一個類型轉換器,需要繼承JavaTypeResolver介面

然後在mybaties設定檔generatorConfig.xml中類型轉換配置位置添加上即可

<javaTypeResolver type="com.generator.MyJavaTypeResolver"><property name="forceBigDecimals" value="false" /><!-- 類型解析器 --></javaTypeResolver>
類型轉換器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;}




MyBatis整合開發代碼怎自動產生(Ant)

mybatis3.0提供了代碼產生的功能.目前最新的是mybatis-generator-core-1.3.1.這是一個比較靈活的外掛程式.
當然不是IDE整合的,只是一個包.可以在命令列中使用,也可以用Ant,Maven甚至直接寫到Java代碼中來實現MyBatis代碼的產生.如果它還是不滿足則可以進行拓展.有了它就可以在使用建模工具產生資料庫之後,串連資料庫來產生相應的基礎代碼.包括了值對象,Data Access Objects的介面及其MyBatis實現.
個人還是喜歡用Ant的方式.下面就用Ant產生的方式來說明.首先寫個Ant檔案來聲明一些屬性和幾個Target.這些Target對於要產生的模組.分成多個模組便於管理和編譯.
build.xml:查看文本複製到剪貼簿<?xml??version="1.0"?????<project??default="sysGenerator"??basedir=
"."????????????<property??name="generated.source.dir"??value="${basedir}"??/????????????????<target??name="sysGenerator"??description="mybatis-generator"????????????????????<taskdef??name="sysGenerator"????????????????????????????classname="org.mybatis.generator.ant.GeneratorAntTask"????????????????????????????classpath=
"../Common_lib/mybatis-generator-core-1.3.1.jar"??/????????????????????<sysGenerator??overwrite="true"??configfile="sysGenerator.xml"??verbose="false"??????????????????????????????<propertyset????????????????????????????????????<propertyref??name="generated.source.dir"/????????????????????????????</propertyset????????????????????</sysGenerator????????????</target????</project????首先定義了一個名稱為sysGenerator的Target.實際操作中可以按模組建立多個.它有個taskdef 定義的是任務的類型和要引用到的包.然後就是它的本文內容.configfile就是配置產生代碼的資訊.個人建議按照系統分模組.下面看看sysGenerator.xml這個檔案的內容查看文本複製到剪貼簿<generatorConfiguration??????......餘下全文>>
 
mybatis利用generatorConfig自動產生的DAO層可以實現複雜的sql語句?

不能。。。想都別想;
工具根本不知道不複雜的商務邏輯是什麼。這才叫需求。
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.