一、首先eclipse配置好maven环境,并且创建好一个SSM框架的工程
这个操作在之前有说,自己去研究。
二、在pom.xml中添加plugin
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
|
<build>
<finalName>ssm_web</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>
1.3
.
2
</version>
<configuration>
<configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
<verbose>
true
</verbose>
<overwrite>
true
</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>
1.3
.
2
</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>
5.1
.
40
</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
|
注意:
1、pom中添加插件要在<build></build>标签里面,并且在这里指定数据库驱动,那么在下一步配置generatorConfig.xml的时候就不用在指定数据库驱动的本地路径;
2、src/main/resources/mybatis-generator/generatorConfig.xml指定的是generatorConfig.xml配置文件的路径,大家可以根据自己的实际情况调整;
三、generatorConfig.xml配置文件
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
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE generatorConfiguration
PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
<generatorConfiguration>
<!-- <classPathEntry location=
"E:/maven/repository/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jar"
/> -->
<context id=
"my"
targetRuntime=
"MyBatis3"
>
<commentGenerator>
<property name=
"suppressDate"
value=
"false"
/>
<property name=
"suppressAllComments"
value=
"true"
/>
</commentGenerator>
<jdbcConnection driverClass=
"com.mysql.jdbc.Driver"
password=
"root"
/>
<javaModelGenerator targetPackage=
"com.xdw.model"
targetProject=
"F:/javawebworkspace/ssm_web/src/main/java"
>
<property name=
"enableSubPackages"
value=
"true"
/>
<property name=
"trimStrings"
value=
"true"
/>
</javaModelGenerator>
<sqlMapGenerator targetPackage=
"com.xdw.mapping"
targetProject=
"F:/javawebworkspace/ssm_web/src/main/java"
>
<property name=
"enableSubPackages"
value=
"true"
/>
</sqlMapGenerator>
<javaClientGenerator targetPackage=
"com.xdw.dao"
targetProject=
"F:/javawebworkspace/ssm_web/src/main/java"
type=
"XMLMAPPER"
>
<property name=
"enableSubPackages"
value=
"true"
/>
</javaClientGenerator>
<table tableName=
"house_type"
domainObjectName=
"HouseType"
>
<!-- <property name=
"useActualColumnNames"
value=
"true"
/> -->
</table>
</context>
</generatorConfiguration>
|
配置讲解:
1、classPathEntry location在这里可以不用配置,因为之前pom中已经配置了数据库驱动;
2、jdbcConnection按照自己的数据库配置相应的驱动类,URL,用户名和密码;
3、javaModelGenerator,sqlMapGenerator,javaClientGenerator配置相应要生成的pojo类,DAO类和mapper对应的xml文件,targetPackage属性指定包名,targetProject指定自己工程的路径,
4、table标签配置数据库表和实体类的映射,tableName属性指定表名,domainObjectName指定生成的pojo类名;<property name="useActualColumnNames" value="true"/>
如果配置的话,那么生成的实体类的属性名称就和数据库
表的每个字段的名称一样,如果不配置,会将数据表里面的字段名称通过_连接的字段自动生成经典的驼峰表示法,比如我这里有个字段名为type_id,生成的属性名称为typeId;
建议大家在数据库建表的时候采用_将单词分隔;
四、创建好之后点击maven build
弹出
在goals中填入mybatis-generator:generate
,然后点击Run
也可以用maven命令行输入mvn mybatis-generator:generate
五、执行结果如下: