Skip to content

Commit

Permalink
fix: 基础service添加事务
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiuman committed Jul 9, 2021
1 parent d98d290 commit 1385897
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public K update(D entity) throws Exception {
}


@Transactional(rollbackFor = Exception.class)
@Override
public boolean remove(D entity) {
return beforeRemove(entity) && getService().remove(dtoToEntity().apply(entity));
Expand All @@ -125,6 +126,7 @@ public boolean remove(Wrapper<D> wrapper) {
return getService().remove((Wrapper<E>) wrapper);
}

@Transactional(rollbackFor = Exception.class)
@Override
public void batchRemove(Iterable<K> keys) {
List<K> keyList = new ArrayList<>();
Expand All @@ -136,6 +138,7 @@ public void batchRemove(Iterable<K> keys) {
getService().batchRemove(keys);
}

@Transactional(rollbackFor = Exception.class)
@Override
public void clear() {
getService().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.github.yiuman.citrus.support.utils.LambdaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
Expand Down Expand Up @@ -48,6 +49,7 @@ protected TreeMapper<E> getTreeMapper() {
}
}

@Transactional(rollbackFor = Exception.class)
@Override
public boolean beforeSave(E entity) throws Exception {
if (entity.getId() != null) {
Expand Down Expand Up @@ -79,6 +81,7 @@ public boolean beforeSave(E entity) throws Exception {
//Over
}

@Transactional(rollbackFor = Exception.class)
@Override
public K save(E entity) throws Exception {
if (!this.beforeSave(entity)) {
Expand All @@ -87,18 +90,21 @@ public K save(E entity) throws Exception {
return getService().save(entity);
}

@Transactional(rollbackFor = Exception.class)
@Override
public boolean batchSave(Iterable<E> entityIterable) {
return getService().batchSave(entityIterable);
}

@Transactional(rollbackFor = Exception.class)
@Override
public boolean beforeRemove(E entity) {
//1.更新所有右值小于当前父节点右值的节点左值 -2
this.beforeDeleteOrMove(entity);
return true;
}

@Transactional(rollbackFor = Exception.class)
@Override
public boolean remove(E entity) {
if (!this.beforeRemove(entity)) {
Expand All @@ -108,18 +114,21 @@ public boolean remove(E entity) {
return getService().remove(entity);
}

@Transactional(rollbackFor = Exception.class)
@Override
public boolean remove(Wrapper<E> wrappers) {
return getService().remove(wrappers);
}

@Transactional(rollbackFor = Exception.class)
@Override
public void batchRemove(Iterable<K> keys) {
List<K> keyList = new ArrayList<>();
keys.forEach(keyList::add);
getTreeMapper().deleteBatch(list(Wrappers.<E>query().in(getKeyColumn(), keyList)));
}

@Transactional(rollbackFor = Exception.class)
@Override
public void clear() {
getService().clear();
Expand Down Expand Up @@ -155,11 +164,13 @@ public E getRoot() {
return getTreeMapper().selectOne(Wrappers.<E>query().isNull(getParentField()));
}

@Transactional(rollbackFor = Exception.class)
@Override
public synchronized void reInit() throws Exception {
reInit(getRoot());
}

@Transactional(rollbackFor = Exception.class)
protected void reInit(E current) throws Exception {
getService().save(current);
List<E> children = loadByParent(current.getId());
Expand Down Expand Up @@ -243,6 +254,7 @@ public List<E> loadByParent(K parentKey) {
return list(Wrappers.<E>query().eq(getParentField(), parentKey));
}

@Transactional(rollbackFor = Exception.class)
@Override
public void move(E current, K moveTo) throws Exception {
//更新树
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public K save(E entity) throws Exception {
return null;
}

@Transactional(rollbackFor = Exception.class)
@Override
public boolean batchSave(Iterable<E> entityIterable) {
entityIterable.forEach(LambdaUtils.consumerWrapper(this::beforeSave));
Expand All @@ -77,6 +78,7 @@ public boolean batchSave(Iterable<E> entityIterable) {
return assertSave;
}

@Transactional(rollbackFor = Exception.class)
@Override
public K update(E entity) throws Exception {
if (!this.beforeUpdate(entity)) {
Expand All @@ -87,11 +89,13 @@ public K update(E entity) throws Exception {
return key;
}

@Transactional(rollbackFor = Exception.class)
@Override
public boolean remove(E entity) {
return this.beforeRemove(entity) && getMapper().deleteById(getKey(entity)) > 1;
}

@Transactional(rollbackFor = Exception.class)
@Override
public void batchRemove(Iterable<K> keys) {
List<K> keyList = new ArrayList<>();
Expand All @@ -103,12 +107,12 @@ public void batchRemove(Iterable<K> keys) {
getMapper().deleteBatchIds((Collection<? extends Serializable>) keys);
}

@Transactional(rollbackFor = Exception.class)
@Override
public void clear() {
getMapper().deleteBatchIds(
list().parallelStream()
.map(LambdaUtils.functionWrapper(this::getKey))
.collect(Collectors.toList()));
getMapper().deleteBatchIds(list().parallelStream()
.map(LambdaUtils.functionWrapper(this::getKey))
.collect(Collectors.toList()));
}

@Override
Expand Down Expand Up @@ -136,6 +140,7 @@ public <P extends IPage<E>> P page(P page, Wrapper<E> queryWrapper) {
return getMapper().selectPage(page, queryWrapper);
}

@Transactional(rollbackFor = Exception.class)
@Override
public boolean remove(Wrapper<E> wrapper) {
return getMapper().delete(wrapper) >= 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.yiuman.citrus.support.model.BaseTree;
import com.github.yiuman.citrus.support.utils.LambdaUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
Expand Down Expand Up @@ -86,6 +87,7 @@ public List<E> loadByParent(K parentKey) {
return parentKey == null ? list(queryWrapper.isNull(getParentField())) : list(queryWrapper.eq(getParentField(), parentKey));
}

@Transactional(rollbackFor = Exception.class)
@Override
public void move(E current, K moveTo) throws Exception {
current.setParentId(moveTo);
Expand Down

0 comments on commit 1385897

Please sign in to comment.