AutoGenerator配置

举报
yd_263341302 发表于 2024/06/10 18:39:27 2024/06/10
【摘要】 哈喽,大家好,我是木头左,AI改变生活!本文将详细解释 MyBatis 生成 Java 代码的过程,包括全局配置、数据源配置和策略配置。 1. 全局配置首先,我们需要创建一个 GlobalConfig 对象,用于配置 MyBatis 的全局设置。以下是一些常用的全局配置选项:setOutputDir(String outputDir):设置生成 Java 代码的输出目录。setFileOve...

哈喽,大家好,我是木头左,AI改变生活!

本文将详细解释 MyBatis 生成 Java 代码的过程,包括全局配置、数据源配置和策略配置。

1. 全局配置

首先,我们需要创建一个 GlobalConfig 对象,用于配置 MyBatis 的全局设置。以下是一些常用的全局配置选项:

  • setOutputDir(String outputDir):设置生成 Java 代码的输出目录。
  • setFileOverride(boolean fileOverride):设置是否覆盖已存在的同名文件。
  • setActiveRecord(boolean activeRecord):设置是否生成 ActiveRecord 特性。如果不需要 ActiveRecord 特性,请设置为 false。
  • setEnableCache(boolean enableCache):设置是否启用二级缓存。
  • setBaseResultMap(boolean baseResultMap):设置是否生成 XML ResultMap。
  • setBaseColumnList(boolean baseColumnList):设置是否生成 XML columList。
  • setAuthor("pengzuo"):设置作者信息。
  • setMapperName("%s"):设置 Mapper 接口的命名规则。
  • setServiceImplName("%sDaoImpl"):设置 Service 实现类的命名规则。
  • setServiceName("I%sDao"):设置 Service 接口的命名规则。
  • setEntityName("%sEntity"):设置 Entity 类的命名规则。
  • setControllerName("%sController"):设置 Controller 类的命名规则。
    接下来,我们将这些全局配置应用到 MyBatis Generator(MBG)中:
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(outPut + "java");
gc.setFileOverride(true);
gc.setActiveRecord(true);
gc.setEnableCache(false);
gc.setBaseResultMap(true);
gc.setBaseColumnList(false);
gc.setAuthor("pengzuo");
gc.setMapperName("%s");
gc.setServiceImplName("%sDaoImpl");
gc.setServiceName("I%sDao");
gc.setEntityName("%sEntity");
gc.setControllerName("%sController");
mpg.setGlobalConfig(gc);

2. 数据源配置

数据源配置用于设置 MyBatis Generator 连接数据库的相关参数。以下是一些常用的数据源配置选项:

  • setDbType(DbType dbType):设置数据库类型,如 MySQL、PostgreSQL 等。
  • setDriverName(String driverName):设置数据库驱动类名。
  • setUsername(String username):设置数据库用户名。
  • setPassword(String password):设置数据库密码。
  • setUrl(String url):设置数据库连接 URL。
    接下来,我们将这些数据源配置应用到 MyBatis Generator(MBG)中:
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.POSTGRE_SQL);
dsc.setDriverName("org.postgresql.Driver");
dsc.setUsername(username);
dsc.setPassword(password);
dsc.setUrl(dbUrl);
mpg.setDataSource(dsc);

3. 策略配置

策略配置用于设置 MyBatis Generator 生成代码的策略,例如表名生成策略、字段常量生成策略等。以下是一些常用的策略配置选项:

  • setTablePrefix(String[] tablePrefix):设置需要生成代码的表的前缀。
  • setNaming(NamingStrategy naming):设置表名生成策略,如下划线转驼峰命名等。
  • setEntityColumnConstant(boolean entityColumnConstant):设置是否为实体类生成字段常量。
  • setEntityTableFieldAnnotationEnable(boolean entityTableFieldAnnotationEnable):设置是否为实体类生成字段注解。
  • setInclude(String[] include):设置需要生成代码的表名。
    接下来,我们将这些策略配置应用到 MyBatis Generator(MBG)中:
StrategyConfig strategy = new StrategyConfig();
strategy.setTablePrefix(new String[]{tablePrefix});
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setEntityColumnConstant(true);
strategy.setEntityTableFieldAnnotationEnable(true);
strategy.setInclude(new String[]{"tb_application"});
mpg.setStrategy(strategy);

至此,我们已经完成了 MyBatis Generator 的配置。接下来,我们可以运行 MyBatis Generator 命令来根据配置文件生成 Java 代码。

我是木头左,感谢各位童鞋的点赞、收藏,我们下期更精彩!

(mutouzuo.oss-cn-hangzhou.aliyuncs.com/my/mudouzuo1.png)

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。