mybatis-generator插件的使用及相关配置

mybatis-generator主要用于逆向工程,根据数据库来生成实体类以及mapper映射文件

1.第一步先添加依赖

<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>

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

 

2.在build标签中对插件进行配置

<plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.2</version>
          <configuration>
            <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
            <verbose>true</verbose>
            <overwrite>true</overwrite>
          </configuration>
          <dependencies>
            <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>5.1.46</version>
            </dependency>
          </dependencies>
        </plugin>

 

3.在pom文件指定的配置文件路径下添加插件的配置文件

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>
    <context id="default" targetRuntime="Mybatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/estore_db" userId="root" password="123456"/>
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <javaModelGenerator targetPackage="cn.estore.pojo" targetProject=".\src\main\java">
            <property name="enableSubPackage" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="cn.estore.mapper" targetProject="./src/main/java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <javaClientGenerator type="XMLMAPPER" targetPackage="cn.estore.mapper" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <table tableName="tb_link"/>
        <table tableName="tb_board"/>
        <table tableName="tb_payment_mode"/>
        <table tableName="tb_user"/>
        <table tableName="tb_category_main"/>
        <table tableName="tb_category_branch"/>
        <table tableName="tb_bill"/>
        <table tableName="tb_product"/>
        <table tableName="tb_bill_item"/>
        <table tableName="tb_user_cart"/>
    </context>
</generatorConfiguration>

 

4.运行插件,即可在项目中生成实体类以及数据库映射文件

 

 5.mapper映射文件在运行时可能会没有被编译,需要在build标签中添加resources配置

 <resources>
            <resource>
                <directory>src/main/java</directory><!--java文件的路径-->
                <includes>
                    <include>**/*.*</include>
                </includes>
                <!-- <filtering>false</filtering>-->
            </resource>
            <resource>
                <directory>src/main/resources</directory><!--资源文件的路径-->
                <includes>
                    <include>**/*.*</include>
                </includes>
                <!-- <filtering>false</filtering>-->
            </resource>
        </resources>

 

posted @ 2020-06-18 12:20  陌客丁  阅读(496)  评论(0编辑  收藏  举报