mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
feat: 新增生成 spring-cache 缓存模板。
This commit is contained in:
parent
bacc56b00e
commit
2439e93220
@ -1065,6 +1065,20 @@ public class GlobalConfig {
|
|||||||
getServiceImplConfig().setSupperClass(serviceImplSupperClass);
|
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()
|
* @see ControllerConfig#isOverwriteEnable()
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.mybatisflex.codegen.config;
|
package com.mybatisflex.codegen.config;
|
||||||
|
|
||||||
|
import com.mybatisflex.spring.service.impl.CacheableServiceImpl;
|
||||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,6 +47,11 @@ public class ServiceImplConfig {
|
|||||||
*/
|
*/
|
||||||
private boolean overwriteEnable;
|
private boolean overwriteEnable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否生成缓存样例代码。
|
||||||
|
*/
|
||||||
|
private boolean cacheExample;
|
||||||
|
|
||||||
public String buildSuperClassImport() {
|
public String buildSuperClassImport() {
|
||||||
return supperClass.getName();
|
return supperClass.getName();
|
||||||
}
|
}
|
||||||
@ -114,4 +120,19 @@ public class ServiceImplConfig {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否生成缓存例子。
|
||||||
|
*/
|
||||||
|
public boolean isCacheExample() {
|
||||||
|
return CacheableServiceImpl.class.equals(supperClass) && cacheExample;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置生成缓存例子。
|
||||||
|
*/
|
||||||
|
public ServiceImplConfig setCacheExample(boolean cacheExample) {
|
||||||
|
this.cacheExample = cacheExample;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
/**
|
/*
|
||||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
* <p>
|
* <p>
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@ -48,6 +48,15 @@ public class Table {
|
|||||||
this.comment = comment;
|
this.comment = comment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getPrimaryKey() {
|
||||||
|
// 这里默认表中一定会有字段,就不做空判断了
|
||||||
|
return columns.stream()
|
||||||
|
.filter(Column::isPrimaryKey)
|
||||||
|
.findFirst()
|
||||||
|
.map(Column::getProperty)
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public Set<String> getPrimaryKeys() {
|
public Set<String> getPrimaryKeys() {
|
||||||
return primaryKeys;
|
return primaryKeys;
|
||||||
|
|||||||
@ -1,3 +1,6 @@
|
|||||||
|
#set(isCacheExample = serviceImplConfig.cacheExample)
|
||||||
|
#set(primaryKey = table.getPrimaryKey())
|
||||||
|
#set(entityClassName = table.buildEntityClassName())
|
||||||
package #(packageConfig.serviceImplPackage);
|
package #(packageConfig.serviceImplPackage);
|
||||||
|
|
||||||
import #(serviceImplConfig.buildSuperClassImport());
|
import #(serviceImplConfig.buildSuperClassImport());
|
||||||
@ -5,6 +8,18 @@ import #(packageConfig.entityPackage).#(table.buildEntityClassName());
|
|||||||
import #(packageConfig.mapperPackage).#(table.buildMapperClassName());
|
import #(packageConfig.mapperPackage).#(table.buildMapperClassName());
|
||||||
import #(packageConfig.servicePackage).#(table.buildServiceClassName());
|
import #(packageConfig.servicePackage).#(table.buildServiceClassName());
|
||||||
import org.springframework.stereotype.Service;
|
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()) 服务层实现。
|
* #(table.getComment()) 服务层实现。
|
||||||
@ -13,6 +28,92 @@ import org.springframework.stereotype.Service;
|
|||||||
* @since #(javadocConfig.getSince())
|
* @since #(javadocConfig.getSince())
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
|
#if(isCacheExample)
|
||||||
|
@CacheConfig(cacheNames = "#(firstCharToLowerCase(entityClassName))")
|
||||||
|
#end
|
||||||
public class #(table.buildServiceImplClassName()) extends #(serviceImplConfig.buildSuperClassName())<#(table.buildMapperClassName()), #(table.buildEntityClassName())> implements #(table.buildServiceClassName()) {
|
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
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user