mybatis- generator自動生成程式碼
阿新 • • 發佈:2020-07-13
一、mybatis- generator簡介
mybatis- generator是mybatis的一款外掛,主要用於根據資料庫錶快速生成對應的domain(即javabean)、mapper介面以及對應的mapper.xml對映檔案,非常的方便,可以減少手動編寫javabean、mapper介面以及mapper對映檔案工作量,非常的高效。
二、mybatis- generator使用
1、config.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> <!--連線mysql的jar包--> <classPathEntry location="D:\ProgramRelate\ProgramCode\mybatis-generator-usage\lib\mysql-connector-java-5.1.6-bin.jar" /> <context id="MySQLTables" targetRuntime="MyBatis3"> <!--mybatis的一些外掛--> <!--<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"> <property name="searchString" value="Example$" /> <property name="replaceString" value="Criteria" /> </plugin>--> <!-- <plugin type="com.lz.cts.plugin.MySQLPaginationPlugin"></plugin>--> <!-- <plugin type="com.lz.cts.plugin.RenameExampleMethodPlugin"></plugin>--> <!-- <plugin type="com.lz.cts.plugin.ModelFieldCustomizePlugin"></plugin>--> <!-- 生成的domain實現了序列化--> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin> <!-- 生成toString()方法--> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin> <!-- 是否去除自動生成的註釋,true:是,false:否 --> <commentGenerator> <property name="suppressDate" value="true" /> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--資料庫連線的資訊:驅動類、連線地址、使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://rm-wz92h1ljvevri4t2y.mysql.rds.aliyuncs.com/ncr" userId="root" password="Wen18948279186"> <!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@10.75.10.17:1521:ORCL" userId="ctsapp" password="ctsapp"> --> <property name="remarks" value="true" /> </jdbcConnection> <!-- 預設false,把JDBC DECIMAL 和 NUMERIC 型別解析為 Integer, 為 true時把JDBC DECIMAL 和NUMERIC 型別解析為java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 指定javaBean生成的位置 targetPackage:生成的類要放的包,真實的包受enableSubPackages屬性控制; targetProject:目標專案,指定一個存在的目錄下,生成的內容會放到指定目錄中,如果目錄不存在,MBG不會自動建目錄 --> <javaModelGenerator targetPackage="com.lz.cts.domain" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <!-- <property name="trimStrings" value="true" /> --> <property name="rootClass" value="com.lz.cts.support.BaseEntity" /> </javaModelGenerator> <!-- 指定mapper對映檔案生成的位置,mapper包會自己給你建立 targetPackage、targetProject同javaModelGenerator中作用一樣--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/java/com/lz/cts"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 指定mapper介面生成的位置 targetPackage、targetProject同javaModelGenerator中作用一樣 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lz.cts.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 指定資料庫表 domainObjectName:生成的domain類的名字,當表名和domain類的名字有差異時一定要設定,如果不設定,直接使用表名作為domain類的名字; 可以設定為somepck.domainName,那麼會自動把domainName類再放到somepck包裡面;--> <table tableName="cls_order_info" /> <table tableName="sys_menu" /> </context> </generatorConfiguration>
2、執行程式生成對應的各層程式碼
package com.lz.cts; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.api.VerboseProgressCallback; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; public class MybatisGeneratorUtil { public static void main(String[] args) { try { deletefile("E:/jaosn"); System.out.println("start generator ..."); List<String> warnings = new ArrayList<>(); boolean overwrite = true; File configFile = new File(MybatisGeneratorUtil.class.getResource("../../../config_mysql.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(new VerboseProgressCallback()); System.out.println("end generator!"); System.exit(0); } catch (Exception e) { e.printStackTrace(); } } /** * * 刪除某個資料夾下的所有資料夾和檔案 * @param delpath String * @throws * FileNotFoundException * @throws IOException * @return boolean */ public static boolean deletefile(String delpath) throws FileNotFoundException, IOException { try { File file = new File(delpath); if (!file.isDirectory()) { file.delete(); } else if (file.isDirectory()) { String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File delfile = new File(delpath + "\\" + filelist[i]); if (!delfile.isDirectory()) { delfile.delete(); } else if (delfile.isDirectory()) { deletefile(delpath + "\\" + filelist[i]); } } //file.delete(); } } catch (FileNotFoundException e) { System.out.println("deletefile() Exception:" + e.getMessage()); } return true; } }
三、推薦mybatis好用的三款外掛
1、mybatis- generator 自動生成對應的javabean、mapper介面以及mapper對映檔案。
2、mybatisplugin 可以快速在mapper介面與mpper對映檔案之間定位。
3、mybatis-pagehelper 分頁外掛,這個沒用過,但是最好不用。
這三大外掛的使用和安裝參見博文: https://blog.csdn.net/Scor1005/article/details/94454377
參考博文:
(1) https://blog.csdn.net/AOBO516/article/details/90245203