mybatis-generator自动生成代码插件使用详解

这个插件(不知道算不算插件)大家应该都知道他的作用,就是和逆向工程差不多的,可以根据表自动生成实体和接口(对应的)。

使用的是maven构建,首先需要在pom.xml文件添加mybatis-generator依赖包以及插件,配置如下:
dependencies中添加:

<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.2</version>
</dependency>

在build的plugins中添加:

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
       <!-- mybatis用于生成代码的配置文件 -->
      <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
</plugin>

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>
<!--    windows下路径, D:\downloads\xxx.jar-->
    <classPathEntry location="D:\moil\mysql-connector-java-5.1.6.jar"/>

    <context id="DB2Tables" targetRuntime="MyBatis3">

<!--        不再追加xml内容-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />

        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/manager?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.imock.moil.pojo" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
<!--            <property name="trimStrings" value="true" />-->
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.imock.moil.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table tableName="mall_order" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
    </context>
</generatorConfiguration>

这个文件里有几个地方要注意下:
1.

<classPathEntry location="D:\moil\mysql-connector-java-5.1.6.jar"/>

这个是jar包在window下的路径;别写错了,要不然会报错

 <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />

这个是xml里有多余的重复的sql语句,加这句就没了

<javaModelGenerator targetPackage="com.imock.moil.pojo"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
</javaModelGenerator>

这是实体类的路径

<sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
</sqlMapGenerator>

mapper文件的路径

<javaClientGenerator type="XMLMAPPER" targetPackage="com.imock.moil.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
</javaClientGenerator>

dao文件路径

 <table tableName="mall_order" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>

这个就是生成的表,后面这些是一些设置

你可能感兴趣的:(插件学习)