logo-cover-idea Mybatis generator插件的配置和使用
    idea Mybatis generator插件的配置和使用,在eclipse中,我们可以在eclipse的插件市场安装一个Mybatis generator的插件。然后对Mybatis generator插件的配置文件进行右键run就好啦。IDea中目前还没发现这么快捷的插件。但是也可以使用maven的方式来实现。

步骤一:添加Mybatis generator依赖

<plugin>
    <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.6</version>
    <configuration>
        <!--配置文件的位置-->
        <configurationFile>src/main/resources/mybatisGeneratorConfigl.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
    <executions>
        <execution>
            <id>Generate MyBatis Artifacts</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <execution>
                 <id>life</id>
                 <phase>deploy</phase>
            </execution><!--避免执行package执行该插件-->
        </execution>

    </executions>
    <dependencies>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.6</version>
        </dependency>
    </dependencies>
</plugin>

注意添加的节点是plugin



 

步骤二:编写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>
   <classPathEntry location="D:/maven_res/mysql/mysql-connector-java/5.1.29/mysql-connector-java-5.1.29.jar" />

   <context id="mysqlTables" targetRuntime="MyBatis3">
      <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
      <commentGenerator>
         <property name="suppressDate" value="true" />
         <property name="suppressAllComments" value="true" />
      </commentGenerator>

      <!-- 数据库链接URL、用户名、密码 -->
      <jdbcConnection driverClass="com.mysql.jdbc.Driver"
         connectionURL="jdbc:mysql://127.0.0.1:3306/sdf"
         userId="root"
         password="root">
      </jdbcConnection>

      <javaTypeResolver>
         <property name="forceBigDecimals" value="false" />
      </javaTypeResolver>


      <javaModelGenerator targetPackage="com.sdf.enterprise.business.model"
         targetProject="d:/workplace/idea/sdf-service/src/main/java">
         <property name="enableSubPackages" value="true" />
         <!-- 从数据库返回的值被清理前后的空格 -->
         <property name="trimStrings" value="true" />
      </javaModelGenerator>

      <!--对应的mapper.xml文件 -->
      <sqlMapGenerator targetPackage="mapper"
         targetProject="d:/workplace/idea/sdf-service/src/main/resources/">
         <property name="enableSubPackages" value="true" />
      </sqlMapGenerator>

      <!-- 对应的Mapper接口类文件 -->
      <javaClientGenerator type="XMLMAPPER"
         targetPackage="com.sdf.enterprise.business.mapper" targetProject="d:/workplace/idea/sdf-service/src/main/java">
         <property name="enableSubPackages" value="true" />
      </javaClientGenerator>

      <!-- 列出要生成代码的所有表,这里配置的是生成Example文件 -->
      <table tableName="patent"/>
      

   </context>
</generatorConfiguration> 

注意:在idea中配置文件需要填写项目的绝对路径。不像eclipse插件可以配置项目相对路径。

步骤三:Mybatis generator插件使用

Mybatis generator插件使用
如上图。点击Mybatis generator插件即可运行生成代码。












 
暂无评论