MyBatis自動產生實體類

來源:互聯網
上載者:User

MyBatis+MySQL產生實體類

需要的工具jar包

mybatis-generator-core-1.3.2.jar

mysql-connector-java-5.0.4.jar

第一步:編寫一個MybatisGeneratorUtil類

package com.test.utils;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;    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();          }      }        }
第二步:編寫一個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:\mysql-connector-java-5.0.4.jar" />      <context id="Mysql2Tables" targetRuntime="MyBatis3">      <jdbcConnection driverClass="com.mysql.jdbc.Driver"          connectionURL="jdbc:mysql://localhost:3306/datebase"          userId="root"          password="root">      </jdbcConnection>        <javaTypeResolver >        <property name="forceBigDecimals" value="false" />      </javaTypeResolver>        <javaModelGenerator targetPackage="com.test.model" targetProject="src">        <property name="enableSubPackages" value="true" />        <property name="trimStrings" value="true" />      </javaModelGenerator>        <sqlMapGenerator targetPackage="com.test.mapper"  targetProject="src">        <property name="enableSubPackages" value="true" />      </sqlMapGenerator>        <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao"  targetProject="src">        <property name="enableSubPackages" value="true" />      </javaClientGenerator>      <table schema="test" tableName="t_student" domainObjectName="Tstudent" enableCountByExample="false" enableUpdateByExample="false"             enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">      </table>      <table schema="test" tableName="t_class" domainObjectName="Tclass" enableCountByExample="false" enableUpdateByExample="false"         enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">     </table>     <table schema="test" tableName="t_stu_class" domainObjectName="Tstclass" enableCountByExample="false" enableUpdateByExample="false"         enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">     </table>  </context>  </generatorConfiguration>
其中<table>中的tableName代表資料庫中的表名、domainObjectName表示你要產生的實體類的名字;

<classPathEntry>中的location代表你的mysql-connector-java-5.0.4.jar地址;

targetPackage便是你要產生的檔案的路徑.

產生之後就是這樣子


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.