Blog

  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

  • 搜索

mybatis_generator

发表于 2019-09-07 分类于 mybatis 阅读次数:
本文字数: 6.8k

MyBatis Generator(mbg)简介

MyBatis Generator将生成:

  • 与表结构匹配的Java POJO。这可能包括:

    • 一个匹配表的主键的类(如果有主键)
    • 一个匹配表的非主键字段的类(BLOB字段除外)
    • 包含表的BLOB字段的类(如果表具有BLOB字段)
    • 用于启用动态选择,更新和删除的类
      这些类之间存在适当的继承关系。请注意,生成器可以配置为生成不同类型的POJO层次结构 - 例如,如果您愿意,可以选择为每个表生成单个域对象。
  • MyBatis / iBATIS兼容的SQL Map XML文件。MBG为配置中的每个表上的简单CRUD函数生成SQL。生成的SQL语句包括:

    • insert
    • update by primary key
    • update by example (using a dynamic where clause)
    • delete by primary key
    • delete by example (using a dynamic where clause)
    • select by primary key
    • select by example (using a dynamic where clause)
    • count by example
      根据表的结构,这些语句有不同的变体(例如,如果表没有主键,则MBG不会通过主键功能生成更新)。
  • 适当使用上述对象的Java客户端类。Java客户端类的生成是可选的。MBG将为MyBatis 3.x生成以下类型的Java客户端:

    • 适用于MyBatis 3.x映射器基础结构的映射器接口

mbg配置文件(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
<?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>
<properties resource="generator.properties"/>
<context id="MySqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<property name="javaFileEncoding" value="UTF-8"/>
<!-- 为模型生成序列化方法-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- 为生成的Java模型创建一个toString方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 自定义插件,可以在生成model类时添加lombok注解 -->
<plugin type="org.mybatis.generator.plugins.LombokPlugin" >
<property name="hasLombok" value="true"/>
</plugin>
<!--可以自定义生成model的代码注释-->
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
<property name="addRemarkComments" value="true"/>
</commentGenerator>
<!--配置数据库连接-->
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.userId}"
password="${jdbc.password}">
<!--解决mysql驱动升级到8.0后不生成指定数据库代码的问题-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!--指定生成model的路径-->
<javaModelGenerator targetPackage="com.yb.mall_demo.mbg.model" targetProject="src/main/java"/>
<!--指定生成mapper.xml的路径-->
<sqlMapGenerator targetPackage="com.yb.mall_demo.mbg.mapper" targetProject="src/main/resources"/>
<!--指定生成mapper接口的的路径-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.yb.mall_demo.mbg.mapper"
targetProject="src/main/java"/>
<!--生成全部表tableName设为%-->
<table tableName="pms_brand">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>

generator.properties(数据库连接配置)

1
2
3
4
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://192.168.88.134:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.userId=root
jdbc.password=123456

LombokPlugin

使用自定义插件,当生成model类时,使用lombok注解,无需生成get,set方法。

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
package org.mybatis.generator.plugins;
/**
* @ClassName LombokPlugin
* @Author Yang
* @Date 2019-09-07 14:53
**/
public class LombokPlugin extends PluginAdapter {
@Override
public boolean validate(List<String> list) {
return true;
}

@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
topLevelClass.addImportedType("lombok.Data");
topLevelClass.addImportedType("lombok.ToString");
topLevelClass.addImportedType("lombok.NoArgsConstructor");
topLevelClass.addImportedType("lombok.AllArgsConstructor");

topLevelClass.addAnnotation("@Data");
topLevelClass.addAnnotation("@ToString");
topLevelClass.addAnnotation("@NoArgsConstructor");
topLevelClass.addAnnotation("@AllArgsConstructor");

topLevelClass.addJavaDocLine("/**");
topLevelClass.addJavaDocLine("* Created by Mybatis Generator on " + date2Str(new Date()));
topLevelClass.addJavaDocLine("*/");

return true;
}

@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

interfaze.addJavaDocLine("/**");
interfaze.addJavaDocLine("* Created by Mybatis Generator on " + date2Str(new Date()));
interfaze.addJavaDocLine("*/");
return true;
}

@Override
public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
//不生成getter
return false;
}

@Override
public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
//不生成setter
return false;
}


private String date2Str(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
return sdf.format(date);
}
}

编译,得到.class文件。然后将这个.class文件放入maven仓库中org\mybatis\generator\mybatis-generator-core\ [版本号],就是你当前pom文件中导入的mybatis-generator-core那个位置。

Generator(运行mbg)

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
/**
* @ClassName Generator
* @Author Yang
* @Date 2019-09-07 13:33
**/
public class Generator {
public static void main(String[] args) throws Exception {
//MBG 执行过程中的警告信息
List<String> warnings = new ArrayList<String>();
//当生成的代码重复时,覆盖原代码
boolean overwrite = true;
//读取我们的 MBG 配置文件
InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(is);
is.close();

DefaultShellCallback callback = new DefaultShellCallback(overwrite);
//创建 MBG
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
//执行生成代码
myBatisGenerator.generate(null);
//输出警告信息
for (String warning : warnings) {
System.out.println(warning);
}
}
}

此处采用java代码的方式运行。也可以使用maven等方式。

mybatis-generator官网


------ 已触及底线感谢您的阅读 ------
麻辣香锅不要辣 微信支付

微信支付

  • 本文作者: 麻辣香锅不要辣
  • 本文链接: https://http://ybhub.gitee.io/2019/09/07/mybatis-generator/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
# mybatis # mbg
jwt
springboot-shiro
  • 文章目录
  • 站点概览
麻辣香锅不要辣

麻辣香锅不要辣

21 日志
11 分类
20 标签
GitHub 简书
  1. 1. MyBatis Generator(mbg)简介
  2. 2. mbg配置文件(generatorConfig.xml)
  3. 3. generator.properties(数据库连接配置)
  4. 4. LombokPlugin
  5. 5. Generator(运行mbg)
© 2019 – 2020 麻辣香锅不要辣 | 站点总字数: 20.4k字
|
0%