SpringBoot整合自动生成实体、服务、实现类配置
1.配置自动生成器:
package com.qhse.logic.generator; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.GlobalConfig; import com.baomidou.mybatisplus.generator.config.PackageConfig; import com.baomidou.mybatisplus.generator.config.StrategyConfig; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import org.junit.Test; /** * V1.0 */ public class GeneratorServiceEntity { // 模块包名 private String packageModelName = ""; // 去掉表前缀 private String tablePrefix = "WWP_"; // 是否使用lombok private boolean lombokModel = false; @Test public void generateCode() { String packageName = "com.qhse.logic"; boolean serviceNameStartWithI = false; // user -> UserService, 设置成true: user -> IUserService // 设置实体包名 packageModelName = ".trafficManage.vehicleManage"; // 设置表名称前缀 tablePrefix = "WWP_TRAFFIC_"; // 设置使用是否使用lombok lombokModel = true; generateByTables(serviceNameStartWithI, packageName, "WWP_TRAFFIC_VEHICLE_SHIFT"); } private void generateByTables( boolean serviceNameStartWithI, String packageName, String... tableNames) { GlobalConfig config = new GlobalConfig(); String dbUrl = "jdbc:oracle:thin:@192.168.0.186:1521:ORCL10G"; DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig .setDbType(DbType.ORACLE) .setUrl(dbUrl) .setUsername("cloud") .setPassword("cloud") .setDriverName("oracle.jdbc.driver.OracleDriver"); StrategyConfig strategyConfig = new StrategyConfig(); // 实体类自动继承Entity,不需要也可以 if (lombokModel) { strategyConfig .setSuperEntityClass(packageName + ".entity.BaseEntity") .setSuperEntityColumns( new String[]{ "STATUS", "UPDATE_TIME", "CREATE_TIME" }); } strategyConfig.setCapitalMode(true) .setEntityLombokModel(lombokModel) // .setDbColumnUnderline(true) .setTablePrefix(tablePrefix) // 去掉前缀 .setNaming(NamingStrategy.underline_to_camel) .setInclude(tableNames); // 修改替换成你需要的表名,多个表名传数组 config .setActiveRecord(false) // 设置作者 .setAuthor("user-007") .setOutputDir("src\\main\\java") .setFileOverride(true); if (!serviceNameStartWithI) { config.setServiceName("%sService"); } new AutoGenerator() .setGlobalConfig(config) .setDataSource(dataSourceConfig) .setStrategy(strategyConfig) .setPackageInfo( new PackageConfig() // 设置父级包名 .setParent(packageName) // 设置controller层的包名 .setController("controller" + packageModelName) // 设置service层的包名 .setService("service" + packageModelName) // 设置impl层的包名 .setServiceImpl("service.impl" + packageModelName) // 设置entity层的包名 .setEntity("entity" + packageModelName)) .execute(); } private void generateByTables(String packageName, String... tableNames) { generateByTables(true, packageName, tableNames); } }
2.配置Lombok,简化实体代码块
/** * <p> * 车班管理 * </p>*/ @Data // 改注解等同于属性名称的get、set方法 @EqualsAndHashCode(callSuper = true) // 通过注解可以比较实体是否相等 @Accessors(chain = true) // 设置值时,有返回值。可连续调用set方法 @TableName("T_VEHICLE") public class VehicleShift extends BaseEntity { private static final long serialVersionUID = 1L; /** * 主键ID */ private String idVehicle; /** * 车称 */ private String vehicleName; /** * 备注 */ private String remark; }