代码生成器

This commit is contained in:
GYJ 2025-02-07 14:51:56 +08:00
parent 88dc5e10b9
commit 0c95415ea9
7 changed files with 198 additions and 64 deletions

View File

@ -1,19 +0,0 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip

View File

@ -3,22 +3,29 @@ package com.czg;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.core.service.IService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.zaxxer.hikari.HikariDataSource;
/**
* @author ww
*/
public class Main {
private final static String BASE_URL = "obmt6die8bni6i00-mi.aliyun-cn-hangzhou-internet.oceanbase.cloud";
private final static String PORT = "3306";
private final static String USERNAME = "admin";
private final static String PASSWORD = "kWF9I/3[Mu,H";
private final static String DATABASE = "czg_cashier";
public static void main(String[] args) {
//配置数据源
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/your-database?characterEncoding=utf-8");
dataSource.setUsername("root");
dataSource.setPassword("******");
dataSource.setJdbcUrl("jdbc:mysql://" + BASE_URL + ":" + PORT + "/" + DATABASE);
dataSource.setUsername(USERNAME);
dataSource.setPassword(PASSWORD);
//创建配置内容两种风格都可以
GlobalConfig globalConfig = createGlobalConfigUseStyle1();
//GlobalConfig globalConfig = createGlobalConfigUseStyle2();
GlobalConfig globalConfig = createGlobalConfigUseStyle();
//通过 datasource globalConfig 创建代码生成器
Generator generator = new Generator(dataSource, globalConfig);
@ -27,65 +34,77 @@ public class Main {
generator.generate();
}
public static GlobalConfig createGlobalConfigUseStyle1() {
public static GlobalConfig createGlobalConfigUseStyle() {
//创建配置内容
GlobalConfig globalConfig = new GlobalConfig();
//设置根包
globalConfig.setBasePackage("com.test");
//设置表前缀和只生成哪些表
globalConfig.setTablePrefix("tb_");
globalConfig.setGenerateTable("tb_account", "tb_account_session");
//设置生成 entity 并启用 Lombok
globalConfig.setEntityGenerateEnable(true);
globalConfig.setEntityWithLombok(true);
//设置项目的JDK版本项目的JDK为14及以上时建议设置该项小于14则可以不设置
globalConfig.setEntityJdkVersion(17);
//设置生成 mapper
globalConfig.setMapperGenerateEnable(true);
//可以单独配置某个列
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setColumnName("tenant_id");
columnConfig.setLarge(true);
columnConfig.setVersion(true);
globalConfig.setColumnConfig("tb_account", columnConfig);
return globalConfig;
}
public static GlobalConfig createGlobalConfigUseStyle2() {
//创建配置内容
GlobalConfig globalConfig = new GlobalConfig();
String currentWorkingDirectory = System.getProperty("user.dir");
String projectPath = currentWorkingDirectory + "/cash-service/system-service";
// String projectPath = currentWorkingDirectory + "/cash-service/account-service";
// String projectPath = currentWorkingDirectory + "/cash-service/product-service";
// String projectPath = currentWorkingDirectory + "/cash-service/order-service";
//设置根包
globalConfig.getPackageConfig()
.setBasePackage("com.test");
.setSourceDir(projectPath + "/src/main/java")
.setBasePackage("com.czg.service.system");
globalConfig.getServiceConfig()
.setClassSuffix("Service")
.setSuperClass(IService.class);
globalConfig.enableService();
globalConfig.getServiceImplConfig()
.setClassSuffix("ServiceImpl")
.setSuperClass(ServiceImpl.class);
globalConfig.enableServiceImpl();
globalConfig.getMapperConfig()
.setClassSuffix("Mapper");
globalConfig.enableMapper();
globalConfig.setMapperXmlPath(projectPath + "/src/main/resources/mapper");
globalConfig.enableMapperXml();
//设置表前缀和只生成哪些表setGenerateTable 未配置时生成所有表
globalConfig.getStrategyConfig()
.setTablePrefix("tb_")
.setGenerateTable("tb_account", "tb_account_session");
.setTablePrefix("")
.setGenerateTable("sys_params");
//设置生成 entity 并启用 Lombok
globalConfig.enableEntity()
.setWithLombok(true)
.setJdkVersion(17);
.setJdkVersion(23);
//设置生成 mapper
globalConfig.enableMapper();
//可以单独配置某个列
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setColumnName("tenant_id");
columnConfig.setLarge(true);
columnConfig.setVersion(true);
ColumnConfig createTime = new ColumnConfig();
createTime.setColumnName("create_time");
createTime.setOnInsertValue("now()");
globalConfig.getStrategyConfig()
.setColumnConfig("tb_account", columnConfig);
.setColumnConfig(createTime);
ColumnConfig updateTime = new ColumnConfig();
updateTime.setColumnName("update_time");
updateTime.setOnUpdateValue("now()");
updateTime.setOnInsertValue("now()");
globalConfig.getStrategyConfig()
.setColumnConfig(updateTime);
ColumnConfig deleted = new ColumnConfig();
deleted.setColumnName("is_del");
deleted.setLogicDelete(true);
globalConfig.getStrategyConfig()
.setColumnConfig(deleted);
// ColumnConfig version = new ColumnConfig();
// version.setColumnName("version");
// version.setVersion(true);
// globalConfig.getStrategyConfig()
// .setColumnConfig(version);
return globalConfig;
}
}
}

View File

@ -0,0 +1,81 @@
package com.czg.service.system.entity;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.io.Serial;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 实体类
*
* @author mac
* @since 2025-02-07
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("sys_params")
public class SysParams implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* ID
*/
@Id(keyType = KeyType.Auto)
private Long id;
/**
* 参数编码
*/
private String paramCode;
/**
* 参数值
*/
private String paramValue;
/**
* 类型 0系统参数 1非系统参数
*/
private Integer paramType;
/**
* 备注
*/
private String remark;
/**
* 创建者id
*/
private Long createUserId;
/**
* 创建时间
*/
@Column(onInsertValue = "now()")
private LocalDateTime createTime;
/**
* 更新者id
*/
private Long updateUserId;
/**
* 更新时间
*/
@Column(onInsertValue = "now()", onUpdateValue = "now()")
private LocalDateTime updateTime;
}

View File

@ -0,0 +1,14 @@
package com.czg.service.system.mapper;
import com.mybatisflex.core.BaseMapper;
import com.czg.service.system.entity.SysParams;
/**
* 映射层
*
* @author mac
* @since 2025-02-07
*/
public interface SysParamsMapper extends BaseMapper<SysParams> {
}

View File

@ -0,0 +1,14 @@
package com.czg.service.system.service;
import com.mybatisflex.core.service.IService;
import com.czg.service.system.entity.SysParams;
/**
* 服务层
*
* @author mac
* @since 2025-02-07
*/
public interface SysParamsService extends IService<SysParams> {
}

View File

@ -0,0 +1,18 @@
package com.czg.service.system.service.impl;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.service.system.entity.SysParams;
import com.czg.service.system.mapper.SysParamsMapper;
import com.czg.service.system.service.SysParamsService;
import org.springframework.stereotype.Service;
/**
* 服务层实现
*
* @author mac
* @since 2025-02-07
*/
@Service
public class SysParamsServiceImpl extends ServiceImpl<SysParamsMapper, SysParams> implements SysParamsService{
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czg.service.system.mapper.SysParamsMapper">
</mapper>