Mybatis(1)——Mybatis Generator自动生成代码

        MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。 MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成 实体类(POJOs)、DAO接口和Mapping映射文件

一、生成代码所需要的文件和jar包
下载地址:https://github.com/mybatis/mybatis-3/releases?after=mybatis-3.2.8


二、generatorConfig.xml配置文件

<?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>  
<!-- 数据库驱动-->  
    <classPathEntry  location="ojdbc6-1.0.0.jar"/>  
    <context id="DB2Tables"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
        <!--数据库链接URL,用户名、密码 -->  
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@10.11.11.8:1521/XHY" userId="xhy" password="xhy">  
        </jdbcConnection>
		<!--数据库中所有number类型的字段,生成POJO时都映射为BigDecimal类型--> 
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="true"/>  
        </javaTypeResolver>  
        <!-- 生成模型的包名和位置-->  
        <javaModelGenerator targetPackage="com.cah.xhy.model" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
        <!-- 生成映射文件的包名和位置-->  
        <sqlMapGenerator targetPackage="MybatisMapper" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
        <!-- 生成DAO的包名和位置-->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.cah.xhy.dao" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>  
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->  
        <table tableName="ws_ips_purchaseplan" domainObjectName="PurchasePlan" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
		<table tableName="ws_ips_purchaseplan_idoc" domainObjectName="Invoice" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
		<table tableName="ws_ips_purchaseplan_import" domainObjectName="InvoiceDetail" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
		<table tableName="ws_ips_packing_list" domainObjectName="Package" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>  
</generatorConfiguration>

三、执行生成命令
打开cmd进入lib目录,执行命令:
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
在src目录中可以找到相应的代码。

四、生成代码Oracle的number类型字段

1、生成代码时,Oracle字段类型如果是number类型(后面不带精度),则生成的实体类中,该字段是Short类型的

2、如果generatorConfig.xml中配置了:

        <javaTypeResolver>  
            <property name="forceBigDecimals" value="true"/>  
        </javaTypeResolver>

则生成的实体类中,所有number类型的字段(不管有没有设置精度),都是BigDecimal类型

3、否则会根据精度不同生成不同的类型:

number长度Java类型
1~4Short
5~9Integer
10~18Long
18+BigDecimal


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值