使用MyBatis Generartor自动生成代码
介绍
MyBatis Generator(http://www.mybatis.org/generator/index.html) 是MyBatis的一款代码生成器,它可以针对数据库表自动生成DTO、Mapper接口及相应的xml文件。
操作步骤
1.在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<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 指定MyBatis Generartor配置文件 -->
<configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
2.在pom.xml
中添加依赖:
1
2
3
4
5<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
3.添加配置文件generatorConfig.xml
,配置文件的内容可参考官方文档(http://www.mybatis.org/generator/configreference/xmlconfig.html) ;
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
<generatorConfiguration>
<!-- 数据库驱动,如果是MySQL,此配置换为MySQL驱动类即可 -->
<classPathEntrylocation="D:\Data\maven\repository\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar" />
<contextid="DB2Tables"targetRuntime="MyBatis3">
<!-- 数据库连接地址 -->
<jdbcConnectiondriverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
connectionURL="jdbc:sqlserver://127.0.0.1:1433;Databasename=Demo"
userId="sa"
password="1">
</jdbcConnection>
<javaTypeResolver >
<propertyname="forceBigDecimals"value="false" />
</javaTypeResolver>
<!-- 生成实体类配置 -->
<javaModelGeneratortargetPackage="com.lanweihong.demo.dto"targetProject="src/main/java">
<propertyname="enableSubPackages"value="true" />
<propertyname="trimStrings"value="true" />
</javaModelGenerator>
<!-- 生成mapper映射文件xml配置 -->
<sqlMapGeneratortargetPackage="mapper"targetProject="src/main/resources">
<propertyname="enableSubPackages"value="true" />
</sqlMapGenerator>
<!-- 生成dao配置 -->
<javaClientGeneratortype="XMLMAPPER"targetPackage="com.lanweihong.demo.dao"targetProject="src/main/java">
<propertyname="enableSubPackages"value="true" />
</javaClientGenerator>
<!-- 生成对应表及类名,和数据库对应 -->
<tabletableName="UserInfo"domainObjectName="UserInfoDTO"></table>
</context>
</generatorConfiguration>
4.添加以上配置完成后,单击IDEA菜单栏的Run
选项,选择Edit Configurations...
:
5.单击左上角的新增按钮,新增Maven配置
6.配置可参考以下,配置完成后单击运行按钮即可自动生成。
命令输出如下则表明成功自动生成对应的实体类和Mapper及映射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"C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" -Dmaven.multiModuleProjectDirectory=D:\Data\Code\Java\venue-ms "-Dmaven.home=C:\Program Files\apache-maven-3.6.0" "-Dclassworlds.conf=C:\Program Files\apache-maven-3.6.0\bin\m2.conf" -javaagent:C:\Users\lanweihong\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\191.7479.19\lib\idea_rt.jar=2284:C:\Users\lanweihong\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\191.7479.19\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files\apache-maven-3.6.0\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version2019.1.3 mybatis-generator:generate
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.lanweihong:demo >-------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- mybatis-generator-maven-plugin:1.3.7:generate (default-cli) @ demo ---
[INFO] Connecting to the Database
[INFO] Introspecting table UserInfo
[INFO] Generating Example class for table UserInfo
[INFO] Generating Record class for table UserInfo
[INFO] Generating Mapper Interface for table UserInfo
[INFO] Generating SQL Map for table UserInfo
[INFO] Saving file UserInfoDTOMapper.xml
[INFO] Saving file UserInfoDTOExample.java
[INFO] Saving file UserInfoDTO.java
[INFO] Saving file UserInfoDTOMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.880 s
[INFO] Finished at: 2019-03-06T22:40:52+08:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
以上是 使用MyBatis Generartor自动生成代码 的全部内容, 来源链接: utcz.com/a/132209.html