mybatis-generator生成器的使用

mybatis-generator生成器的使用

记录一下怎么使用这个插件,首先声明我使用的是springboot。

1.pom文件引入插件

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
            <!-- 主要需要改一下配置文件的位置 -->
            <configurationFile>generatorConfig.xml</configurationFile>
            <verbose>true</verbose>
            <overwrite>true</overwrite>
        </configuration>
        <executions>
            <execution>
                <id>Generate MyBatis Artifacts</id>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-core</artifactId>
                <version>1.3.2</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>

这里主要是要注意配置文件generatorConfig.xml的位置,就是我注释的那行。我把配置文件和pom文件放在同级目录。也建议都这么做,后面配置文件就可以少改一点。

2.配置generatorConfig.xml

然后是修改配置文件,主要改这么几处:

  • jdbc驱动位置
  • 数据库的连接信息
  • 实体类,dao,xml的包路径及文件路径。

如果你的配置文件generatorConfig.xml也放在和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>

    <!--JDBC驱动jar包的位置,这里就是本地仓库的位置,自己项目配置的mysql驱动-->
    <classPathEntry
            location="E:\Users\EDZ\.m2\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar"/>
    <context id="my" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>
        <!-- 数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/数据库名?tinyInt1isBit=false"
                        userId="******"
                        password="*****"/>
        <!-- 实体类 -->
        <javaModelGenerator targetPackage="com.power.tools.demo.module.admin.model"
                            targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- xml文件 -->
        <sqlMapGenerator targetPackage="mapping"
                         targetProject="./src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- dao层 -->
        <javaClientGenerator targetPackage="com.power.tools.demo.module.admin.dao"
                             targetProject="./src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 这里有几张表就复制几个 -->
        <table tableName="t_b_account" domainObjectName="Account"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>

3.运行插件

前面都配置正确的话,这里就运行一下插件就好了。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-75izeeeE-1579423353179)(mybatis-generator生成器的使用/1579422447644.png)]

就可以看到生成的文件了

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Qc5DesvU-1579423353181)(mybatis-generator生成器的使用/1579423337454.png)]

行一下插件就好了。
在这里插入图片描述

就可以看到生成的文件了
在这里插入图片描述
应该还有很多功能,如果你需要一些特别的功能的话,可以去mybatis官网看一下这个插件还有什么地方可以配置的。

发布了28 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43094917/article/details/104042534