mybatis入門篇(五):代碼生成器(MyBatis Generator)

這篇文章只是按照本身的須要去配置代碼生成器,未對全部配置進行講解,須要瞭解具體詳情的,請到官網查閱文檔。點擊傳送java

一、首先引入相關的依賴,爲了操做方便,也引入代碼生成器的插件mysql

<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
</plugin>

二、在reources中添加MyBatis Generator(MBG)的配置:generatorConfig.xmlsql

<?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>
    <!--配置mysql驅動的路徑,不然會提示找不到驅動-->
    <classPathEntry location="/usr/local/apache-maven-3.6.0/repository/mysql/mysql-connector-java/8.0.15/mysql-connector-java-8.0.15.jar"/>

    <!--主要配置,必需的配置-->
    <context id="MySqlCOntext" defaultModelType="flat" targetRuntime="MyBatis3Simple">
        <!--註釋相關配置,此處選擇不生成註釋-->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--配置數據庫鏈接相關配置-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/forest?useSSL=false"
                        userId="root" password="root">
            <!--此項必填,不然生成實體時掃描的數據庫不單單是連接中的數據庫,還會包括相似於mysql、sys這樣的系統數據庫-->
            <property name="nullCatalogMeansCurrent" value="true" />
        </jdbcConnection>

        <!--配置實體類的包和物理路徑-->
        <javaModelGenerator targetPackage="com.forest.owl.entity"
                            targetProject="/var/project/owl/src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="false"/>
        </javaModelGenerator>

        <!--配置mapper.xml文件的包和物理路徑-->
        <sqlMapGenerator targetPackage="com.forest.owl.mapper"
                         targetProject="/var/project/owl/src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!--配置須要生成的表,若是是全部表,則選擇tableName="%"-->
        <table tableName="user"/>
        <table tableName="role"/>
        <table tableName="privilege"/>
        <table tableName="user_role"/>
        <table tableName="role_privilege"/>
    </context>
</generatorConfiguration>

三、配置完成後運行MyBatis Generator的maven插件便可。數據庫

相關文章
相關標籤/搜索