Skip to content

Commit

Permalink
mp
Browse files Browse the repository at this point in the history
  • Loading branch information
andanyoung committed Oct 18, 2023
1 parent 3f8822d commit cf4235f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 52 deletions.
7 changes: 0 additions & 7 deletions admin4j-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
<jsch.version>0.1.55</jsch.version>
<netty-all.version>4.1.86.Final</netty-all.version>
<ip2region.version>2.6.6</ip2region.version>
<okhttp3.version>4.11.0</okhttp3.version>
<justauth.version>1.4.0</justauth.version>
<jimureport.version>1.5.6</jimureport.version>
<xercesImpl.version>2.12.2</xercesImpl.version>
Expand Down Expand Up @@ -612,12 +611,6 @@
<version>${netty-all.version}</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp3.version}</version>
</dependency>


<dependency>
<groupId>com.xkcoding.justauth</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,14 @@
*/
public interface ICommandBatchService<T> {

/**
* 默认批次提交数量
*/
int DEFAULT_BATCH_SIZE = 1000;


/**
* 插入(批量)
*
* @param entityList 实体对象集合
*/
@Transactional(rollbackFor = Exception.class)
default boolean saveBatch(Collection<T> entityList) {
return saveBatch(entityList, DEFAULT_BATCH_SIZE);
}
boolean saveBatch(Collection<T> entityList);


/**
Expand All @@ -45,9 +38,7 @@ default boolean saveBatch(Collection<T> entityList) {
* @param entityList 实体对象集合
*/
@Transactional(rollbackFor = Exception.class)
default boolean saveOrUpdateBatch(Collection<T> entityList) {
return saveOrUpdateBatch(entityList, DEFAULT_BATCH_SIZE);
}
boolean saveOrUpdateBatch(Collection<T> entityList);


/**
Expand All @@ -67,9 +58,7 @@ default boolean saveOrUpdateBatch(Collection<T> entityList) {
* @param entityList 实体对象集合
*/
@Transactional(rollbackFor = Exception.class)
default boolean updateBatchById(Collection<T> entityList) {
return updateBatchById(entityList, DEFAULT_BATCH_SIZE);
}
boolean updateBatchById(Collection<T> entityList);

/**
* 根据ID 批量更新
Expand Down Expand Up @@ -108,9 +97,7 @@ default boolean updateBatchById(Collection<T> entityList) {
* @since 3.5.0
*/
@Transactional(rollbackFor = Exception.class)
default boolean removeBatchByIds(Collection<?> list) {
return removeBatchByIds(list, DEFAULT_BATCH_SIZE);
}
boolean removeBatchByIds(Collection<?> list);

/**
* 批量删除(jdbc批量提交)
Expand All @@ -133,9 +120,7 @@ default boolean removeBatchByIds(Collection<?> list) {
* @since 3.5.0
*/
@Transactional(rollbackFor = Exception.class)
default boolean removeBatchByIds(Collection<?> list, boolean useFill) {
return removeBatchByIds(list, DEFAULT_BATCH_SIZE, useFill);
}
boolean removeBatchByIds(Collection<?> list, boolean useFill);


/**
Expand All @@ -147,7 +132,5 @@ default boolean removeBatchByIds(Collection<?> list, boolean useFill) {
* @return 删除结果
* @since 3.5.0
*/
default boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
throw new UnsupportedOperationException("不支持的方法!");
}
boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.admin4j.framework.mp.service;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;

import java.io.Serializable;

Expand All @@ -27,9 +26,7 @@ public interface ICommandService<T> {
*
* @param entity 实体对象
*/
default boolean save(T entity) {
return SqlHelper.retBool(getBaseMapper().insert(entity));
}
boolean save(T entity);


/**
Expand All @@ -47,9 +44,7 @@ default boolean save(T entity) {
*
* @param entity 实体对象
*/
default boolean updateById(T entity) {
return SqlHelper.retBool(getBaseMapper().updateById(entity));
}
boolean updateById(T entity);


//---------------------------------------------------------------- remove ------------------------
Expand All @@ -59,9 +54,7 @@ default boolean updateById(T entity) {
*
* @param id 主键ID
*/
default boolean removeById(Serializable id) {
return SqlHelper.retBool(getBaseMapper().deleteById(id));
}
boolean removeById(Serializable id);

/**
* 根据 ID 删除
Expand All @@ -71,7 +64,5 @@ default boolean removeById(Serializable id) {
* @return 删除结果
* @since 3.5.0
*/
default boolean removeById(Serializable id, boolean useFill) {
throw new UnsupportedOperationException("不支持的方法!");
}
boolean removeById(Serializable id, boolean useFill);
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ public boolean removeById(Serializable id) {
return true;
}

/**
* 根据 entity 条件,删除记录
*
* @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
*/
protected boolean remove(Wrapper<T> queryWrapper) {
return SqlHelper.retBool(getBaseMapper().delete(queryWrapper));
}

// ---------------------------------------------------------------- exist ----------------------------------------------------------------

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
* @since 2023/8/17 11:04
*/

public class CommandServiceImpl<M extends BaseMapper<T>, T> extends QueryServiceImpl<M, T> implements ICommandService<T>, IBizService<T>, ICommandBatchService<T> {

public class CommandServiceImpl<M extends BaseMapper<T>, T> extends QueryServiceImpl<M, T> implements ICommandService<T>, ICommandBatchService<T>, IBizService<T> {

protected final static int DEFAULT_BATCH_SIZE = 1000;
protected Log log = LogFactory.getLog(getClass());


Expand Down Expand Up @@ -78,6 +78,26 @@ protected LambdaUpdateWrapper<T> lambdaUpdate(T entity) {
return Wrappers.lambdaUpdate(entity);
}

/**
* 插入一条记录(选择字段,策略插入)
*
* @param entity 实体对象
*/
@Override
public boolean save(T entity) {
return SqlHelper.retBool(getBaseMapper().insert(entity));
}

/**
* 插入(批量)
*
* @param entityList 实体对象集合
*/
@Transactional(rollbackFor = Exception.class)
public boolean saveBatch(Collection<T> entityList) {
return saveBatch(entityList, DEFAULT_BATCH_SIZE);
}


/**
* 批量插入
Expand All @@ -87,7 +107,6 @@ protected LambdaUpdateWrapper<T> lambdaUpdate(T entity) {
* @return ignore
*/
@Transactional(rollbackFor = Exception.class)
@Override
public boolean saveBatch(Collection<T> entityList, int batchSize) {
String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE);
return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
Expand Down Expand Up @@ -124,8 +143,26 @@ public boolean saveOrUpdate(T entity) {
return false;
}


/**
* 批量修改插入
*
* @param entityList 实体对象集合
*/
@Transactional(rollbackFor = Exception.class)
public boolean saveOrUpdateBatch(Collection<T> entityList) {
return saveOrUpdateBatch(entityList, DEFAULT_BATCH_SIZE);
}

/**
* 批量修改插入
* Params:
*
* @param entityList 实体对象集合
* @param batchSize 每次的数量
* @return
*/
@Transactional(rollbackFor = Exception.class)
@Override
public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
Expand All @@ -142,8 +179,34 @@ public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
});
}

@Transactional(rollbackFor = Exception.class)
/**
* 根据 ID 选择修改
*
* @param entity 实体对象
*/
@Override
public boolean updateById(T entity) {
return SqlHelper.retBool(getBaseMapper().updateById(entity));
}

/**
* 根据ID 批量更新
*
* @param entityList 实体对象集合
*/
@Transactional(rollbackFor = Exception.class)
public boolean updateBatchById(Collection<T> entityList) {
return updateBatchById(entityList, DEFAULT_BATCH_SIZE);
}

/**
* 根据ID 批量更新
*
* @param entityList 实体对象集合
* @param batchSize 更新批次数量
* @return
*/
@Transactional(rollbackFor = Exception.class)
public boolean updateBatchById(Collection<T> entityList, int batchSize) {
String sqlStatement = getSqlStatement(SqlMethod.UPDATE_BY_ID);
return executeBatch(entityList, batchSize, (sqlSession, entity) -> {
Expand Down Expand Up @@ -189,7 +252,6 @@ public boolean removeById(Serializable id) {
return SqlHelper.retBool(getBaseMapper().deleteById(id));
}

@Override
@Transactional(rollbackFor = Exception.class)
public boolean removeByIds(Collection<?> list) {
if (CollectionUtils.isEmpty(list)) {
Expand All @@ -202,6 +264,19 @@ public boolean removeByIds(Collection<?> list) {
return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
}

/**
* 批量删除(jdbc批量提交)
*
* @param list 主键ID或实体列表(主键ID类型必须与实体类型字段保持一致)
* @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
* @return 删除结果
* @since 3.5.0
*/
@Transactional(rollbackFor = Exception.class)
public boolean removeBatchByIds(Collection<?> list, boolean useFill) {
return removeBatchByIds(list, DEFAULT_BATCH_SIZE, useFill);
}

/**
* 批量删除
*
Expand All @@ -211,7 +286,6 @@ public boolean removeByIds(Collection<?> list) {
* @since 3.5.0
*/
@Transactional(rollbackFor = Exception.class)
@Override
public boolean removeByIds(Collection<?> list, boolean useFill) {
if (CollectionUtils.isEmpty(list)) {
return false;
Expand All @@ -235,14 +309,34 @@ public boolean removeById(Serializable id, boolean useFill) {
return SqlHelper.retBool(getBaseMapper().deleteById(id));
}

@Override

/**
* 批量删除(jdbc批量提交)
*
* @param list 主键ID或实体列表(主键ID类型必须与实体类型字段保持一致)
* @return 删除结果
* @since 3.5.0
*/
@Transactional(rollbackFor = Exception.class)
public boolean removeBatchByIds(Collection<?> list) {
return removeBatchByIds(list, DEFAULT_BATCH_SIZE);
}

/**
* 批量删除(jdbc批量提交)
*
* @param list 主键ID或实体列表
* @param batchSize 批次大小
* @return 删除结果
* @since 3.5.0
*/
@Transactional(rollbackFor = Exception.class)
public boolean removeBatchByIds(Collection<?> list, int batchSize) {
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
return removeBatchByIds(list, batchSize, tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill());
}

@Override

@Transactional(rollbackFor = Exception.class)
public boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
String sqlStatement = getSqlStatement(SqlMethod.DELETE_BY_ID);
Expand Down

0 comments on commit cf4235f

Please sign in to comment.