Mybatis代码自动生成插件使用


1.配置pom.xml

添加mybatis-generator-maven-plugin到pom.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
<? xml  version = "1.0"  encoding = "UTF-8" ?>
< project  xmlns = "http://maven.apache.org/POM/4.0.0"
          xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
     < parent >
         < artifactId >orm</ artifactId >
         < groupId >com.cdai.demo</ groupId >
         < version >1.0-SNAPSHOT</ version >
     </ parent >
     < modelVersion >4.0.0</ modelVersion >
 
     < artifactId >mybatis-generator</ artifactId >
 
     < dependencies >
         < dependency >
             < groupId >mysql</ groupId >
             < artifactId >mysql-connector-java</ artifactId >
             < version >5.1.9</ version >
         </ dependency >
     </ dependencies >
 
     < build >
         < plugins >
             < plugin >
                 < groupId >org.mybatis.generator</ groupId >
                 < artifactId >mybatis-generator-maven-plugin</ artifactId >
                 < version >1.3.2</ version >
                 < configuration >
                     < verbose >true</ verbose >
                     < overwrite >true</ overwrite >
                 </ configuration >
             </ plugin >
         </ plugins >
     </ build >
 
</ project >



2.插件配置文件

插件配置文件默认读取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"
 
< generatorConfiguration >
     < classPathEntry  location = "C:\Users\cdai\.m2\repository\mysql\mysql-connector-java\5.1.9\mysql-connector-java-5.1.9.jar"  />
 
     < context  id = "Test"  targetRuntime = "MyBatis3" >
 
         < jdbcConnection  driverClass = "com.mysql.jdbc.Driver"
                         connectionURL = "jdbc:mysql://192.168.1.100:3306/demo?generateSimpleParameterMetadata=true"
                         userId = "root"
                         password = "" >
         </ jdbcConnection >
 
         < javaTypeResolver  >
             < property  name = "forceBigDecimals"  value = "false"  />
         </ javaTypeResolver >
 
         <!-- generate Model -->
         < javaModelGenerator  targetPackage = "com.cdai.demo.orm.mybatis"  targetProject = "D:\ideaspaces\demo\orm\mybatis-generator\src\main\java" >
             < property  name = "enableSubPackages"  value = "true"  />
             < property  name = "trimStrings"  value = "true"  />
         </ javaModelGenerator >
 
         <!-- generate xml -->
         < sqlMapGenerator  targetPackage = "com.cdai.demo.orm.mybatis"   targetProject = "D:\ideaspaces\demo\orm\mybatis-generator\src\main\resources" >
             < property  name = "enableSubPackages"  value = "true"  />
         </ sqlMapGenerator >
 
         <!-- generate Mapper -->
         < javaClientGenerator  type = "XMLMAPPER"  targetPackage = "com.cdai.demo.orm.mybatis"   targetProject = "D:\ideaspaces\demo\orm\mybatis-generator\src\main\java" >
             < property  name = "enableSubPackages"  value = "true"  />
         </ javaClientGenerator >
 
         < table  schema = ""  tableName = "account"  domainObjectName = "Account" ></ table >
 
     </ context >
 
</ generatorConfiguration >



3.运行插件

运行代码生成插件的Maven任务:


生成代码:


©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页