一、MyBatisGeneratorConfig.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>
<!-- jdbc驱动类的路径 --> <classPathEntry location="E:\gitworkplace\mybatis_generate\mysql-connector-java-5.1.25.jar" /> <context id="mysqltables" targetRuntime="MyBatis3"> <!-- 去除自动生成的注释 --> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库连接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.98:3306/zd_maps_dev?useUnicode=true&characterEncoding=UTF-8" userId="dev" password="dev"> </jdbcConnection><javaTypeResolver>
<property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetProject:自动生成代码的存放位置 --> <!-- 生成entity类 --> <javaModelGenerator targetPackage="com.zdnst.biz.funeral.model.domain" targetProject="E:\gitworkplace\maps-jushi\zdnst-common\zdnst-biz\biz-model\src\main\java"> <property name="enableSubPackages" value="false" /> <!-- 是否针对string类型的字段在set的时候进行trim调用 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成mybatis的xml映射文件 --> <sqlMapGenerator targetPackage="mappings" targetProject="E:\gitworkplace\maps-jushi\zdnst-common\zdnst-biz\biz-model\src\main\resources"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 生成Mapper接口(即DAO接口) --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.zdnst.biz.funeral.model.mapper" targetProject="E:\gitworkplace\maps-jushi\zdnst-common\zdnst-biz\biz-model\src\main\java"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 指定表和实体类的具体映射信息 --> <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 --> <!--合作伙伴--><table tableName="ju_fn_funeral" domainObjectName="Funeral" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false"> <property name="useActualColumnNames" value="true" /> </table>
</context>
</generatorConfiguration>二、jar包
mybatis-generator-core-1.3.2.jar
mysql-connector-java-5.1.25.jar
三、使用命令
如果只想生成简单点的代码,比如没有动态查询的 在table元素的属性中有几个属性应该设置为false,示例如下:
enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" 配置文件弄好生成代码有多种方式,可以用命令行、ant、插件等方式来生成,命令行生成方式示例如下 (主要是制定jar路径和配置文件路径): java -jar E:\\gitworkplace\\mybatis_generate\\mybatis-generator-core-1.3.2.jar -configfile E:\\gitworkplace\\mybatis_generate\\MyBatisGeneratorConfig.xml -overwrite