關於mybatis生成外掛Generator配置檔案中自定義註釋

Tian_Shi_Hei_De發表於2017-06-26
1.編寫一個實現org.mybatis.generator.api.CommentGenerator介面的類。這個類有預設的實現,不會寫可以參考預設實現
org.mybatis.generator.internal.DefaultCommentGenerator

2.在maven外掛中新增依賴
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.3</version>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
        <dependency>
            <groupId>com.demo.aaa</groupId>
            <artifactId>bbb</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</plugin>

如上將驅動也新增到以來中,就可以不用在配置檔案中增加classPathEntry元素。
bbb是專案本身,需要先在本地安裝方可引用。


如上配置完成後在外掛的配置檔案中
<commentGenerator type="com.zhaolu.tian.MyComment">
    <property name="suppressAllComments" value="true"/>
    <property name="suppressDate" value="true"/>
</commentGenerator>
如此指定實現類即可。
最後提醒一下:有個坑,mysql的java驅動對於generator外掛高版本的不適用,還是用低版本的吧。5.1.37就挺好的不是嘛
最後貼一個完整的配置,好以後拿來就用。
<?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="generatorConfig.properties"/>

    <context id="mysql" defaultModelType="flat" targetRuntime="MyBatis3Simple">

        <property name="autoDelimitKeywords" value="true"/>
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <property name="javaFileEncoding" value="UTF-8"/>


        <commentGenerator type="com.m2m.mybatis.MyCommentGenerator">
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}"
                        password="${jdbc.password}"/>

        <javaModelGenerator targetPackage="${modelPackage}" targetProject="${targetPackage}">
            
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="${sqlPackage}" targetProject="${targetPackage}"/>
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="${clientPackage}" targetProject="${targetPackage}">
            <!--繼承通用mapper介面-->
            <property name="rootInterface" value="tk.mybatis.mapper.common.Mapper"/>
        </javaClientGenerator>

        
        <table tableName="%">
            <generatedKey column="id" sqlStatement="Mysql"/>
        </table>
    </context>
</generatorConfiguration>


相關文章