Mybatis3 generator的使用附demo 及sqlserver 分页插件
?? 网上找了好多关于Mybatis3 generator 自动化工具的教程 都说的很是含糊, 好吧 cmd什么的我实在不懂得敲。。。
详细的用法我已经在附件demo里体现了 。?
这里需要注明的是 附件里的demo也是在网上找的demo基础上改的~? 添加了关于sqlserver 分页 插件
Mybatis generator的使用主要是 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><properties resource="util/generatorConfig.properties" /><!-- classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 --><classPathEntry location="${classPath}" /><context id="MBG" targetRuntime="MyBatis3"defaultModelType="conditional"><plugin type="plugin.SelectByPagePlugin" /><!-- 此处是将Example改名为Criteria 当然 想改成什么都行~<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"><property name="searchString" value="Example$" /><property name="replaceString" value="Criteria" /></plugin>--><plugintype="org.mybatis.generator.plugins.EqualsHashCodePlugin" /><plugintype="org.mybatis.generator.plugins.MapperConfigPlugin"><property name="fileName" value="GeneratedMapperConfig.xml" /><property name="targetPackage"value="com.cy.mybatis.mbg.util" /><property name="targetProject" value="${targetProject}" /></plugin><commentGenerator> <!-- 去除自动生成的注释 --><property name="suppressAllComments" value="true" /></commentGenerator><jdbcConnection driveruserId="${userId}"password="${password}"></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver> <!-- targetProject:自动生成代码的位置 --><javaModelGenerator targetPackage="${modelPackage}"targetProject="${targetProject}"><property name="enableSubPackages" value="true" /></javaModelGenerator><sqlMapGenerator targetPackage="${sqlMapperPackage}"targetProject="${targetProject}"><property name="enableSubPackages" value="true" /></sqlMapGenerator><javaClientGenerator type="XMLMAPPER"targetPackage="${daoMapperPackage}"targetProject="${targetProject}"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 --><!-- <table tableName="visitor_info" domainObjectName="Visitor" enableCountByExample="false" enableUpdateByExample="false"enableDeleteByExample="false" enableSelectByExample="false"selectByExampleQueryId="false"></table>--><table tableName="weather_info" domainObjectName="Weather"></table></context></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? 如果不需要删掉即可~
?