Search in sources :

Example 1 with Tran

use of org.noear.solon.data.annotation.Tran in project solon by noear.

the class ServiceImpl method removeBatchByIds.

@Tran
public boolean removeBatchByIds(Collection<T> list, int batchSize, boolean useFill) {
    String sqlStatement = this.getSqlStatement(SqlMethod.DELETE_BY_ID);
    TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
    return this.executeBatch(list, batchSize, (sqlSession, e) -> {
        if (useFill && tableInfo.isWithLogicDelete()) {
            if (this.entityClass.isAssignableFrom(e.getClass())) {
                sqlSession.update(sqlStatement, e);
            } else {
                T instance = tableInfo.newInstance();
                tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), new Object[] { e });
                sqlSession.update(sqlStatement, instance);
            }
        } else {
            sqlSession.update(sqlStatement, e);
        }
    });
}
Also used : TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Tran(org.noear.solon.data.annotation.Tran)

Example 2 with Tran

use of org.noear.solon.data.annotation.Tran in project solon by noear.

the class TranInterceptor method doIntercept.

@Override
public Object doIntercept(Invocation inv) throws Throwable {
    ValHolder val0 = new ValHolder();
    Tran anno = inv.method().getAnnotation(Tran.class);
    TranExecutorImp.global.execute(anno, () -> {
        val0.value = inv.invoke();
    });
    return val0.value;
}
Also used : ValHolder(org.noear.solon.core.ValHolder) Tran(org.noear.solon.data.annotation.Tran)

Example 3 with Tran

use of org.noear.solon.data.annotation.Tran in project solon by noear.

the class ServiceImpl method saveOrUpdate.

@Tran
public boolean saveOrUpdate(T entity) {
    if (null == entity) {
        return false;
    } else {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
        Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!", new Object[0]);
        String keyProperty = tableInfo.getKeyProperty();
        Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!", new Object[0]);
        Object idVal = tableInfo.getPropertyValue(entity, tableInfo.getKeyProperty());
        return !StringUtils.checkValNull(idVal) && !Objects.isNull(this.getById((Serializable) idVal)) ? this.updateById(entity) : this.save(entity);
    }
}
Also used : TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Tran(org.noear.solon.data.annotation.Tran)

Example 4 with Tran

use of org.noear.solon.data.annotation.Tran in project solon by noear.

the class ServiceImpl method saveOrUpdateBatch.

@Tran
public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
    TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
    Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!", new Object[0]);
    String keyProperty = tableInfo.getKeyProperty();
    Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!", new Object[0]);
    return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, entityList, batchSize, (sqlSession, entity) -> {
        Object idVal = tableInfo.getPropertyValue(entity, keyProperty);
        return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession.selectList(this.getSqlStatement(SqlMethod.SELECT_BY_ID), entity));
    }, (sqlSession, entity) -> {
        MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap();
        param.put("et", entity);
        sqlSession.update(this.getSqlStatement(SqlMethod.UPDATE_BY_ID), param);
    });
}
Also used : MapperMethod(org.apache.ibatis.binding.MapperMethod) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) Tran(org.noear.solon.data.annotation.Tran)

Aggregations

Tran (org.noear.solon.data.annotation.Tran)4 TableInfo (com.baomidou.mybatisplus.core.metadata.TableInfo)3 MapperMethod (org.apache.ibatis.binding.MapperMethod)1 ValHolder (org.noear.solon.core.ValHolder)1