diff --git a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/service/impl/CacheableServiceImpl.java b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/service/impl/CacheableServiceImpl.java new file mode 100644 index 00000000..b58b94de --- /dev/null +++ b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/service/impl/CacheableServiceImpl.java @@ -0,0 +1,109 @@ +/* + * 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.spring.service.impl; + +import com.mybatisflex.core.BaseMapper; +import com.mybatisflex.core.exception.FlexExceptions; +import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.service.IService; +import com.mybatisflex.core.table.TableInfo; +import com.mybatisflex.core.table.TableInfoFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Collection; + +/** + *

可缓存数据的 Service 实现类。 + * + *

该实现类对缓存做了以下处理: + * + *

+ * + * @author 王帅 + * @since 2023-05-30 + */ +public class CacheableServiceImpl, T> implements IService { + + @Autowired + @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") + private M mapper; + + /** + * {@inheritDoc} + */ + @Override + public BaseMapper getMapper() { + return mapper; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean saveOrUpdate(T entity) { + TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass()); + Object[] pkArgs = tableInfo.buildPkSqlArgs(entity); + if (pkArgs.length == 0 || pkArgs[0] == null) { + return save(entity); + } else { + return updateById(entity); + } + } + + /** + *

不支持批量更新操作。 + */ + @Override + public boolean updateBatch(Collection entities, int batchSize) { + throw FlexExceptions.wrap("Batch update do not support caching operation."); + } + + /** + *

获取默认的 {@link QueryWrapper}。 + * + *

使用 {@link QueryWrapper#create()} 构建默认查询条件的时候, + * 要使用 {@link QueryWrapper#from(String...)} 方法指定从哪个表 + * 查询数据,不然使用 {@link QueryWrapper#toDebugSQL()} 生成的 + * SQL 语句就是 {@code "SELECT * FROM"},没有表名信息。 + * + *

默认通过反射获取表名,建议重写,根据情况设置默认表名,以提升效率。 + * + *

例如: + * + *

{@code
+     * @Override
+     * public QueryWrapper query() {
+     *     return QueryWrapper.create().from(ACCOUNT);
+     * }
+     * }
+ * + * @return 默认的 {@link QueryWrapper} + */ + @Override + public QueryWrapper query() { + String tableName = TableInfoFactory.ofMapperClass(getMapper().getClass()).getTableName(); + return QueryWrapper.create().from(tableName); + } + +} \ No newline at end of file