zoukankan      html  css  js  c++  java
  • 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:Userslenovo.m2
    epositorymysqlmysql-connector-java5.1.49mysql-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目录删除

  • 相关阅读:
    [转] Actor生命周期理解
    [转] Linux History(历史)命令用法 15 例
    [转] CDH6 安装文章链接收集
    [转] org.scalatest.FunSuite Scala Examples
    [转] Mock以及Mockito的使用
    关于 maven 打包直接运行的 fat jar (uber jar) 时需要包含本地文件系统第三方 jar 文件的问题
    [转] flume使用(六):后台启动及日志查看
    [转] etcd 搭建与使用
    [转] 2018年最新桌面CPU性能排行天梯图(含至强处理器)
    让 Linux grep 的输出不换行
  • 原文地址:https://www.cnblogs.com/ifme/p/12813950.html
Copyright © 2011-2022 走看看