mybatis-generator maven插件生成实体及dao代码

利用mybatis的maven插件反向生成数据库表与实体及dao到项目工程中
配置炒鸡简单,见下步骤

maven插件pox.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
33
34
<properties>
<mysql.version>5.1.18</mysql.version>
</properties>
<!--mysql依赖包-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<!-- mybatis maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugins>
<build>

generator配置文件

src/main/resources下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
33
34
35
36
37
38
39
40
41
<?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">
<!-- 最后一行table元素中修改自己的要生成的表名及实体名称 -->
<generatorConfiguration>
<context id="call" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 关闭自动生成的注释 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库连接信息 修改成你的配置-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/yourDB"
userId="root" password="123456">
</jdbcConnection>
<!-- model实体类 targetPackage是你工程的放实体包名 -->
<javaModelGenerator targetPackage="com.bm.insurance.cloud.sale.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- sql xml -->
<sqlMapGenerator targetPackage="mybatis" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- dao targetPackage是你工程的放dao包名-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.bm.insurance.cloud.sale.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 数据库表名tableName, 生成的实体名 -->
<table tableName="red_package_activity" domainObjectName="RedPackageActivity"
enableDeleteByExample="false" enableDeleteByPrimaryKey="false"></table>
</context>
</generatorConfiguration>

在eclipse中应用

  • 0.用idea的,你懂的 ^_^
  • 1.选择pom.xml文件,击右键先择Run AS——Maven Build…
  • 2.在Goals框中输入命令:mybatis-generator:generate
  • 3.Run就好了,在工程上刷新