!229 feat:内置 serviceImpl-Solon 模板用于切换

Merge pull request !229 from 王帅/main
This commit is contained in:
Michael Yang 2023-08-02 13:43:23 +00:00 committed by Gitee
commit 47887fdbfe
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 86 additions and 32 deletions

View File

@ -20,16 +20,16 @@
<dependencies> <dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.mybatis-flex</groupId> <groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring</artifactId> <artifactId>mybatis-flex-core</artifactId>
<version>${mybatis-flex.version}</version> <version>${mybatis-flex.version}</version>
<exclusions>
<exclusion>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
@ -99,6 +99,7 @@
<artifactId>swagger-annotations</artifactId> <artifactId>swagger-annotations</artifactId>
<version>1.6.11</version> <version>1.6.11</version>
<optional>true</optional> <optional>true</optional>
<scope>test</scope>
</dependency> </dependency>
@ -112,10 +113,37 @@
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
<version>${spring.version}</version> <version>${spring.version}</version>
<optional>true</optional>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring</artifactId>
<version>${mybatis-flex.version}</version>
<exclusions>
<exclusion>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
</exclusion>
</exclusions>
<optional>true</optional>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-solon-plugin</artifactId>
<version>${mybatis-flex.version}</version>
<exclusions>
<exclusion>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
</exclusion>
</exclusions>
<optional>true</optional>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>

View File

@ -15,9 +15,6 @@
*/ */
package com.mybatisflex.codegen.config; package com.mybatisflex.codegen.config;
import com.mybatisflex.spring.service.impl.CacheableServiceImpl;
import com.mybatisflex.spring.service.impl.ServiceImpl;
/** /**
* 生成 ServiceImpl 的配置 * 生成 ServiceImpl 的配置
* *
@ -40,7 +37,7 @@ public class ServiceImplConfig {
/** /**
* 自定义 ServiceImpl 的父类 * 自定义 ServiceImpl 的父类
*/ */
private Class<?> superClass = ServiceImpl.class; private Class<?> superClass;
/** /**
* 是否覆盖之前生成的文件 * 是否覆盖之前生成的文件
@ -53,10 +50,16 @@ public class ServiceImplConfig {
private boolean cacheExample; private boolean cacheExample;
public String buildSuperClassImport() { public String buildSuperClassImport() {
if (superClass == null) {
return "com.mybatisflex.spring.service.impl.ServiceImpl";
}
return superClass.getName(); return superClass.getName();
} }
public String buildSuperClassName() { public String buildSuperClassName() {
if (superClass == null) {
return "ServiceImpl";
}
return superClass.getSimpleName(); return superClass.getSimpleName();
} }
@ -124,7 +127,7 @@ public class ServiceImplConfig {
* 是否生成缓存例子 * 是否生成缓存例子
*/ */
public boolean isCacheExample() { public boolean isCacheExample() {
return CacheableServiceImpl.class.equals(superClass) && cacheExample; return cacheExample;
} }
/** /**

View File

@ -95,7 +95,7 @@ public class StrategyConfig {
ignoreColumns = new HashSet<>(); ignoreColumns = new HashSet<>();
} }
for (String column : columns) { for (String column : columns) {
if (column != null && column.trim().length() > 0) { if (column != null && !column.trim().isEmpty()) {
ignoreColumns.add(column.trim().toLowerCase()); ignoreColumns.add(column.trim().toLowerCase());
} }
} }
@ -103,19 +103,14 @@ public class StrategyConfig {
} }
/** /**
* 设置要生成的模式 * 设置要生成的模式
*
* @return
*/ */
public String getGenerateSchema() { public String getGenerateSchema() {
return generateSchema; return generateSchema;
} }
/** /**
* 获取要生成的模式 * 获取要生成的模式
*
* @param generateSchema
* @return
*/ */
public StrategyConfig setGenerateSchema(String generateSchema) { public StrategyConfig setGenerateSchema(String generateSchema) {
this.generateSchema = generateSchema; this.generateSchema = generateSchema;
@ -209,7 +204,7 @@ public class StrategyConfig {
} }
for (String table : tables) { for (String table : tables) {
if (table != null && table.trim().length() > 0) { if (table != null && !table.trim().isEmpty()) {
generateTables.add(table.trim()); generateTables.add(table.trim());
} }
} }
@ -226,7 +221,7 @@ public class StrategyConfig {
} }
for (String table : tables) { for (String table : tables) {
if (table != null && table.trim().length() > 0) { if (table != null && !table.trim().isEmpty()) {
unGenerateTables.add(table.trim()); unGenerateTables.add(table.trim());
} }
} }

View File

@ -31,6 +31,7 @@ public final class TemplateConst {
public static final String MAPPER = "/templates/enjoy/mapper.tpl"; public static final String MAPPER = "/templates/enjoy/mapper.tpl";
public static final String SERVICE = "/templates/enjoy/service.tpl"; public static final String SERVICE = "/templates/enjoy/service.tpl";
public static final String SERVICE_IMPL = "/templates/enjoy/serviceImpl.tpl"; public static final String SERVICE_IMPL = "/templates/enjoy/serviceImpl.tpl";
public static final String SERVICE_IMPL_SOLON = "/templates/enjoy/serviceImpl-Solon.tpl";
public static final String CONTROLLER = "/templates/enjoy/controller.tpl"; public static final String CONTROLLER = "/templates/enjoy/controller.tpl";
public static final String TABLE_DEF = "/templates/enjoy/tableDef.tpl"; public static final String TABLE_DEF = "/templates/enjoy/tableDef.tpl";
public static final String MAPPER_XML = "/templates/enjoy/mapperXml.tpl"; public static final String MAPPER_XML = "/templates/enjoy/mapperXml.tpl";

View File

@ -0,0 +1,18 @@
package #(packageConfig.serviceImplPackage);
import com.mybatisflex.solon.service.impl.ServiceImpl;
import #(packageConfig.entityPackage).#(table.buildEntityClassName());
import #(packageConfig.mapperPackage).#(table.buildMapperClassName());
import #(packageConfig.servicePackage).#(table.buildServiceClassName());
import org.noear.solon.annotation.ProxyComponent;
/**
* #(table.getComment()) 服务层实现。
*
* @author #(javadocConfig.getAuthor())
* @since #(javadocConfig.getSince())
*/
@ProxyComponent
public class #(table.buildServiceImplClassName()) extends ServiceImpl<#(table.buildMapperClassName()), #(table.buildEntityClassName())> implements #(table.buildServiceClassName()) {
}

View File

@ -21,7 +21,7 @@ import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableConfig; import com.mybatisflex.codegen.config.TableConfig;
import com.mybatisflex.codegen.config.TableDefConfig; import com.mybatisflex.codegen.config.TableDefConfig;
import com.mybatisflex.spring.service.impl.CacheableServiceImpl; import com.mybatisflex.codegen.constant.TemplateConst;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test; import org.junit.Test;
@ -132,7 +132,7 @@ public class GeneratorTest {
globalConfig.enableService(); globalConfig.enableService();
//配置生成 serviceImpl //配置生成 serviceImpl
globalConfig.enableServiceImpl() globalConfig.enableServiceImpl()
.setSuperClass(CacheableServiceImpl.class) // .setSuperClass(CacheableServiceImpl.class)
.setCacheExample(true); .setCacheExample(true);
//配置生成 controller //配置生成 controller
globalConfig.enableController(); globalConfig.enableController();
@ -250,6 +250,10 @@ public class GeneratorTest {
.setMapperXmlPath(System.getProperty("user.dir") + "/src/test/resources/mapper") .setMapperXmlPath(System.getProperty("user.dir") + "/src/test/resources/mapper")
.setBasePackage("com.test"); .setBasePackage("com.test");
// 设置模板路径
globalConfig.getTemplateConfig()
.setServiceImpl(TemplateConst.SERVICE_IMPL_SOLON);
// 设置表前缀和只生成哪些表 // 设置表前缀和只生成哪些表
globalConfig.getStrategyConfig() globalConfig.getStrategyConfig()
.setTablePrefix("sys_") .setTablePrefix("sys_")
@ -261,6 +265,11 @@ public class GeneratorTest {
.setWithLombok(false) .setWithLombok(false)
.setWithActiveRecord(true); .setWithActiveRecord(true);
// 配置生成 service
globalConfig.enableMapper();
globalConfig.enableService();
globalConfig.enableServiceImpl();
//通过 datasource globalConfig 创建代码生成器 //通过 datasource globalConfig 创建代码生成器
Generator generator = new Generator(dataSource, globalConfig); Generator generator = new Generator(dataSource, globalConfig);