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

Posted wsdpz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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=".srcmainjava">
            <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=".srcmainjava">
            <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>

 

以上是关于mybatis-generator插件的使用及相关配置的主要内容,如果未能解决你的问题,请参考以下文章

IEDA 使用mybatis-generator插件自动生成代码及遇到的问题

如何在idea中使用Mybatis-generator插件快速生成代码

Mybatis-Generator插件的使用与Spring集成Mybatis的配置

使用mybatis-generator插件生成mapper类

使用mybatis-generator插件生成mapper类

mybatis-generator 代码自动生成插件