35.自动生成实体类,mapper

package com.hlxd.cloud.consumer.service.impl;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Czq
 * @Date 2022/5/17 9:03
 * @Description: TODO
 * @Version: 1.0
 */
public class TestAutoGenerate {
	@Test
	public void autoGenerate() {
		// Step1:代码生成器
		AutoGenerator mpg = new AutoGenerator();

		// Step2:全局配置
		GlobalConfig gc = new GlobalConfig();
		// 填写代码生成的目录(需要修改)
		String projectPath = "E:\\hlxd\\scada-code2\\hl-cloud\\hl-cloud-service\\hl-cloud-device-access";
		// 拼接出代码最终输出的目录
		gc.setOutputDir(projectPath + "/src/main/java");
		// 配置开发者信息(可选)(需要修改)
		gc.setAuthor("ksl");
		// 配置是否打开目录,false 为不打开(可选)
		gc.setOpen(false);
		// 实体属性 Swagger2 注解,添加 Swagger 依赖,开启 Swagger2 模式(可选)
		//gc.setSwagger2(true);
		// 重新生成文件时是否覆盖,false 表示不覆盖(可选)
		gc.setFileOverride(false);
		// 配置主键生成策略,此处为 ASSIGN_ID(可选)
		gc.setIdType(IdType.AUTO);
		// 配置日期类型,此处为 ONLY_DATE(可选)
		gc.setDateType(DateType.ONLY_DATE);
		// 默认生成的 service 会有 I 前缀
		gc.setServiceName("%sService");
		mpg.setGlobalConfig(gc);

		// Step3:数据源配置(需要修改)
		DataSourceConfig dsc = new DataSourceConfig();
		// 配置数据库 url 地址
		dsc.setUrl("jdbc:mysql://127.0.0.1:3306/hl_cloud_iot?useUnicode=true&characterEncoding=utf8");
		// dsc.setSchemaName("testMyBatisPlus"); // 可以直接在 url 中指定数据库名
		// 配置数据库驱动
		dsc.setDriverName("com.mysql.cj.jdbc.Driver");
		// 配置数据库连接用户名
		dsc.setUsername("root");
		// 配置数据库连接密码
		dsc.setPassword("123456");
		mpg.setDataSource(dsc);

		// Step:4:包配置
		PackageConfig pc = new PackageConfig();
		// 配置父包名(需要修改)
		pc.setParent("com.hlxd.cloud.iot.device.access");
		// 配置模块名(需要修改)
//		pc.setModuleName("model");
		// 配置 entity 包名
		pc.setEntity("entity");
		// 配置 mapper 包名
		pc.setMapper("mapper");
		// 配置 service 包名
		pc.setService("service");
		// 配置 controller 包名
		pc.setController("controller");
		mpg.setPackageInfo(pc);

		//Step5:配置mapper文件位置
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
			}
		};
		String templatePath = "/templates/mapper.xml.vm";
		//⾃定义输出配置
		List<FileOutConfig> focList = new ArrayList<>();
		//⾃定义配置会被优先输出
		focList.add(new FileOutConfig(templatePath) {
			@Override
			public String outputFile(TableInfo tableInfo) {
				//⾃定义输出⽂件名,如果你Entity设置了前后缀、此处注意xml的名称会跟着发⽣变化!!
				return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
			}
		});
		cfg.setFileOutConfigList(focList);
		mpg.setCfg(cfg);
		TemplateConfig templateConfig = new TemplateConfig();
		templateConfig.setXml(null);
		mpg.setTemplate(templateConfig);


		// Step6:策略配置(数据库表配置)
		StrategyConfig strategy = new StrategyConfig();
		// 指定表名(可以同时操作多个表,使用 , 隔开)(需要修改)
		strategy.setInclude("iot_opc_config", "iot_opc_group", "iot_opc_detail");
		// 配置数据表与实体类名之间映射的策略
		strategy.setNaming(NamingStrategy.underline_to_camel);
		// 配置数据表的字段与实体类的属性名之间映射的策略
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		// 配置 lombok 模式
		strategy.setEntityLombokModel(true);
		// 配置 rest 风格的控制器(@RestController)
		strategy.setRestControllerStyle(true);
		// 配置驼峰转连字符
		strategy.setControllerMappingHyphenStyle(true);
		// 配置表前缀,生成实体时去除表前缀
		// 此处的表名为 test_mybatis_plus_user,模块名为 test_mybatis_plus,去除前缀后剩下为 user。
		//strategy.setTablePrefix(pc.getModuleName() + "_");
		mpg.setStrategy(strategy);

		// Step6:执行代码生成操作
		mpg.execute();
	}
}

工具
http://www.javacoder.top/mybatis_plus.html#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!对于自动生成实体类mapper的问题,可以考虑使用MyBatis Generator插件来实现。MyBatis Generator是一个官方支持的代码生成工具,可以根据数据库表结构自动生成实体类mapper接口。 以下是一些简单的步骤来使用MyBatis Generator插件来生成实体类mapper: 1. 首先,您需要在您的项目中引入MyBatis Generator插件的依赖。您可以在pom.xml文件(如果您使用Maven)或build.gradle文件(如果您使用Gradle)中添加相应的依赖。 2. 接下来,编写一个MyBatis Generator的配置文件(一般命名为generatorConfig.xml),用于定义生成实体类mapper的规则。配置文件中包含了数据库连接信息、要生成的表、生成的目标包等信息。 3. 在配置文件中,您可以配置生成的实体类的规则,例如是否使用Lombok注解、是否生成对应字段的getters和setters等。 4. 配置好generatorConfig.xml文件后,您可以执行MyBatis Generator插件来生成实体类mapper。一般情况下,可以通过命令行或者IDE中的插件来执行。 5. 执行成功后,您将在指定的目标包中看到生成的实体类mapper接口。这些文件将根据您在配置文件中定义的规则生成。 需要注意的是,使用MyBatis Generator插件生成的实体类mapper只是初始化的代码,您仍然需要根据需要进行调整和扩展。 希望以上信息对您有所帮助!如有更多问题,请继续提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值