標籤: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介面