mybatis自动生成代码

mybatis自动生成代码

如今的开发,作为数据持久层的框架已从Hibernate转变成了mybatis框架。相比与Hibernate,mybatis有学习难度低,SQL优化好等一些优点。
下面说一下mybatis中如何自动生成代码。
生成代码需要的东西:

- 要有表
- generatorConfig.xml文件
- 一个命令

首先说一下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="E:\study\soft\apache-tomcat-7.0.40\lib\ojdbc14-10.2.0.1.0.jar"/>  
<context id="ORACLETables"  targetRuntime="MyBatis3">  
    <commentGenerator>  
        <property name="suppressDate" value="true"/>  
        <property name="suppressAllComments" value="true"/>  
    </commentGenerator>  
    <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:localhost:db" userId="www" password="www1234">  
    </jdbcConnection>  
    <javaTypeResolver>  
        <property name="forceBigDecimals" value="true"/>  
    </javaTypeResolver>
    <!-- *model.java的生成位置 -->
    <javaModelGenerator targetPackage="com.exgrain.eoms.model.reportInfo" targetProject="D:\workspace_luna\xxx\src\main\java">  
        <property name="enableSubPackages" value="true"/>  
        <property name="trimStrings" value="true"/>  
    </javaModelGenerator> 
    <!-- *mapper.xml的生成位置 -->
    <sqlMapGenerator targetPackage="reportInfo" targetProject="D:\workspace_luna\xxx\src\main\resources\mybatis\sqlmap">  
        <property name="enableSubPackages" value="true"/>  
    </sqlMapGenerator>
    <!-- *mapper.java的生成位置 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.exgrain.eoms.mapper.reportInfo" targetProject="D:\workspace_luna\xxx\src\main\java">  
        <property name="enableSubPackages" value="true"/>  
    </javaClientGenerator>
    <!-- tableName为表名,domainObjectName为三个自动生成文件的前缀名 -->
    <table tableName="表名" domainObjectName="文件前缀名" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>  
</generatorConfiguration>

这里给大家一个建议,生成的代码一定要重新copy一份改了名字,在新的文件里写自己的新东西,在不要问我为什么,等你需要重新生成代码的时候就明白了0.0。

再来贴上自动生成时的命令:
java -jar E:\maven_jar\org\mybatis\generator\mybatis-generator-core\1.3.2\mybatis-generator-core-1.3.2.jar -configfile D:\workspace\XXX\src\main\generatorConfig.xml -overwrite
注意:mybatis-generator-core-1.3.2.jar和generatorConfig.xml 的位置一定要写对。
然后执行,一切OK。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值