zoukankan      html  css  js  c++  java
  • idea集成 MyBatis Generator 插件,自动生成dao,model,sql map文件

    过程非常简单,只需要两部就搞定了,对于码农来说还是少写了很多代码,大大提高了编码效率。

    1.集成到开发环境中

    本文以maven管理的功能来举例,只需要将插件添加到pom.xml文件中即可。(注意此处是以plugin的方式,放在<plugins> </plugins>中间即可)

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

    2.编写配置文件 generatorConfig.xml

    注意:在idea开发环境下,此文件需要放在resource根目录下,mybatis generator默认加载此目录的配置文件

    <?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>
        <!--数据库驱动mysql jar -->
    
        <classPathEntry
                location="C:Program Files (x86)MySQLConnector.J 5.1mysql-connector-java-5.1.40-bin.jar"/>
        <!--数据库驱动oracle jar -->
        <!--
        <classPathEntry
                location="E:appAcerproduct11.2.0client_2jdbclibojdbc5.jar"/>-->
        <context id="Tables" targetRuntime="MyBatis3">
            <!--去除注释 -->
    
            <commentGenerator>
                <property name="suppressAllComments" value="true"/>
            </commentGenerator>
    
            <!--mysql数据库连接 -->
    
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/goods" userId="root"
                            password="root">
            </jdbcConnection>
            <!--oracle数据库连接 -->
            <!--
           <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
                           connectionURL="jdbc:oracle:thin:@172.16.36.23:1521/FOKF"
                           userId="YCGCE" password="YCGCE">
               <property name="remarksReporting" value="true"></property>
           </jdbcConnection>-->
    
            <!--默认false Java type resolver will always use java.math.BigDecimal if
                the database column is of type DECIMAL or NUMERIC. -->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
    
            <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建 使用Maven生成在target目录下,会自动创建) -->
            <javaModelGenerator targetPackage="com.model"
                                targetProject="E:work7goodssrcmainjava">
                <property name="enableSubPackages" value="true"/>
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
            <!--生成SQLMAP文件 -->
            <sqlMapGenerator targetPackage="mapper"
                             targetProject="E:work7goodssrcmain
    esources">
                <property name="enableSubPackages" value="false"/>
            </sqlMapGenerator>
            <!--生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现 context id="DB2Tables" 修改targetRuntime="MyBatis3" -->
            <javaClientGenerator type="XMLMAPPER"
                                 targetPackage="com.dao"
                                 targetProject="E:work7goodssrcmainjava">
                <property name="enableSubPackages" value="false"/>
            </javaClientGenerator>
    
            <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等 -->
            <table tableName="t_user" domainObjectName="User"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"/>
            <table tableName="t_admin" domainObjectName="Admin"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"/>
            <table tableName="t_book" domainObjectName="Book"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"/>
            <table tableName="t_cartitem" domainObjectName="CartItem"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"/>
            <table tableName="t_category" domainObjectName="Category"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"/>
            <table tableName="t_order" domainObjectName="Order"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"/>
            <table tableName="t_orderitem" domainObjectName="OrderItem"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"/>
    
        </context>
    </generatorConfiguration>

    集成工作完成了,看看如何使用! 
    so easy,只需在plugins中找到mybatis-generator plugin即可,双击运行或右击 运行都可。如下如所示: 

    附上工程目录结构及自动生成的文件

  • 相关阅读:
    正整数分解质因数
    水仙花数
    键入任意整数,将之从小到大输出
    有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少
    输入某年某月某日,判断这一天是这一年的第几天?
    java 日期增加
    oracle数据库 ORA-01461: can bind a LONG value only for insert into a LONG column解决方案
    JAVA实现图片叠加效果
    JAVA_GET请求URL
    sqlserver-触发器-判断更新了哪个字段。
  • 原文地址:https://www.cnblogs.com/feifeicui/p/8399962.html
Copyright © 2011-2022 走看看