fix: update 方法不能使用 CachePut 注解。

This commit is contained in:
Suomm 2023-06-02 18:10:26 +08:00
parent 206ca15b1b
commit ed28acd98d
4 changed files with 24 additions and 26 deletions

View File

@ -103,16 +103,23 @@ public class AccountServiceImpl extends CacheableServiceImpl<MyAccountMapper, Ac
return super.removeByIds(ids); return super.removeByIds(ids);
} }
// 根据查询条件更新时,实体类主键可能为 null。
@Override @Override
@CachePut(key = "#entity.id") @CacheEvict(allEntries = true)
public boolean update(Account entity, QueryWrapper query) { public boolean update(Account entity, QueryWrapper query) {
return super.update(entity, query); return super.update(entity, query);
} }
@Override @Override
@CachePut(key = "#entity.id") @CacheEvict(key = "#entity.id")
public boolean updateById(Account entity) { public boolean updateById(Account entity, boolean ignoreNulls) {
return super.updateById(entity); return super.updateById(entity, ignoreNulls);
}
@Override
@CacheEvict(allEntries = true)
public boolean updateBatch(Collection<Account> entities, int batchSize) {
return super.updateBatch(entities, batchSize);
} }
@Override @Override

View File

@ -14,7 +14,6 @@ import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
@ -53,15 +52,21 @@ public class #(table.buildServiceImplClassName()) extends #(serviceImplConfig.bu
} }
@Override @Override
@CachePut(key = "#entity.#(primaryKey)") @CacheEvict(allEntries = true)
public boolean update(#(entityClassName) entity, QueryWrapper query) { public boolean update(#(entityClassName) entity, QueryWrapper query) {
return super.update(entity, query); return super.update(entity, query);
} }
@Override @Override
@CachePut(key = "#entity.#(primaryKey)") @CacheEvict(key = "#entity.#(primaryKey)")
public boolean updateById(#(entityClassName) entity) { public boolean updateById(#(entityClassName) entity, boolean ignoreNulls) {
return super.updateById(entity); return super.updateById(entity, ignoreNulls);
}
@Override
@CacheEvict(allEntries = true)
public boolean updateBatch(Collection<#(entityClassName)> entities, int batchSize) {
return super.updateBatch(entities, batchSize);
} }
@Override @Override

View File

@ -160,16 +160,16 @@ public interface IService<T> {
* *
* @param entity 实体类对象 * @param entity 实体类对象
* @return {@code true} 更新成功{@code false} 更新失败 * @return {@code true} 更新成功{@code false} 更新失败
* @apiNote 若实体类属性数据为 {@code null}该属性不会新到数据库
*/ */
default boolean updateById(T entity) { default boolean updateById(T entity) {
return SqlUtil.toBool(getMapper().update(entity)); return updateById(entity, true);
} }
/** /**
* 根据主键更新数据 * 根据主键更新数据
* *
* @param entity 实体对象 * @param entity 实体对象
* @param ignoreNulls 是否忽略 null * @param ignoreNulls 是否忽略 null
* @return {@code true} 更新成功{@code false} 更新失败 * @return {@code true} 更新成功{@code false} 更新失败
*/ */
@ -227,7 +227,6 @@ public interface IService<T> {
* @param batchSize 每批次更新数量 * @param batchSize 每批次更新数量
* @return {@code true} 更新成功{@code false} 更新失败 * @return {@code true} 更新成功{@code false} 更新失败
*/ */
@SuppressWarnings("unchecked")
default boolean updateBatch(Collection<T> entities, int batchSize) { default boolean updateBatch(Collection<T> entities, int batchSize) {
return Db.tx(() -> { return Db.tx(() -> {
final List<T> entityList = CollectionUtil.toList(entities); final List<T> entityList = CollectionUtil.toList(entities);

View File

@ -17,7 +17,6 @@
package com.mybatisflex.spring.service.impl; package com.mybatisflex.spring.service.impl;
import com.mybatisflex.core.BaseMapper; import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.query.QueryTable; import com.mybatisflex.core.query.QueryTable;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.service.IService; import com.mybatisflex.core.service.IService;
@ -25,8 +24,6 @@ import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.table.TableInfoFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collection;
/** /**
* <p>可缓存数据的 Service 实现类 * <p>可缓存数据的 Service 实现类
* *
@ -35,8 +32,6 @@ import java.util.Collection;
* <ul> * <ul>
* <li>重写 {@link #saveOrUpdate(Object)} 方法分别调用 {@link #save(Object)} {@link #updateById(Object)} * <li>重写 {@link #saveOrUpdate(Object)} 方法分别调用 {@link #save(Object)} {@link #updateById(Object)}
* 方法避免缓存无法更新造成数据不一致 * 方法避免缓存无法更新造成数据不一致
* <li>重写{@link #updateBatch(Collection, int)} 方法默认抛出异常不支持批量更新操作
* 防止批量更新数据缓存不一致
* <li>重写 {@link #query()} 方法解决使用 {@link QueryWrapper#toSQL()} 作为缓存 * <li>重写 {@link #query()} 方法解决使用 {@link QueryWrapper#toSQL()} 作为缓存
* 的主键时"SELECT * FROM" 后面没有表名的问题 * 的主键时"SELECT * FROM" 后面没有表名的问题
* </ul> * </ul>
@ -72,14 +67,6 @@ public class CacheableServiceImpl<M extends BaseMapper<T>, T> implements IServic
} }
} }
/**
* <p>不支持批量更新操作
*/
@Override
public boolean updateBatch(Collection<T> entities, int batchSize) {
throw FlexExceptions.wrap("Batch update do not support caching operation.");
}
/** /**
* <p>获取默认的 {@link QueryWrapper} * <p>获取默认的 {@link QueryWrapper}
* *