feat: 新增生成 spring-cache 缓存模板。

This commit is contained in:
Suomm 2023-06-01 20:19:11 +08:00
parent bacc56b00e
commit 2439e93220
4 changed files with 159 additions and 14 deletions

View File

@ -1065,6 +1065,20 @@ public class GlobalConfig {
getServiceImplConfig().setSupperClass(serviceImplSupperClass);
}
/**
* @see ServiceImplConfig#isCacheExample()
*/
public boolean isServiceImplCacheExample() {
return getServiceImplConfig().isCacheExample();
}
/**
* @see ServiceImplConfig#setCacheExample(boolean)
*/
public void setServiceImplCacheExample(boolean cacheExample) {
getServiceImplConfig().setCacheExample(cacheExample);
}
/**
* @see ControllerConfig#isOverwriteEnable()
*/

View File

@ -15,6 +15,7 @@
*/
package com.mybatisflex.codegen.config;
import com.mybatisflex.spring.service.impl.CacheableServiceImpl;
import com.mybatisflex.spring.service.impl.ServiceImpl;
/**
@ -46,6 +47,11 @@ public class ServiceImplConfig {
*/
private boolean overwriteEnable;
/**
* 是否生成缓存样例代码
*/
private boolean cacheExample;
public String buildSuperClassImport() {
return supperClass.getName();
}
@ -114,4 +120,19 @@ public class ServiceImplConfig {
return this;
}
/**
* 是否生成缓存例子
*/
public boolean isCacheExample() {
return CacheableServiceImpl.class.equals(supperClass) && cacheExample;
}
/**
* 设置生成缓存例子
*/
public ServiceImplConfig setCacheExample(boolean cacheExample) {
this.cacheExample = cacheExample;
return this;
}
}

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
*/
package com.mybatisflex.codegen.entity;
@ -48,6 +48,15 @@ public class Table {
this.comment = comment;
}
public String getPrimaryKey() {
// 这里默认表中一定会有字段就不做空判断了
return columns.stream()
.filter(Column::isPrimaryKey)
.findFirst()
.map(Column::getProperty)
.orElse(null);
}
public Set<String> getPrimaryKeys() {
return primaryKeys;

View File

@ -1,3 +1,6 @@
#set(isCacheExample = serviceImplConfig.cacheExample)
#set(primaryKey = table.getPrimaryKey())
#set(entityClassName = table.buildEntityClassName())
package #(packageConfig.serviceImplPackage);
import #(serviceImplConfig.buildSuperClassImport());
@ -5,6 +8,18 @@ import #(packageConfig.entityPackage).#(table.buildEntityClassName());
import #(packageConfig.mapperPackage).#(table.buildMapperClassName());
import #(packageConfig.servicePackage).#(table.buildServiceClassName());
import org.springframework.stereotype.Service;
#if(isCacheExample)
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
#end
/**
* #(table.getComment()) 服务层实现。
@ -13,6 +28,92 @@ import org.springframework.stereotype.Service;
* @since #(javadocConfig.getSince())
*/
@Service
#if(isCacheExample)
@CacheConfig(cacheNames = "#(firstCharToLowerCase(entityClassName))")
#end
public class #(table.buildServiceImplClassName()) extends #(serviceImplConfig.buildSuperClassName())<#(table.buildMapperClassName()), #(table.buildEntityClassName())> implements #(table.buildServiceClassName()) {
#if(isCacheExample)
@Override
@CacheEvict(allEntries = true)
public boolean remove(QueryWrapper query) {
return super.remove(query);
}
@Override
@CacheEvict(key = "#id")
public boolean removeById(Serializable id) {
return super.removeById(id);
}
@Override
@CacheEvict(allEntries = true)
public boolean removeByIds(Collection<? extends Serializable> ids) {
return super.removeByIds(ids);
}
@Override
@CachePut(key = "#entity.#(primaryKey)")
public boolean update(#(entityClassName) entity, QueryWrapper query) {
return super.update(entity, query);
}
@Override
@CachePut(key = "#entity.#(primaryKey)")
public boolean updateById(#(entityClassName) entity) {
return super.updateById(entity);
}
@Override
@Cacheable(key = "#id")
public #(entityClassName) getById(Serializable id) {
return super.getById(id);
}
@Override
@Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
public #(entityClassName) getOne(QueryWrapper query) {
return super.getOne(query);
}
@Override
@Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
public <R> R getOneAs(QueryWrapper query, Class<R> asType) {
return super.getOneAs(query, asType);
}
@Override
@Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
public List<#(entityClassName)> list(QueryWrapper query) {
return super.list(query);
}
@Override
@Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
public <R> List<R> listAs(QueryWrapper query, Class<R> asType) {
return super.listAs(query, asType);
}
/**
* @deprecated 无法通过注解进行缓存操作。
*/
@Override
@Deprecated
public List<#(entityClassName)> listByIds(Collection<? extends Serializable> ids) {
return super.listByIds(ids);
}
@Override
@Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
public long count(QueryWrapper query) {
return super.count(query);
}
@Override
@Cacheable(key = "#root.methodName + ':' + #query.toSQL()")
public Page<#(entityClassName)> page(Page<#(entityClassName)> page, QueryWrapper query) {
return super.page(page, query);
}
#end
}