使用mybatis生成逆向工程的详细步骤,我个人感觉这个是最简单的一个了,虽然网上有很多种的方法来生成逆向工程,可是这个方法最简单。在这里我是使用maven搭建的环境,但是在正常的环境下也是一样的。
步骤:
1、创建一个genreatorConfig.xml文件,这个文件的名字可以任意。我创建的时候是将它放在了src/main/resources下,这个文件的内容并不需要去记,只需要去网上找就可以了。我们要做的只是对配置文件当中的一些部分做修改,修改成自己的数据就可以了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<? 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 >
<!--数据库驱动,最好不要有中文字符,不然会找不到-->
< classPathEntry location = "D:\MysqlJdbcconnector/mysql-connector-java-5.1.41-bin.jar" />
< context id = "DB2Tables" targetRuntime = "MyBatis3" >
< commentGenerator >
< property name = "suppressDate" value = "true" />
< property name = "suppressAllComments" value = "true" />
</ commentGenerator >
<!--数据库链接地址账号密码-->
< jdbcConnection driverClass = "com.mysql.jdbc.Driver" connectionURL = "jdbc:mysql://localhost/pet_hospital" userId = "root" password = "112233" >
</ jdbcConnection >
< javaTypeResolver >
< property name = "forceBigDecimals" value = "false" />
</ javaTypeResolver >
<!--生成Model类存放位置-->
< javaModelGenerator targetPackage = "com.qianfeng.bean" targetProject = "src/main/java" >
< property name = "enableSubPackages" value = "true" />
< property name = "trimStrings" value = "true" />
</ javaModelGenerator >
<!--生成映射文件存放位置-->
< sqlMapGenerator targetPackage = "com.qianfeng.bean" targetProject = "src/main/java" >
< property name = "enableSubPackages" value = "true" />
</ sqlMapGenerator >
<!--生成DaoMapper类存放位置-->
< javaClientGenerator type = "XMLMAPPER" targetPackage = "com.qiafeng.dao" targetProject = "src/main/java" >
< property name = "enableSubPackages" value = "true" />
</ javaClientGenerator >
<!--生成对应表及类名,需要记住的一点是逆向工程无法生成关联关系,只能生成单表操作-->
< table tableName = "owners"
domainObjectName = "Owners"
></ table >
< table tableName = "pets"
domainObjectName = "Pets"
></ table >
< table tableName = "types"
domainObjectName = "Types"
></ table >
< table tableName = "employee"
domainObjectName = "Employee"
></ table >
< table tableName = "specialties"
domainObjectName = "Specialties"
></ table >
< table tableName = "vets"
domainObjectName = "Vets"
></ table >
< table tableName = "visits"
domainObjectName = "Visits"
></ table >
< table tableName = "vet_specialties"
domainObjectName = "VetSpecialties"
></ table >
</ context >
</ generatorConfiguration >
|
2、导入相应的jar包,mybatis-generator-core这个包和mysql-connector-java这个包
3、创建一个测试类,然后运行下面代码就可以了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public static void generator() throws Exception{
List< String > warnings = new ArrayList< String >();
boolean overwrite = true;
//项目根路径不要有中文,我的有中文,所以使用绝对路径
File configFile = new File("src/main/resources/genreatorConfig.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);
}
public static void main(String[] args) {
try {
generator();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
|
以上这篇基于mybatis逆向工程的使用步骤详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/ganjunnan/p/7804429.html