1.配置pom文件

  1. <build>
  2. <plugins>
  3. <!--mybatis逆向工程插件-->
  4. <plugin>
  5. <groupId>org.mybatis.generator</groupId>
  6. <artifactId>mybatis-generator-maven-plugin</artifactId>
  7. <version>1.3.6</version>
  8. <configuration>
  9. <!--配置文件的位置-->
  10. <configurationFile>GeneratorMapper.xml</configurationFile>
  11. <verbose>true</verbose>
  12. <overwrite>true</overwrite>
  13. </configuration>
  14. </plugin>
  15. </plugins>
  16. </build>

2.导入配置文件

一般都放在和src同级的目录下
注意数据库驱动地址有没有写错

<?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="D:\DevelopSoft\repository\mysql\mysql-connector-java\5.1.43\mysql-connector-java-5.1.43.jar"/>

    <!-- 配置 table 表信息内容体,targetRuntime 指定采用 MyBatis3 的版本 -->
    <context id="tables" targetRuntime="MyBatis3">

        <!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/school"
                        userId="root"
                        password="123123">
        </jdbcConnection>


        <!-- 生成 model 类,targetPackage 指定 model 类的包名, targetProject 指定
        生成的 model 放在 Idea 的哪个工程下面-->
        <javaModelGenerator targetPackage="com.chuxin.schoolmanager.entity"
                            targetProject="D:\Code\08_JavaStudy\h02_school\schoolmanager\src\main\java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="false"/>
        </javaModelGenerator>

        <!-- 生成 MyBatis 的 Mapper.xml 文件,targetPackage 指定 mapper.xml 文件的
        包名, targetProject 指定生成的 mapper.xml 放在 eclipse 的哪个工程下面 -->
        <sqlMapGenerator targetPackage="com.chuxin.schoolmanager.mapperxml"
                         targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包
        名, targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.chuxin.schoolmanager.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>


        <!-- 数据库表名及对应的 Java 模型类名 -->
        <!--
            tableName 数据库中表的名称
            domainObjectName 表对应生成的实体类的名称
        -->
        <table tableName="tbl_student" domainObjectName="Student"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>

        <table tableName="tbl_school" domainObjectName="School"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>