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~4 | Short |
5~9 | Integer |
10~18 | Long |
18+ | BigDecimal |