喵星之旅-狂奔的兔子-mybatis代码生成器

mybatis-generator

一、需要准备的内容

idea社区版、jdk1.8、数据库连接驱动、可用的数据库

二、新建maven项目

配置pom文件

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?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"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>club.kittybunny</groupId>
<artifactId>mybatis-tool</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.55</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
</dependencies>

<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.5</version>
<configuration>
<configurationFile>src/main/java/club/kittybunny/mybatistool/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.5</version>
</dependency>
</dependencies>
</plugin>


</plugins>
</build>

</project>

里面指定了配置文件路径:

<configurationFile>src/main/java/club/kittybunny/mybatistool/generatorConfig.xml</configurationFile>

三、创建配置文件

在指定路径下创建配置文件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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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="/home/bunny/文档/mysql-connector-java-5.1.25-bin.jar"/>
<context id="my" targetRuntime="MyBatis3">

<!-- 配置pojo的序列化 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>

</commentGenerator>

<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/bunny" userId="root"
password="123456"/>

<javaModelGenerator targetPackage="club.kittybunny.bunnybatis.entity"
targetProject="/home/bunny/文档/demo">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<sqlMapGenerator targetPackage="mybatis/sqlmap"
targetProject="/home/bunny/文档/demo">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<javaClientGenerator targetPackage="club.kittybunny.bunnybatis.dao"
targetProject="/home/bunny/文档/demo" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>




<table tableName="sys_role_permission" domainObjectName="SysRolePermission"
enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
selectByExampleQueryId="true" >

</table>
<table tableName="sys_user" domainObjectName="SysUser"
enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
selectByExampleQueryId="true" >
<columnOverride column="ID" javaType="java.lang.Double"></columnOverride>
        <generatedKey column="id" sqlStatement="MySql" identity="true"/>


</table>

</context>
</generatorConfiguration>

以下内容自行修改:

里面2个表的配置:一个默认、一个指定个别字段的转换类型和主键自动生成(插入数据后内存中对象获得主键)。

指明了驱动路径是:

"/home/bunny/文档/mysql-connector-java-5.1.25-bin.jar"

连接信息:

jdbcConnection

生成文件的包路径和文件位置:

javaModelGenerator
sqlMapGenerator
javaClientGenerator

四、运行程序生成代码

找到相应的jar运行,在配置好的路径下就会出现生成的文件,不可以重复生成。如果要再次生成,需要把原有路径下生成内容删除。
Alt text

文章目录
  1. 一、需要准备的内容
  2. 二、新建maven项目
  3. 三、创建配置文件
  4. 四、运行程序生成代码
|