1. 程式人生 > 實用技巧 >圖論-最短路

圖論-最短路

1、Mybatis 逆向工程是什麼?

MyBatis Generator

簡稱mbg,是一個專門為Mybatis框架使用者定製的程式碼生成器,可以快速的根據表生成對應的

對映檔案、介面和bean類。支援基本的增刪改查,以及QBC(Query By Criteria)風格的條件查詢。

2、如何使用

首先說一下,以下配置必須放在一個完整的mybatis專案下,別犯傻哈!

(即引入了mybatis相關的jar包和 具備mybatis全域性配置檔案)

1)匯入mybatis逆向工程相應的jar包

2)編寫全域性配置檔案mbg.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> <!-- targetRuntime="MyBatis3Simple":生成簡單版的CRUD MyBatis3:豪華版 -->
<context id="DB2Tables" targetRuntime="MyBatis3"> <!-- jdbcConnection:指定如何連線到目標資料庫 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true" userId
="root" password="houchen"> </jdbcConnection> <!-- --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- javaModelGenerator:指定javaBean的生成策略 targetPackage="test.model":目標包名 targetProject="\MBGTestProject\src":目標工程 --> <javaModelGenerator targetPackage="com.atguigu.mybatis.bean" targetProject=".\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- sqlMapGenerator:sql對映生成策略: --> <sqlMapGenerator targetPackage="com.atguigu.mybatis.dao" targetProject=".\conf"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- javaClientGenerator:指定mapper介面所在的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 指定要逆向分析哪些表:根據表要建立javaBean --> <table tableName="tbl_dept" domainObjectName="Department"></table> <table tableName="tbl_employee" domainObjectName="Employee"></table> </context> </generatorConfiguration>
<?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>

    <!--
        targetRuntime="MyBatis3Simple":生成簡單版的CRUD
        MyBatis3:豪華版

     -->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- jdbcConnection:指定如何連線到目標資料庫 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true"
                        userId="root"
                        password="houchen">
        </jdbcConnection>

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

        <!-- javaModelGenerator:指定javaBean的生成策略
        targetPackage="test.model":目標包名
        targetProject="\MBGTestProject\src":目標工程
        -->
        <javaModelGenerator targetPackage="com.atguigu.mybatis.bean"
                            targetProject=".\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- sqlMapGenerator:sql對映生成策略: -->
        <sqlMapGenerator targetPackage="com.atguigu.mybatis.dao"
                         targetProject=".\conf">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- javaClientGenerator:指定mapper介面所在的位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao"
                             targetProject=".\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 指定要逆向分析哪些表:根據表要建立javaBean -->
        <table tableName="tbl_dept" domainObjectName="Department"></table>
        <table tableName="tbl_employee" domainObjectName="Employee"></table>
    </context>
</generatorConfiguration>

3) 測試逆向工程

@Test
public void testMybatisGenerate() throws Exception {
    List<String> warnings = new ArrayList<String>();
    boolean overwrite = true;
    File configFile = new File("mbg.xml");
    ConfigurationParser cp = new ConfigurationParser(warnings);
    Configuration config = cp.parseConfiguration(configFile);
    DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
            callback, warnings);
    myBatisGenerator.generate(null);
}

確實生成了相應的實體、介面和 mapper: