浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

   网上找了好多关于Mybatis3 generator 自动化工具的教程 都说的很是含糊, 好吧 cmd什么的我实在不懂得敲。。。

详细的用法我已经在附件demo里体现了 。 

这里需要注明的是 附件里的demo也是在网上找的demo基础上改的~  添加了关于sqlserver 分页 插件

Mybatis generator的使用主要是 generatorConfig.xml配置文件的使用

 

Generatorconfig.xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE generatorConfiguration  
  3.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
  4.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
  5.   
  6. <generatorConfiguration>  
  7.     <properties resource="util/generatorConfig.properties" />  
  8.     <!-- classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 -->  
  9.       
  10.     <classPathEntry location="${classPath}" />  
  11.     <context id="MBG" targetRuntime="MyBatis3"  
  12.         defaultModelType="conditional">  
  13.         <plugin type="plugin.SelectByPagePlugin" />  
  14.         <!-- 此处是将Example改名为Criteria 当然 想改成什么都行~  
  15.             <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">  
  16.             <property name="searchString" value="Example$" />  
  17.             <property name="replaceString" value="Criteria" />  
  18.             </plugin>  
  19.         -->  
  20.         <plugin  
  21.             type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />  
  22.         <plugin  
  23.             type="org.mybatis.generator.plugins.MapperConfigPlugin">  
  24.             <property name="fileName" value="GeneratedMapperConfig.xml" />  
  25.             <property name="targetPackage"  
  26.                 value="com.cy.mybatis.mbg.util" />  
  27.             <property name="targetProject" value="${targetProject}" />  
  28.         </plugin>  
  29.         <commentGenerator>  
  30.          <!-- 去除自动生成的注释 -->  
  31.             <property name="suppressAllComments" value="true" />  
  32.         </commentGenerator>  
  33.         <jdbcConnection driverClass="${driverClass}"  
  34.             connectionURL="${connectionURL}" userId="${userId}"  
  35.             password="${password}">  
  36.         </jdbcConnection>  
  37.         <javaTypeResolver>  
  38.             <property name="forceBigDecimals" value="false" />  
  39.         </javaTypeResolver>  
  40.         <!-- targetProject:自动生成代码的位置 -->  
  41.         <javaModelGenerator targetPackage="${modelPackage}"  
  42.             targetProject="${targetProject}">  
  43.             <property name="enableSubPackages" value="true" />  
  44.         </javaModelGenerator>  
  45.   
  46.         <sqlMapGenerator targetPackage="${sqlMapperPackage}"  
  47.             targetProject="${targetProject}">  
  48.             <property name="enableSubPackages" value="true" />  
  49.         </sqlMapGenerator>  
  50.   
  51.         <javaClientGenerator type="XMLMAPPER"  
  52.             targetPackage="${daoMapperPackage}"  
  53.             targetProject="${targetProject}">  
  54.             <property name="enableSubPackages" value="true" />  
  55.         </javaClientGenerator>  
  56.         <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->  
  57.         <!--   
  58.             <table tableName="visitor_info" domainObjectName="Visitor" enableCountByExample="false" enableUpdateByExample="false"  
  59.             enableDeleteByExample="false" enableSelectByExample="false"  
  60.             selectByExampleQueryId="false">  
  61.               
  62.             </table>  
  63.         -->  
  64.         <table tableName="weather_info" domainObjectName="Weather">  
  65.         </table>  
  66.     </context>  
  67. </generatorConfiguration>  

 

其中的properties元素里引用了一个generatorConfig.properties配置文件是为了方便移植的时候,只需修改resource的路径值和generatorConfig.properties里的值即进行生成操作

 

配置文件配好了之后,

进行生成代码, 既可以使用命令的方式, 也可以自已写一个带main的类来运行, 附件里提供了一个带main的运行类

MyBatisGeneratorTool.java 。

由于在使用Mybatis generator 工具时 默认会生成example类 如果你不喜欢也可以在generatorConfig.xml中设置不自动生成。

当然test包里也提供了一个简单的包含Example类的测试用例。

 

sqlserver的分页插件 在<plugin type="plugin.SelectByPagePlugin" />
中配置~ 如果不需要取掉此行再执行就是了

为了调试方便 配置了log4j 在控制台输出sql  如果不需要删掉即可~

 

posted on 2012-02-10 00:56  lexus  阅读(3298)  评论(0编辑  收藏  举报