通过maven+mybatis-generator插件反向生成源代码,如有一个table表为resource,那么将自动生成这样三个文件:Resource.java类、ResourceMapper.java类ResourceMapper.xml配置文件。

  生成的文件如图:

  maven标识符pom.xml文件配置如下:

<plugins>
    <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>                    
      <verbose>true</verbose>
      <overwrite>true</overwrite> </configuration> </plugin> </plugins>

      mybatis-generator.xml文件配置如下(注意此文件放在src/main/resources目录下,同时配置项targetProject目录必须存在,targetPackage目录会自动生成):

<?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="H:\soft\jdbcdriver\mysql\mysql-connector-java-5.1.6-bin.jar" />
    <context id="context" targetRuntime="MyBatis3">
        <!-- 消除插件自动生成的垃圾注释  -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"
            userId="root" password="yyh081616" />
        <!-- pojo,mapper等源文件的路径 -->
        <javaModelGenerator targetPackage="cn.yyh.pojo"
            targetProject="E:\autocode" />
        <sqlMapGenerator targetPackage="cn.yyh.mapper"
            targetProject="E:\autocode" />
        <javaClientGenerator targetPackage="cn.yyh.mapper"
            targetProject="E:\autocode" type="XMLMAPPER" />
        <!-- 表信息 -->
        <table schema="test" tableName="resource" enableCountByExample="false"
            enableUpdateByExample="false" enableDeleteByExample="false"
            enableSelectByExample="false" selectByExampleQueryId="false" />
        <!-- <table schema="test" tableName="user" /> -->
    </context>
</generatorConfiguration>

  整个生成源代码的项目结构如下(注意generatorConfig.xml的路径):

      最后执行:clean mybatis-generator:generate命令即可生成源文件。