mybatis怎麼自動產生實體類,Mapper設定檔和Dao介面

來源:互聯網
上載者:User

標籤:connector   mst   img   warning   out   import   back   png   hibernate   

 

1.首先準備好jar包

https://github.com/mybatis/generator/releases 下載MyBatis Generator  

下載壓縮包後,開啟可以看到lib目錄下有我們需要的jar包,添加到項目引用

 

 

 

2.和Hibernate逆向產生一樣,這裡也需要一個設定檔:

 

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>    <!-- 資料庫驅動包的位置,我這裡是放到D盤的 -->        <classPathEntry location="D:\mysql-connector-java-5.1.0-bin.jar" />              <context id="Mysql2Tables" targetRuntime="MyBatis3">      <!-- 資料庫驅動,串連url,使用者名稱,密碼 -->      <jdbcConnection driverClass="com.mysql.jdbc.Driver"              connectionURL="jdbc:mysql://192.168.144.100:3306/networkdept"              userId="root"              password="123456">          </jdbcConnection>                <javaTypeResolver >            <property name="forceBigDecimals" value="false" />          </javaTypeResolver>                <!-- 產生實體類的包名和位置 -->       <javaModelGenerator targetPackage="cn.networkdepartment.pojo" targetProject="src">            <property name="enableSubPackages" value="true" />            <property name="trimStrings" value="true" />          </javaModelGenerator>                  <!-- 產生dao介面的包名和位置 -->    <sqlMapGenerator targetPackage="cn.networkdepartment.dao"  targetProject="src">            <property name="enableSubPackages" value="true" />          </sqlMapGenerator>                <!-- 產生的對應檔包名和位置 -->     <javaClientGenerator type="XMLMAPPER" targetPackage="cn.networkdepartment.dao"  targetProject="src">            <property name="enableSubPackages" value="true" />          </javaClientGenerator>          <!-- 要產生的那些表(更改tableName 和domainObjectName 就可以了) -->        <table schema="test" tableName="nd_class" domainObjectName="NClass" enableCountByExample="false" enableUpdateByExample="false"                 enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">          </table>              <table schema="test" tableName="nd_integral" domainObjectName="Integral" enableCountByExample="false" enableUpdateByExample="false"             enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">         </table>            <table schema="test" tableName="nd_log" domainObjectName="Nlog" enableCountByExample="false" enableUpdateByExample="false"             enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">         </table>       <table schema="test" tableName="nd_maintain" domainObjectName="Maintain" enableCountByExample="false" enableUpdateByExample="false"             enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">         </table>         <table schema="test" tableName="nd_member" domainObjectName="Member" enableCountByExample="false" enableUpdateByExample="false"             enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">         </table>       <table schema="test" tableName="nd_post" domainObjectName="Npost" enableCountByExample="false" enableUpdateByExample="false"             enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">         </table>       <table schema="test" tableName="nd_systemset" domainObjectName="Systemset" enableCountByExample="false" enableUpdateByExample="false"             enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">         </table>     </context>      </generatorConfiguration>  

 

 3.建立一個類通過main方法產生代碼,運行這個類

package test;  import java.io.File;    import java.io.IOException;    import java.sql.SQLException;    import java.util.ArrayList;    import java.util.List;        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;    /** * 建立測試類別,載入設定檔自動產生dao,Mapper檔案 * @author Bryce * */public class MybatisGeneratorUtil {            public static void main(String[] args) {            try {                System.out.println("start generator ...");                List<String> warnings = new ArrayList<String>();                boolean overwrite = true;                File configFile = new File(MybatisGeneratorUtil.class.getResource("/generator.xml").getFile());                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);                System.out.println("end generator!");            } catch (IOException e) {                e.printStackTrace();            } catch (XMLParserException e) {                e.printStackTrace();            } catch (InvalidConfigurationException e) {                e.printStackTrace();            } catch (SQLException e) {                e.printStackTrace();            } catch (InterruptedException e) {                e.printStackTrace();            }        }            }  

然後就可以看到實體類和dao都產生了

 

mybatis怎麼自動產生實體類,Mapper設定檔和Dao介面

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.