命令+mybatis-generator插件自動生成Mapper映射文件

自己寫各種 *Mapper.xml和 *Mapper.Java,注意各種sql語句中的 id 是否匹配,xml中的namespace是否正確,很麻煩有木有?今天博客內容就是高大上的自動構建~

  1. 需要的工具包、文件 
    jar包自己上網找資源就好了

  2. 下面來介紹一下generator.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="D:\software\lib\mysql-connector-java-5.1.21.jar" /> -->
    <classPathEntry location="E:\mysql-connector-java-5.1.29-bin.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 數據庫鏈接URL、用戶名、密碼  jdbc\:mysql\://localhost\:3306/cinema-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/cinema" userId="root" password="a">
        <!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa"> -->
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="com.rindy.cinema.entity" targetProject="E:\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="E:\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.rindy.cinema.mapper" targetProject="E:\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就可以)這裏是生成兩張表的映射文件,多表可自行增加,確保你的數據庫中已經建好表,並且表名沒有寫錯~ -->
        <table tableName="filminfo" domainObjectName="FILMINFO" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="filmtype" domainObjectName="FILMTYPE" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />

    </context></generatorConfiguration>

3.上圖中的 src是爲生成的映射文件新建的空目錄,構建成功,該目錄下將會有你需要的文件。

4.自動生成Mapper映射文件命令:

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite11

5.下面來看一下我的運行結果: 
這裏寫圖片描述

6.可能有的小夥伴會因爲編碼格式而引發一些錯誤,我們來看一下下面這種情況怎麼解決 
這裏寫圖片描述 
改一下編碼格式就可以了 
這裏寫圖片描述 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章