使用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 }