Intellij IDEA 2016学习系列之(二)mybatis-generator自动生成

来源:互联网 发布:dota2数据 编辑:程序博客网 时间:2024/06/15 12:12
【CSDN 技术主题月】物联网全栈开发    【评论送书】每周荐书:MySQL、Kafka、微信小程序    CSDN日报20170602 ——《程序员、技术主管和架构师》    IBM PowerAI人工智能马拉

Intellij IDEA 2016学习系列之(二)mybatis-generator自动生成

标签: mavenmybatisgeneratorintellij ideaidea
6861人阅读 评论(4)收藏举报
本文章已收录于:
分类:
作者同类文章X
    作者同类文章X
      作者同类文章X

        目录(?)[+]

        1. Intellij IDEA 2016中使用MyBatis-generator 自动生成MyBatis代码
        2. 在maven工程中的resource中创建generatorConfigxml
          1. 配置generatorConfigxml的
        3. 配置pomxml
        4. 生成对象的两种方式
          1. 方式一使用idea的maven插件直接快速生成
          2. 方式二在Intellij IDEA添加一个Run运行选项使用maven运行mybatis-generator-maven-plugin插件
            1. Step1选择配置edit configuration
            2. Step2创建maven运行项
            3. Step3配置命令 mybatis-generatorgenerate -e
            4. Step4运行

        Intellij IDEA 2016中使用MyBatis-generator 自动生成MyBatis代码

        1.在maven工程中的resource中创建generatorConfig.xml

        这里写图片描述

        配置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>   <!--mysql 连接数据库jar 这里选择自己本地位置-->   <classPathEntry location="D:/mysql-connector-java-5.1.20-bin.jar" />   <context id="testTables" targetRuntime="MyBatis3">      <commentGenerator>         <!-- 是否去除自动生成的注释 true:是 : false:否 -->         <property name="suppressAllComments" value="true" />      </commentGenerator>      <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->      <jdbcConnection driverClass="com.mysql.jdbc.Driver"         connectionURL="jdbc:mysql://localhost:3306/ecps" userId="root"         password="root">      </jdbcConnection>      <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和          NUMERIC 类型解析为java.math.BigDecimal -->      <javaTypeResolver>         <property name="forceBigDecimals" value="false" />      </javaTypeResolver>      <!-- targetProject:生成PO类的位置 -->      <javaModelGenerator targetPackage="com.ecps.seckill.pojo"         targetProject="src/main/java">         <!-- enableSubPackages:是否让schema作为包的后缀 -->         <property name="enableSubPackages" value="false" />         <!-- 从数据库返回的值被清理前后的空格 -->         <property name="trimStrings" value="true" />      </javaModelGenerator>        <!-- targetProject:mapper映射文件生成的位置            如果maven工程只是单独的一个工程,targetProject="src/main/java"           若果maven工程是分模块的工程,targetProject="所属模块的名称",例如:           targetProject="ecps-manager-mapper",下同-->      <sqlMapGenerator targetPackage="com.ecps.seckill.mapper"         targetProject="src/main/java">         <!-- enableSubPackages:是否让schema作为包的后缀 -->         <property name="enableSubPackages" value="false" />      </sqlMapGenerator>      <!-- targetPackage:mapper接口生成的位置 -->      <javaClientGenerator type="XMLMAPPER"         targetPackage="com.ecps.seckill.mapper"         targetProject="src/main/java">         <!-- enableSubPackages:是否让schema作为包的后缀 -->         <property name="enableSubPackages" value="false" />      </javaClientGenerator>      <!-- 指定数据库表 -->      <table schema="" tableName="seckill"></table>      <table schema="" tableName="success_killed"></table>      </context></generatorConfiguration>
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30
        • 31
        • 32
        • 33
        • 34
        • 35
        • 36
        • 37
        • 38
        • 39
        • 40
        • 41
        • 42
        • 43
        • 44
        • 45
        • 46
        • 47
        • 48
        • 49
        • 50
        • 51
        • 52
        • 53
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30
        • 31
        • 32
        • 33
        • 34
        • 35
        • 36
        • 37
        • 38
        • 39
        • 40
        • 41
        • 42
        • 43
        • 44
        • 45
        • 46
        • 47
        • 48
        • 49
        • 50
        • 51
        • 52
        • 53

        配置pom.xml

        在pom.xml中位置mybatis-generator的插件

        <build>    <plugins>        <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>            <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></build>
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30

        生成对象的两种方式

        方式一:使用idea的maven插件直接快速生成

        在完成以上两步之后。就会在idea中看到:直接点击mybatis-generator:generate就可生成。
        这里写图片描述

        方式二:在Intellij IDEA添加一个“Run运行”选项,使用maven运行mybatis-generator-maven-plugin插件 :

        Step1:选择配置edit configuration

        这里写图片描述

        Step2:创建maven运行项

        这里写图片描述

        Step3:配置命令 mybatis-generator:generate -e

        这里写图片描述

        Step4:运行

        这里写图片描述
        做完以上几步。就可以看到运行的选项。点击运行即可。

        1
        0
         
         

          相关文章推荐
        • Intellij IDEA 2016学习系列之(三)修改mybatis-generator源码生成中文注释
        • Intellij IDEA 14中使用MyBatis-generator 自动生成MyBatis代码
        • 使用MyBatis-Generator自动生成映射文件
        • Intellij IDEA中使用MyBatis-generator 自动生成MyBatis代码
        • MyBatis-Generator自动生成基本代码
        • mybatis-generator 自动生成带中文注释方法(附实体类)
        • Eclipse 使用mybatis generator插件自动生成代码
        • Intellij IDEA 2016学习系列之(一)创建maven 多模块项目
        • Intellij IDEA 14中使用MyBatis-generator 自动生成MyBatis代码
        • IntelliJ IDEA入门系列(3)-- mybatis 报错

        参考知识库

        img

        MySQL知识库

        img

        Java 知识库

        img

        Java EE知识库

        img

        Java SE知识库

        更多资料请参考:
        猜你在找
        经典JDBC+MyBatis学习视频
        java数据库连接技术JDBC
        使用JDBC操作MySql数据库
        轻量级当当数据库中间件 Sharding-JDBC 深度解析
        JDBC+ Oracle 高级编程精讲
        springMvc jdbc jQWidgets项目案例jasperreport自动化报表系统
        JDBC入门教程(备java基础,oracle,mysql,javaee)
        Mybatis入门到精通(备java基础,oracle,mysql,javaee必备)
        【系列课】Springmvc4+Mybatis3+Spring4+Bootstrap3之更新
        基于Maven+Springmvc+Spring+Mybatis+jQueryMobile驴友社区
        关闭
        查看评论
        3楼 幸运的天才小驴2017-01-23 15:52发表 [回复] [引用][举报]
        发现在idea中,多子工程情况下,
        报错:[WARNING] Exception retrieving table metadata: Column 'COMMENT' not found.
        [WARNING] The specified target project directory ecps-manager-mapper does not exist
        [WARNING] The specified target project directory ecps-manager-pojo does not exist
        [WARNING] The specified target project directory ecps-manager-pojo does not exist
        [WARNING] The specified target project directory ecps-manager-mapper does not exist
        结果方案:targetProject="../ecps-manager-mapper/src/main/java" 不再是ecps-manager-mapper,但是在eclipse中使用ecps-manager-mapper 是可以直接生成的。
        2楼 荒村野驴2016-12-15 22:43发表 [回复] [引用][举报]
        首先很感谢博主,按照这个帖子的步骤我实现了mapper接口和xml文件的自动生成。但是我遇到了一个问题:多次运行generator插件的话,mapper.xml文件的内容不是覆盖,而是不断的追加生成,我百度了好久都没找到合适的解决方案,博主能帮忙给点建议吗?
        谢谢。
        Re: 幸运的天才小驴2016-12-23 13:50发表 [回复] [引用][举报]
        回复ahmwh: <commentGenerator>
        <!-- 是否去除自动生成的注释 true:是 : false:否 -->
        <property name="suppressAllComments" value="true" />
        </commentGenerator>
        设置为:false 即可。
        但是这样 代码中就有非常多的注释。看起来不是很友好。
        一般我只有在需要的时候才使用这个插件,平时都是注释掉的。
        1楼 FraserYu2016-08-29 20:19发表 [回复] [引用][举报]
        非常感谢这么清晰的博文,受益匪浅,我能转载一下吗?
        发表评论
        • 用 户 名:
        • qq_15237993
        • 评论内容:
        • 插入代码
          HTML/XMLobjective-cDelphiRubyPHPC#C++JavaScriptVisual BasicPythonJavaCSSSQL其它
            
        * 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
        快速回复TOP
        核心技术类目
        全部主题HadoopAWS移动游戏JavaAndroidiOSSwift智能硬件DockerOpenStackVPNSparkERPIE10EclipseCRMJavaScript数据库UbuntuNFCWAPjQueryBIHTML5SpringApache.NETAPIHTMLSDKIISFedoraXMLLBSUnitySplashtopUMLcomponentsWindows MobileRailsQEMUKDECassandraCloudStackFTCcoremailOPhoneCouchBase云计算iOS6RackspaceWeb AppSpringSideMaemoCompuware大数据aptechPerlTornadoRubyHibernateThinkPHPHBasePureSolrAngularCloud FoundryRedisScalaDjangoBootstrap
        阅读全文
        0 0
        原创粉丝点击