diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java index 6bc7aeac..3f31ef8f 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java @@ -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() */ diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java index 0c0d5523..27d440b6 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java @@ -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; + } + } \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java index 0a229ca0..520a10e0 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java @@ -1,17 +1,17 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * 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 - *

- * 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. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * 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. */ 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 getPrimaryKeys() { return primaryKeys; diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl index c5b45e48..9a13ffd0 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl @@ -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 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 getOneAs(QueryWrapper query, Class 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 List listAs(QueryWrapper query, Class asType) { + return super.listAs(query, asType); + } + + /** + * @deprecated 无法通过注解进行缓存操作。 + */ + @Override + @Deprecated + public List<#(entityClassName)> listByIds(Collection 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 } \ No newline at end of file