mybatis-generator自动生成代码插件使用详解

  mybatis-generator是一款在使用mybatis框架时,自动生成model,dao和mapper的工具,很大程度上减少了业务开发人员的手动编码时间,今天自己研究了一下,也分享一下使用心得供大家简单使用。

  本人使用的是maven构建,首先需要在pom.xml文件添加mybatis-generator依赖包以及插件,配置如下:

  dependencies中添加:

<dependency>
    <groupId>org.mybatis.generatorgroupId>
    <artifactId>mybatis-generator-coreartifactId>
    <version>1.3.2version>
dependency>

  在build的plugins中添加:

<plugin>
    <groupId>org.mybatis.generatorgroupId>
    <artifactId>mybatis-generator-maven-pluginartifactId>
    <version>1.3.2version>
    <configuration>
       
      <configurationFile>src/main/resources/generatorConfig.xmlconfigurationFile>
        <verbose>trueverbose>
        <overwrite>trueoverwrite>
    configuration>
plugin>

  上面就是pom.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>
    <classPathEntry
            location="/Users/yehaixiao/Maven/mysql/mysql-connector-java/5.1.30/mysql-connector-java.jar"/>
    <context id="my" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        commentGenerator>
      
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/test" userId="root"
                        password="******"/>

     
        <javaModelGenerator targetPackage="com.ssmgen.demo.model"
                            targetProject="/Users/yehaixiao/asiainfo/ssm-demo/src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        javaModelGenerator>

     
        <sqlMapGenerator targetPackage="com.ssmgen.demo.mapper"
                         targetProject="/Users/yehaixiao/asiainfo/ssm-demo/src/main/java">
            <property name="enableSubPackages" value="true"/>
        sqlMapGenerator>

        
        <javaClientGenerator targetPackage="com.ssmgen.demo.mapper"
                             targetProject="/Users/yehaixiao/asiainfo/ssm-demo/src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        javaClientGenerator>
     
        <table tableName="TEST1" domainObjectName="Test"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        table>

    context>
generatorConfiguration>

 

  到此为止,所有的配置已完毕,如果在ecplise中使用,则右击工程,maven build,添加命令mybatis-generator:generate,代码生成完毕!

  

你可能感兴趣的:(mybatis-generator自动生成代码插件使用详解)