Idea使用 MyBatis Generator 插件快速生成代码

1. 引入插件

将如下依赖添加到pom.xml文件中即可。

注意此处是以plugin的方式

<plugin>
     <groupId>org.mybatis.generator</groupId>
     <artifactId>mybatis-generator-maven-plugin</artifactId>
     <version>1.4.0</version>
</plugin>

2. 编写generatorConfig.xml

将该文件放置在resources的根目录下面

<?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>
    <!-- 1. 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry
            location="C:\Users\lenovo\.m2\repository\mysql\mysql-connector-java\5.1.49\mysql-connector-java-5.1.49.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!-- 2. 数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/datadb?characterEncoding=utf8" userId="root"
                        password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 3. 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.example.demo.model" targetProject="D:/project/demo/generator/src/">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 4. 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="com.example.demo.mapper" targetProject="D:/project/demo/generator/src/">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 5. 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="D:/project/demo/generator/src/">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 6. 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="alexa" domainObjectName="Alexa" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="develop" domainObjectName="Develop" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="ios" domainObjectName="Ios" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="num" domainObjectName="Num" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="total" domainObjectName="Total" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="users" domainObjectName="User" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>

    </context>
</generatorConfiguration>

  1. 注意根据自己本地环境,修改对应的配置信息
  2. targetProject必须真实存在
  3. 要生成的表配置取决于自己数据库中的表

3. 运行maven

4. 执行效果

将生成的代码移到项目中的路径,即可将generator目录删除

猜你喜欢

转载自www.cnblogs.com/ifme/p/12813950.html