1. 程式人生 > >利用mybatis框架逆向工程生成實體類dao和mapper

利用mybatis框架逆向工程生成實體類dao和mapper

1、引入maven依賴

<build>
        <finalName>demo6</finalName>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version
>
1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>

2、在resourecs下新建GeneratorConfig.xm配置檔案,

GeneratorConfig.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>

    <!--資料庫驅動jar包的真實路徑 -->
<classPathEntry location="C:\repository_zl\repository\mysql\mysql-connector-java\5.1.39\mysql-connector-java-5.1.39.jar"/> <context id="context" targetRuntime="MyBatis3"> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <commentGenerator> <property name="suppressAllComments" value="false"/> <property name="suppressDate" value="true"/> </commentGenerator> <!--資料庫連線的資訊:驅動類、連線地址、使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/cloud_note" userId="root" password="123456"/> <!-- 預設false,把JDBC DECIMAL 和 NUMERIC 型別解析為 Integer,為 true時把JDBC DECIMAL 和 NUMERIC 型別解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--指定包名生成實體類 以及生成的地址 (可以自定義地址,如果路徑不存在會自動建立) --> <javaModelGenerator targetPackage="cn.wfc.bean" targetProject=".\src\main\java"> <!-- enableSubPackages:是否讓schema作為包的字尾 --> <property name="enableSubPackages" value="false"/> <!-- 從資料庫返回的值被清理前後的空格 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- !!!! Mapper XML Configurations !!!! --> <sqlMapGenerator targetPackage="cn.wfc.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- !!!! Mapper Interface Configurations !!!! --> <javaClientGenerator targetPackage="cn.wfc.dao" targetProject=".\src\main\java" type="XMLMAPPER"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 指定資料庫表 --> <table tableName="cn_notebook" domainObjectName="Notebook" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="cn_note" domainObjectName="Note" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <!--<table schema="db_wechat" tableName="t_commandcontent" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/> <table schema="db_wechat" tableName="t_automessage" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/> <table schema="db_wechat" tableName="t_command" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>--> </context> </generatorConfiguration>

3、新增一個run,設定引數為mybatis-generator:generate -e
這裡寫圖片描述
4、點選執行,即可生成對應的程式碼。我這裡取了兩個表。