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

Intellij IDEA 14 做为Java IDE 神器,接触后发现,很是好用,对它爱不释手,打算离开eclipse和myeclipse,投入Intellij IDEA的怀抱。html

 然而在使用的过程当中会发现Intellij IDEA也有一些不尽如意的地方,不免会有些不爽:Intellij IDEA 的插件库远不及eclipse的丰富。 mybatis-generator在eclipse中有专门的插件,而没有开发出Intellij IDEA可以使用的插件。java

不过不用灰心,若是你的项目是使用maven组织的,那么咱们能够在Intellij IDEA中使用 mybatis-generator-maven-plugin插件来完成MyBatis model 和Mapper文件的自动生成。sql

 

一、在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件

[html] view plain copy数据库

  1. <build>  
  2.   <finalName>xxx</finalName>  
  3.   <plugins>  
  4.     <plugin>  
  5.       <groupId>org.mybatis.generator</groupId>  
  6.       <artifactId>mybatis-generator-maven-plugin</artifactId>  
  7.       <version>1.3.2</version>  
  8.       <configuration>  
  9.         <verbose>true</verbose>  
  10.         <overwrite>true</overwrite>  
  11.       </configuration>  
  12.     </plugin>  
  13.   </plugins>  
  14. </build>  

2. 在maven项目下的src/main/resources 目录下创建名为 generatorConfig.xml的配置文件,做为mybatis-generator-maven-plugin 插件的执行目标,模板以下:

<?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>  
    <!--导入属性配置 -->  
    <properties resource="generator.properties"></properties>  
    <!--指定特定数据库的jdbc驱动jar包的位置 -->  
    <classPathEntry location="${jdbc.driverLocation}"/>  
    <context id="default" targetRuntime="MyBatis3">  
        <!-- optional,旨在建立class时,对注释进行控制 -->  
        <commentGenerator>  
            <property name="suppressDate" value="true" />  
        </commentGenerator>  
        <!--jdbc的数据库链接 -->  
        <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}">  
        </jdbcConnection>  
        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->  
        <javaTypeResolver >  
            <property name="forceBigDecimals" value="false" />  
        </javaTypeResolver>  
        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类  
            targetPackage     指定生成的model生成所在的包名  
            targetProject     指定在该项目下所在的路径  
        -->  
        <javaModelGenerator targetPackage="org.louis.hometutor.po" targetProject="src/main/java">  
            <!-- 是否对model添加 构造函数 -->  
            <property name="constructorBased" value="true"/>  
            <!-- 是否容许子包,即targetPackage.schemaName.tableName -->  
            <property name="enableSubPackages" value="false"/>  
  
            <!-- 创建的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->  
            <property name="immutable" value="true"/>  
  
            <!-- 给Model添加一个父类 -->  
            <property name="rootClass" value="com.foo.louis.Hello"/>  
            <!-- 是否对类CHAR类型的列的数据进行trim操做 -->  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
        <!--Mapper映射文件生成所在的目录 为每个数据库的表生成对应的SqlMap文件 -->  
        <sqlMapGenerator targetPackage="org.louis.hometutor.domain" targetProject="src/main/java">  
            <property name="enableSubPackages" value="false"/>  
        </sqlMapGenerator>  
        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码  
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象  
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象  
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口  
        -->  
        <javaClientGenerator targetPackage="com.foo.tourist.dao" targetProject="src/main/java" type="MIXEDMAPPER">  
            <property name="enableSubPackages" value=""/>  
            <!--  
                    定义Maper.java 源代码中的ByExample() 方法的可视性,可选的值有:  
                    public;  
                    private;  
                    protected;  
                    default  
                    注意:若是 targetRuntime="MyBatis3",此参数被忽略  
             -->  
            <property name="exampleMethodVisibility" value=""/>  
            <!--方法名计数器  
              Important note: this property is ignored if the target runtime is MyBatis3.  
             -->  
            <property name="methodNameCalculator" value=""/>  
            <!-- 为生成的接口添加父接口 -->  
            <property name="rootInterface" value=""/>  
        </javaClientGenerator>  
        <table tableName="lession" schema="louis">  
            <!-- optional   , only for mybatis3 runtime  
                 自动生成的键值(identity,或者序列值)  
               若是指定此元素,MBG将会生成<selectKey>元素,而后将此元素插入到SQL Map的<insert> 元素之中  
               sqlStatement 的语句将会返回新的值  
               若是是一个自增主键的话,你可使用预约义的语句,或者添加自定义的SQL语句. 预约义的值以下:  
                  Cloudscape    This will translate to: VALUES IDENTITY_VAL_LOCAL()  
                  DB2:      VALUES IDENTITY_VAL_LOCAL()  
                  DB2_MF:       SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1  
                  Derby:        VALUES IDENTITY_VAL_LOCAL()  
                  HSQLDB:   CALL IDENTITY()  
                  Informix:     select dbinfo('sqlca.sqlerrd1') from systables where tabid=1  
                  MySql:        SELECT LAST_INSERT_ID()  
                  SqlServer:    SELECT SCOPE_IDENTITY()  
                  SYBASE:   SELECT @@IDENTITY  
                  JDBC:     This will configure MBG to generate code for MyBatis3 suport of JDBC standard generated keys. This is a database independent method of obtaining the value from identity columns.  
                  identity: 自增主键  If true, then the column is flagged as an identity column and the generated <selectKey> element will be placed after the insert (for an identity column). If false, then the generated <selectKey> will be placed before the insert (typically for a sequence).  
-->  
            <generatedKey column="" sqlStatement="" identity="" type=""/>  
            <!-- optional.  
                    列的命名规则:  
                    MBG使用 <columnRenamingRule> 元素在计算列名的对应 名称以前,先对列名进行重命名,  
                    做用:通常须要对BUSI_CLIENT_NO 前的BUSI_进行过滤  
                    支持正在表达式  
                     searchString 表示要被换掉的字符串  
                     replaceString 则是要换成的字符串,默认状况下为空字符串,可选  
            -->  
            <columnRenamingRule searchString="" replaceString=""/>  
            <!-- optional.告诉 MBG 忽略某一列  
                    column,须要忽略的列  
                    delimitedColumnName:true ,匹配column的值和数据库列的名称 大小写彻底匹配,false 忽略大小写匹配  
                    是否限定表的列名,即固定表列在Model中的名称  
            -->  
            <ignoreColumn column="PLAN_ID"  delimitedColumnName="true" />  
            <!--optional.覆盖MBG对Model 的生成规则  
                 column: 数据库的列名  
                 javaType: 对应的Java数据类型的彻底限定名  
                 在必要的时候能够覆盖由JavaTypeResolver计算获得的java数据类型. For some databases, this is necessary to handle "odd" database types (e.g. MySql's unsigned bigint type should be mapped to java.lang.Object).  
                 jdbcType:该列的JDBC数据类型(INTEGER, DECIMAL, NUMERIC, VARCHAR, etc.),该列能够覆盖由JavaTypeResolver计算获得的Jdbc类型,对某些数据库而言,对于处理特定的JDBC 驱动癖好 颇有必要(e.g. DB2's LONGVARCHAR type should be mapped to VARCHAR for iBATIS).  
                 typeHandler:  
            -->  
            <columnOverride column="" javaType=""    jdbcType="" typeHandler=""  delimitedColumnName="" />  
        </table>  
    </context>  
</generatorConfiguration>  mybatis

这里使用了外置的配置文件generatorMybatis.properties,能够将一下属性配置到properties文件之中,增长配置的灵活性:
[plain] view plain copyoracle

  1. jdbc.driverLocation=D:\\maven\\com\\oracle\\ojdbc14\\10.2.0.4.0\\ojdbc14-10.2.0.4.0.jar  
  2. jdbc.driverClass=oracle.jdbc.driver.OracleDriver  
  3. jdbc.connectionURL=jdbc:oracle:thin:@//localhost:1521/XE  
  4. jdbc.userId=LOUIS  
  5. jdbc.password=123456  

generatorConfig.xml文件路径结构app

项目目录以下:less


 

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

 

 

以后弹出运行配置框,为当前配置配置一个名称,这里其名为"generator",而后在 “Command line” 选项中输入“mybatis-generator:generate  -e”dom

这里加了“-e ”选项是为了让该插件输出详细信息,这样能够帮助咱们定位问题。eclipse

若是添加成功,则会在run 选项中有“generator” 选项,以下:

点击运行,而后不出意外的话,会在控制台输出信息日志

若是有错误的话,因为添加了-e 选项,会把具体的详细错误信息打印出来的,根据错误信息修改便可

相关文章
相关标签/搜索