官方文檔:http://www.mybatis.org/generator/index.htmlhtml
①本身百度下載mybatis-generator-core,版本爲1.3.5。java
②若是使用Maven工程的話能夠加入如下依賴:mysql
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency>
<?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> <!--指定數據庫驅動URL--> <classPathEntry location = "D:\WORKSPACE\intelljIdea\StudyPlatform\target\Study\WEB-INF\lib\mysql-connector-java-5.0.8.jar" /> <!--context 裏的id用於惟一標識該Context,在生成錯誤信息時可能會使用到, targetRuntime表示當前使用的Mybatis版本,3.x的版本就是MyBatis3 --> <context id = "MysqlTables" targetRuntime = "MyBatis3"> <commentGenerator> <!--指定自動生成的文件是否忽略寫入註釋,true不寫註釋,false寫註釋--> <property name = "suppressAllComments" value = "false" /> <!--指定自動生成的文件是否忽略寫入生成日期,true不寫,false寫--> <property name = "suppressDate" value = "false" /> <!-- 指定自動生成的JavaBean若數據庫字段帶有註釋, 是否將對應註釋寫入到JavaBean的屬性上,true不寫,false不寫, 若suppressAllComments設定爲true,則此項設置無效 --> <property name = "addRemarkComments" value = "true" /> <!-- 指定日期註釋的格式,若suppressAllComments或者suppressDate 設定爲true,則此項設置無效 --> <property name = "dateFormat" value = "yyyy-MM-dd" /> </commentGenerator> <!--鏈接數據庫的信息--> <jdbcConnection driverClass = "com.mysql.jdbc.Driver" connectionURL = "jdbc:mysql://localhost:3306/ssm" userId = "root" password = "12345"> </jdbcConnection> <!--爲true時,數據庫裏的 DECIMAL 或者 NUMERIC類型將被映射成Java裏的 java.math.BigDecimal類型,若是爲false,將按照必定規則進行映射 --> <javaTypeResolver> <property name = "forceBigDecimals" value = "false" /> </javaTypeResolver> <!--指定生成的PO類放在哪一個路徑下--> <javaModelGenerator targetPackage = "at.flying.domain" targetProject = "D:\WORKSPACE\intelljIdea\StudyPlatform\src\main\java"> <!--是否讓schema做爲包的後綴--> <property name = "enableSubPackages" value = "false" /> <!--是否清理從數據庫返回的值得先後空格,若是數據庫裏的值是字符串的話比較有用--> <property name = "trimStrings" value = "true" /> </javaModelGenerator> <!--指定生成的映射文件放在哪一個路徑下--> <sqlMapGenerator targetPackage = "at.flying.mapper.xml" targetProject = "D:\WORKSPACE\intelljIdea\StudyPlatform\src\main\resources"> <!--是否讓schema做爲包的後綴--> <property name = "enableSubPackages" value = "false" /> </sqlMapGenerator> <!--指定生成的Mapper接口放在哪一個路徑下--> <javaClientGenerator type = "XMLMAPPER" targetPackage = "at.flying.interfaces" targetProject = "D:\WORKSPACE\intelljIdea\StudyPlatform\src\main\java"> <!--是否讓schema做爲包的後綴--> <property name = "enableSubPackages" value = "false" /> </javaClientGenerator> <!--指定須要被Mybatis逆向工程生成java代碼的表--> <table schema = "ssm" tableName = "user" domainObjectName = "User"> <!--這個值爲true時將會把表中的column名做爲User屬性的名字, 若是爲false,將會按照駝峯命名法自動爲User的屬性指定名字 --> <property name = "useActualColumnNames" value = "false" /> <!--指定column在數據庫的類型,在進行jdbcType到javaType映射時以這裏設置的爲準--> <columnOverride column = "uname" jdbcType = "VARCHAR" /> </table> <table tableName = "talker" /> <table schema = "ssm" tableName = "talk_msg" domainObjectName = "talkMsg"> <!--爲該column指定User中的屬性名--> <columnOverride column = "t_from" property = "from" /> <columnOverride column = "t_to" property = "to" /> <columnOverride column = "t_date" property = "date" /> </table> </context> </generatorConfiguration>
package at.flying; import org.junit.Test; 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; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class TestMybatis { @Test public void test1() { try { /*逆向工程執行過程當中產生的警告信息*/ List<String> warnings = new ArrayList<String>(); /*指定若是在指定路徑下有與逆向工程生成的同名文件是否覆蓋原有的同名文件*/ boolean overwrite = true; /*逆向工程配置文件的路徑*/ File configFile = new File("D:\\WORKSPACE\\intelljIdea\\StudyPlatform\\src\\main\\resources\\mybatis_generator.xml"); 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); /*打印出警告信息*/ for (String warining : warnings) { System.out.println(warining); } } catch (Exception e) { e.printStackTrace(); } } }
注:程序員
Mybatis逆向工程只能生成一些簡單的操做表的語句,因此該逆向工程只針對單表操做,而表之間的關係沒法映射出來,像關聯表的查詢等較複雜的SQL語句仍是須要程序員本身寫的。sql
關於Mybatis的逆向工程「配置文件」更加詳細的內容請參考官方文檔:http://www.mybatis.org/generator/configreference/xmlconfig.html數據庫