使用mybatis生成逆向工程的詳細步驟,我個人感覺這個是最簡單的一個了,雖然網上有很多種的方法來生成逆向工程,可是這個方法最簡單。在這里我是使用maven搭建的環境,但是在正常的環境下也是一樣的。
步驟:
1、創建一個genreatorConfig.xml文件,這個文件的名字可以任意。我創建的時候是將它放在了src/main/resources下,這個文件的內容並不需要去記,只需要去網上找就可以了。我們要做的只是對配置文件當中的一些部分做修改,修改成自己的數據就可以了。
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
5
6 <generatorConfiguration>
7 <!--數據庫驅動,最好不要有中文字符,不然會找不到-->
8 <classPathEntry location="D:\MysqlJdbcconnector/mysql-connector-java-5.1.41-bin.jar" />
9
10
11 <context id="DB2Tables" targetRuntime="MyBatis3">
12
13 <commentGenerator>
14 <property name="suppressDate" value="true"/>
15 <property name="suppressAllComments" value="true"/>
16 </commentGenerator>
17 <!--數據庫鏈接地址賬號密碼-->
18 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/pet_hospital" userId="root" password="112233">
19 </jdbcConnection>
20 <javaTypeResolver>
21 <property name="forceBigDecimals" value="false"/>
22 </javaTypeResolver>
23 <!--生成Model類存放位置-->
24 <javaModelGenerator targetPackage="com.qianfeng.bean" targetProject="src/main/java">
25 <property name="enableSubPackages" value="true"/>
26 <property name="trimStrings" value="true"/>
27 </javaModelGenerator>
28 <!--生成映射文件存放位置-->
29 <sqlMapGenerator targetPackage="com.qianfeng.bean" targetProject="src/main/java">
30 <property name="enableSubPackages" value="true"/>
31 </sqlMapGenerator>
32 <!--生成DaoMapper類存放位置-->
33 <javaClientGenerator type="XMLMAPPER" targetPackage="com.qiafeng.dao" targetProject="src/main/java">
34
35 <property name="enableSubPackages" value="true"/>
36
37 </javaClientGenerator>
38 <!--生成對應表及類名,需要記住的一點是逆向工程無法生成關聯關系,只能生成單表操作-->
39 <table tableName="owners"
40 domainObjectName="Owners"
41 ></table>
42 <table tableName="pets"
43 domainObjectName="Pets"
44 ></table>
45 <table tableName="types"
46 domainObjectName="Types"
47 ></table>
48 <table tableName="employee"
49 domainObjectName="Employee"
50 ></table>
51 <table tableName="specialties"
52 domainObjectName="Specialties"
53 ></table>
54 <table tableName="vets"
55 domainObjectName="Vets"
56 ></table>
57 <table tableName="visits"
58 domainObjectName="Visits"
59 ></table>
60 <table tableName="vet_specialties"
61 domainObjectName="VetSpecialties"
62 ></table>
63
64 </context>
65 </generatorConfiguration>
2、導入相應的jar包,mybatis-generator-core這個包和mysql-connector-java這個包
3、創建一個測試類,然后運行下面代碼就可以了。
1 public static void generator() throws Exception{ 2 List<String> warnings = new ArrayList<String>(); 3 boolean overwrite = true; 4 //項目根路徑不要有中文,我的有中文,所以使用絕對路徑 5 File configFile = new File("src/main/resources/genreatorConfig.xml"); 6 ConfigurationParser cp = new ConfigurationParser(warnings); 7 Configuration config = cp.parseConfiguration(configFile); 8 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 9 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 10 myBatisGenerator.generate(null); 11 } 12 public static void main(String[] args) { 13 try { 14 generator(); 15 } catch (Exception e) { 16 // TODO Auto-generated catch block 17 e.printStackTrace(); 18 } 19 }