1. 程式人生 > >使用MyBatis Generator代碼生成器的簡單模式

使用MyBatis Generator代碼生成器的簡單模式

功能 mstr lease 不支持 需要 imp github style void

在動態web項目的lib目錄下放入mybatis-3.2.2jar、mysql-connector-java-5.1.25-bin.jar、log4j-1.2.17.jar
還有生成器的jar包mybatis-generator-core-1.3.2.jar(1.3.1版本不支持MyBatis3Simple格式),添加到build path,
在src目錄下創建cn.java17.pojo和cn.java17.dao兩個目錄,
還有添加一個log4j.properties文件,
之後再在項目路徑下創建mbg.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> <!-- 指定數據庫連接的jar包 --> <!-- <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
--> <context id="MySQLTables" targetRuntime="MyBatis3Simple" > <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <commentGenerator> <property value="true" name="suppressAllComments"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/smbms" userId="root" password="123456"> </jdbcConnection> <!-- 要不要強制轉化為大數據類型 --> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 指定生成javabean的包 --> <javaModelGenerator targetPackage="cn.java17.pojo" targetProject=".\src"> <!-- 子包也可以 --> <property name="enableSubPackages" value="true" /> <!-- 生成去除掉前後空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 指定生成映射器xml文件的位置 --> <sqlMapGenerator targetPackage="cn.java17.dao" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 指定生成映射器java類文件的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.java17.dao" targetProject=".\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator>   <!-- 指定要生成的表名 --> <table tableName="smbms_address" domainObjectName="Address" > </table> <table tableName="smbms_bill" domainObjectName="Bill" > </table> <table tableName="smbms_provider" domainObjectName="Provider" > </table> <table tableName="smbms_role" domainObjectName="Role" > </table> <table tableName="smbms_user" domainObjectName="User" > </table> </context> </generatorConfiguration>
創建用於執行生成MyBatis相關文件帶main函數的class如下:
 1 import java.io.File;
 2 import java.util.ArrayList;
 3 import java.util.List;
 4 import org.mybatis.generator.api.MyBatisGenerator;
 5 import org.mybatis.generator.config.Configuration;
 6 import org.mybatis.generator.config.xml.ConfigurationParser;
 7 import org.mybatis.generator.internal.DefaultShellCallback;
 8 
 9 public class TestMBG {
10     public static void main(String[] args) throws Exception {
11         List<String> warnings = new ArrayList<String>();
12         boolean overwrite = true;
13         File configFile = new File("mbg.xml");
14         ConfigurationParser cp = new ConfigurationParser(warnings);
15         Configuration config = cp.parseConfiguration(configFile);
16         DefaultShellCallback callback = new DefaultShellCallback(overwrite);
17         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
18         myBatisGenerator.generate(null);
19     }
20 }

執行以上代碼即可生成相應的MyBatis映射文件和pojo文件。其中overwrite如果是true代表,後面再次生成的文件是覆蓋前面生成的同名文件,

如果生成的文件修改過,要保留修改痕跡,請慎重,把overwrite改成false。

其中context元素下的targetRuntime="MyBatis3Simple",指生成的pojo文件不生成相應的example示例文件,而且生成的映射接口文件

不產生復雜的增刪改查函數和對應pojo中example文件的示例映射文件,只生成一些簡單的增刪改查文件,

而javaModelGenerator、sqlMapGenerator、javaClientGenerator等元素的targetPackage屬性是用於指定

文件生成到哪個包路徑下,而targetProject屬性是用於設置這些包在哪個Source Folder下,寫.\src指的是本項目的src目錄。

生成器生成的代碼不一定涵蓋你需要的所有功能,大家可以自己在這些基礎上自行擴展。

生成器的jar文件來源於https://github.com/mybatis/generator/releases

 
 

使用MyBatis Generator代碼生成器的簡單模式