From ed28acd98dfd7ea33dfd91e897055a2905311eb0 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 2 Jun 2023 18:10:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20update=20=E6=96=B9=E6=B3=95=E4=B8=8D?= =?UTF-8?q?=E8=83=BD=E4=BD=BF=E7=94=A8=20CachePut=20=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/core/data-cache.md | 15 +++++++++++---- .../resources/templates/enjoy/serviceImpl.tpl | 15 ++++++++++----- .../com/mybatisflex/core/service/IService.java | 7 +++---- .../spring/service/impl/CacheableServiceImpl.java | 13 ------------- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/docs/zh/core/data-cache.md b/docs/zh/core/data-cache.md index e0ce5d6d..75e297c7 100644 --- a/docs/zh/core/data-cache.md +++ b/docs/zh/core/data-cache.md @@ -103,16 +103,23 @@ public class AccountServiceImpl extends CacheableServiceImpl entities, int batchSize) { + return super.updateBatch(entities, batchSize); } @Override 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 9a13ffd0..da1e8ed1 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl @@ -14,7 +14,6 @@ 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; @@ -53,15 +52,21 @@ public class #(table.buildServiceImplClassName()) extends #(serviceImplConfig.bu } @Override - @CachePut(key = "#entity.#(primaryKey)") + @CacheEvict(allEntries = true) 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); + @CacheEvict(key = "#entity.#(primaryKey)") + public boolean updateById(#(entityClassName) entity, boolean ignoreNulls) { + return super.updateById(entity, ignoreNulls); + } + + @Override + @CacheEvict(allEntries = true) + public boolean updateBatch(Collection<#(entityClassName)> entities, int batchSize) { + return super.updateBatch(entities, batchSize); } @Override diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java index 19652951..e556c184 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/service/IService.java @@ -160,16 +160,16 @@ public interface IService { * * @param entity 实体类对象 * @return {@code true} 更新成功,{@code false} 更新失败。 + * @apiNote 若实体类属性数据为 {@code null},该属性不会新到数据库。 */ default boolean updateById(T entity) { - return SqlUtil.toBool(getMapper().update(entity)); + return updateById(entity, true); } - /** * 根据主键更新数据 * - * @param entity 实体对象 + * @param entity 实体对象 * @param ignoreNulls 是否忽略 null 值 * @return {@code true} 更新成功,{@code false} 更新失败。 */ @@ -227,7 +227,6 @@ public interface IService { * @param batchSize 每批次更新数量 * @return {@code true} 更新成功,{@code false} 更新失败。 */ - @SuppressWarnings("unchecked") default boolean updateBatch(Collection entities, int batchSize) { return Db.tx(() -> { final List entityList = CollectionUtil.toList(entities); 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 index 2ddad0d5..961e680e 100644 --- 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 @@ -17,7 +17,6 @@ package com.mybatisflex.spring.service.impl; import com.mybatisflex.core.BaseMapper; -import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.query.QueryTable; import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.service.IService; @@ -25,8 +24,6 @@ import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfoFactory; import org.springframework.beans.factory.annotation.Autowired; -import java.util.Collection; - /** *

可缓存数据的 Service 实现类。 * @@ -35,8 +32,6 @@ import java.util.Collection; *

    *
  • 重写 {@link #saveOrUpdate(Object)} 方法,分别调用 {@link #save(Object)} 和 {@link #updateById(Object)} * 方法,避免缓存无法更新造成数据不一致。 - *
  • 重写{@link #updateBatch(Collection, int)} 方法,默认抛出异常,不支持批量更新操作, - * 防止批量更新数据,缓存不一致。 *
  • 重写 {@link #query()} 方法,解决使用 {@link QueryWrapper#toSQL()} 作为缓存 * 的主键时,"SELECT * FROM" 后面没有表名的问题。 *
@@ -72,14 +67,6 @@ public class CacheableServiceImpl, T> implements IServic } } - /** - *

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

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