1. 程式人生 > >IntelliJ IDEA MyBatis-Plugin外掛的使用

IntelliJ IDEA MyBatis-Plugin外掛的使用

使用IntelliJ IDEA安裝MyBatis-Plugin外掛。

http://myoss.github.io/2016/MyBatis-Plugin-學習使用/

通過簡單的配置就可以生成所需的實體類、mapper對映檔案和介面。

在src/main/resources目錄下新建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>

    <!-- !!!! Driver Class Path !!!! -->
    <classPathEntry location=""/>

    <context id="context" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="false"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <!-- !!!! Database Configurations !!!! -->
        <jdbcConnection driverClass="" connectionURL="" userId="" password=""/>

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

        <!-- !!!! Model Configurations !!!! -->
        <javaModelGenerator targetPackage="" targetProject="THIS_CONFIGURATION_IS_NOT_REQUIRED">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- !!!! Mapper XML Configurations !!!! -->
        <sqlMapGenerator targetPackage="" targetProject="THIS_CONFIGURATION_IS_NOT_REQUIRED">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- !!!! Mapper Interface Configurations !!!! -->
        <javaClientGenerator targetPackage="" targetProject="THIS_CONFIGURATION_IS_NOT_REQUIRED" type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!-- !!!! Table Configurations !!!! -->
        <table tableName="" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false"/>
    </context>
</generatorConfiguration>
<properties url="file:///D:/N3verL4nd/Desktop/M/src/main/resources/jdbc.properties"/>
<!-- !!!! Database Configurations !!!! -->
<jdbcConnection driverClass="${jdbc.driverClassName}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>

可以如上載入資料庫配置檔案,貌似使用resource不能使用。

詳細配置說明:

http://www.jianshu.com/p/e09d2370b796

http://luoxianming.cn/2016/03/15/mybatis0/

作如下配置:

<?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>

    <!-- !!!! Driver Class Path !!!! -->
    <classPathEntry location="D:\Java\gradle\home\caches\modules-2\files-2.1\mysql\mysql-connector-java\5.1.40\ef2a2ceab1735eaaae0b5d1cccf574fb7c6e1c52\mysql-connector-java-5.1.40.jar "/>


    <context id="context" targetRuntime="MyBatis3">

        <property name="javaFileEncoding" value="UTF-8"/>

        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="false"/>
        </commentGenerator>

        <!-- !!!! Database Configurations !!!! -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="root" password="lgh123"/>

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

        <!-- !!!! Model Configurations !!!! -->
        <javaModelGenerator targetPackage="cn.bjut.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- !!!! Mapper XML Configurations !!!! -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- !!!! Mapper Interface Configurations !!!! -->
        <javaClientGenerator targetPackage="cn.bjut.mapper" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- !!!! Table Configurations !!!! -->
        <table tableName="persons" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               enableUpdateByExample="false" domainObjectName="Person"/>
    </context>
</generatorConfiguration>

domainObjectName:生成實體類、對映的名稱(此處就是Person.java、PersonMapper.java、PersonMapper.xml)

在配置該xml檔案前你得先建立相應的目錄結構。

在該xml檔案上右鍵:


就會自動生成實體類、mapper對映檔案和相應的介面了。

生成前後對應的目錄結構:


不知道是不是該外掛本身的bug還是破解導致的bug:

<sqlMapGenerator targetPackage="cn.bjut.mapper" targetProject="src/main/resources">
    <property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
如上配置,經測試,該外掛將mapper對映檔案生成在src/main/java路徑下,而不是在src/main/resources目錄下。

後來使用官方的生成程式碼則沒有問題,這也證實了該bug的存在。

package cn.bjut;


import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by N3verL4nd on 2017/7/6.
 */
public class MyBatisGene {
    public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<>();
        boolean overwrite = true;
        InputStream in = MyBatisGene.class.getClassLoader().getResourceAsStream("mybatis-generator-config.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(in);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}