一、首先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>
<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"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<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"
connectionurl= "jdbc:mysql://127.0.0.1:3306/crm" userid= "root"
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
五、执行结果如下:
总结
以上所述是小编给大家介绍的ssm框架通过mybatis-generator自动生成代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!