|
@@ -60,6 +60,8 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
|
|
|
|
|
|
protected Class<?> entityClass = currentModelClass();
|
|
|
|
|
|
+ protected Class<?> mapperClass = currentMapperClass();
|
|
|
+
|
|
|
/**
|
|
|
* 判断数据库操作是否成功
|
|
|
*
|
|
@@ -72,6 +74,10 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
|
|
|
return SqlHelper.retBool(result);
|
|
|
}
|
|
|
|
|
|
+ protected Class<T> currentMapperClass() {
|
|
|
+ return (Class<T>) ReflectionKit.getSuperClassGenericType(getClass(), 0);
|
|
|
+ }
|
|
|
+
|
|
|
protected Class<T> currentModelClass() {
|
|
|
return (Class<T>) ReflectionKit.getSuperClassGenericType(getClass(), 1);
|
|
|
}
|
|
@@ -102,7 +108,10 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
|
|
|
*
|
|
|
* @param sqlMethod ignore
|
|
|
* @return ignore
|
|
|
+ * @see #getSqlStatement(SqlMethod)
|
|
|
+ * @deprecated 3.3.3
|
|
|
*/
|
|
|
+ @Deprecated
|
|
|
protected String sqlStatement(SqlMethod sqlMethod) {
|
|
|
return SqlHelper.table(entityClass).getSqlStatement(sqlMethod.getMethod());
|
|
|
}
|
|
@@ -117,10 +126,21 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public boolean saveBatch(Collection<T> entityList, int batchSize) {
|
|
|
- String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE);
|
|
|
+ String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE);
|
|
|
return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取mapperStatementId
|
|
|
+ *
|
|
|
+ * @param sqlMethod 方法名
|
|
|
+ * @return 命名id
|
|
|
+ * @since 3.3.3
|
|
|
+ */
|
|
|
+ protected String getSqlStatement(SqlMethod sqlMethod) {
|
|
|
+ return SqlHelper.getSqlStatement(mapperClass, sqlMethod);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* TableId 注解存在更新记录,否插入一条记录
|
|
|
*
|
|
@@ -148,21 +168,21 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
|
|
|
Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
|
|
|
String keyProperty = tableInfo.getKeyProperty();
|
|
|
Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
|
|
|
- return SqlHelper.saveOrUpdateBatch(this.entityClass, this.log, entityList, batchSize, (sqlSession, entity) -> {
|
|
|
+ return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, entityList, batchSize, (sqlSession, entity) -> {
|
|
|
Object idVal = ReflectionKit.getFieldValue(entity, keyProperty);
|
|
|
return StringUtils.checkValNull(idVal)
|
|
|
- || CollectionUtils.isEmpty(sqlSession.selectList(tableInfo.getSqlStatement(SqlMethod.SELECT_BY_ID.getMethod()), entity));
|
|
|
+ || CollectionUtils.isEmpty(sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_BY_ID), entity));
|
|
|
}, (sqlSession, entity) -> {
|
|
|
MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
|
|
|
param.put(Constants.ENTITY, entity);
|
|
|
- sqlSession.update(tableInfo.getSqlStatement(SqlMethod.UPDATE_BY_ID.getMethod()), param);
|
|
|
+ sqlSession.update(getSqlStatement(SqlMethod.UPDATE_BY_ID), param);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public boolean updateBatchById(Collection<T> entityList, int batchSize) {
|
|
|
- String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID);
|
|
|
+ String sqlStatement = getSqlStatement(SqlMethod.UPDATE_BY_ID);
|
|
|
return executeBatch(entityList, batchSize, (sqlSession, entity) -> {
|
|
|
MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
|
|
|
param.put(Constants.ENTITY, entity);
|
|
@@ -226,5 +246,5 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
|
|
|
protected <E> boolean executeBatch(Collection<E> list, BiConsumer<SqlSession, E> consumer) {
|
|
|
return executeBatch(list, DEFAULT_BATCH_SIZE, consumer);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|