mybatis generator逆向工程
阿新 • • 發佈:2021-06-20
根據mysql表逆向生成mapper、dao、model。
新增mybatis generator依賴
<!--mybatis-generator--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6</version> </dependency>
在resources目錄下新增配置檔案(加了黃色背景處需要修改)
<?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> <!-- context 是逆向工程的主要配置資訊 --> <!-- id:起個名字 --> <!-- targetRuntime:設定生成的檔案適用於那個 mybatis 版本 --> <context id="default" targetRuntime="MyBatis3"> <!--optional,指在建立class時,對註釋進行控制--> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="false"/> </commentGenerator> <!--jdbc的資料庫連線 wg_insert 為資料庫名字--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/study?useUnicode=true&characeterEncoding=utf-8&serverTimezone=UTC" userId="root" password="root"></jdbcConnection> <!--非必須,型別處理器,在資料庫型別和java型別之間的轉換控制--> <javaTypeResolver> <!-- 預設情況下資料庫中的 decimal,bigInt 在 Java 對應是 sql 下的 BigDecimal 類 --> <!-- 不是 double 和 long 型別 --> <!-- 使用常用的基本型別代替 sql 包下的引用型別 --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- targetPackage:生成的實體類所在的包 --> <!-- targetProject:生成的實體類所在的硬碟位置 --> <javaModelGenerator targetPackage="com.example.permissions.model" targetProject="src/main/java"> <!-- 是否允許子包 --> <property name="enableSubPackages" value="false"/> <!-- 是否對modal新增建構函式 --> <property name="constructorBased" value="true"/> <!-- 是否清理從資料庫中查詢出的字串左右兩邊的空白字元 --> <property name="trimStrings" value="true"/> <!-- 建立modal物件是否不可改變 即生成的modal物件不會有setter方法,只有構造方法 --> <property name="immutable" value="false"/> </javaModelGenerator> <!-- targetPackage 和 targetProject:生成的 mapper 檔案的包和位置 --> <sqlMapGenerator targetPackage="com.example.permissions.sqlmap" targetProject="src/main/java"> <!-- 針對資料庫的一個配置,是否把 schema 作為字包名 --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- targetPackage 和 targetProject:生成的 interface 檔案的包和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.permissions.dao" targetProject="src/main/java"> <!-- 針對 oracle 資料庫的一個配置,是否把 schema 作為字包名 --> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- tableName是資料庫中的表名,domainObjectName是生成的JAVA模型名,後面的引數不用改,要生成更多的表就在下面繼續加table標籤 --> <table tableName="sys_config" domainObjectName="SysConfig" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <!-- tableName是資料庫中的表名,domainObjectName是生成的JAVA模型名,後面的引數不用改,要生成更多的表就在下面繼續加table標籤 --> <table tableName="sys_dept" domainObjectName="SysDept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="sys_dict" domainObjectName="SysDict" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> .....(有多少張表需要逆向生成對應檔案新增多少個<table></table>) </context> </generatorConfiguration>
隨便找一個配置類新增main方法
public static void main(String[] args) { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("src/main/resources/generatorConfig.xml"); //mybatis generator逆向工程配置檔案位置 ConfigurationParser cp = new ConfigurationParser(warnings); try { org.mybatis.generator.config.Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
執行該main方法