新增API selectJoinCount()

This commit is contained in:
admin 2021-05-31 11:38:01 +08:00
parent 9a7b1503ed
commit 9ec722ad46
7 changed files with 52 additions and 0 deletions

View File

@ -16,6 +16,13 @@ import java.util.Map;
*/
public interface MPJBaseMapper<T> extends BaseMapper<T> {
/**
* 根据 Wrapper 条件查询总记录数
*
* @param wrapper joinWrapper
*/
Integer selectJoinCount(@Param(Constants.WRAPPER) MPJBaseJoin wrapper);
/**
* 连表查询返回一条记录
*

View File

@ -13,6 +13,11 @@ import java.util.Map;
*/
public interface MPJBaseService<T> extends IService<T> {
/**
* 根据 Wrapper 条件查询总记录数
*/
Integer selectJoinCount(MPJBaseJoin wrapper);
/**
* 连接查询返回一条记录
*/

View File

@ -39,6 +39,11 @@ public class MPJBaseServiceImpl<M extends MPJBaseMapper<T>, T> extends ServiceIm
return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), ServiceImpl.class, 1);
}
@Override
public Integer selectJoinCount(MPJBaseJoin wrapper) {
return baseMapper.selectJoinCount(wrapper);
}
@Override
public <DTO> DTO selectJoinOne(Class<DTO> clazz, MPJBaseJoin wrapper) {
return baseMapper.selectJoinOne(clazz, wrapper);

View File

@ -30,6 +30,7 @@ public class MPJSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> list = super.getMethodList(mapperClass);
list.add(new SelectJoinCount());
list.add(new SelectJoinOne());
list.add(new SelectJoinList());
list.add(new SelectJoinPage());

View File

@ -51,6 +51,13 @@ public abstract class MPJAbstractMethod extends AbstractMethod {
SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), selectColumns);
}
@Override
protected String sqlCount() {
return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null and %s != ''", WRAPPER,
Q_WRAPPER_SQL_SELECT, Q_WRAPPER_SQL_SELECT),
SqlScriptUtils.unSafeParam(Q_WRAPPER_SQL_SELECT), ASTERISK);
}
protected String sqlAlias() {
return SqlScriptUtils.convertIf("${ew.alias}", String.format("%s != null and %s != ''", "ew.alias", "ew.alias"), false);
}

View File

@ -0,0 +1,24 @@
package com.github.yulichang.method;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
/**
* copy {@link com.baomidou.mybatisplus.core.injector.methods.SelectCount}
*
* @author yulichang
* @since 1.1.8
*/
public class SelectJoinCount extends MPJAbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_COUNT;
String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlCount(),
tableInfo.getTableName(), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Integer.class);
}
}

View File

@ -14,6 +14,9 @@ public enum SqlMethod {
/**
* 连表查询
*/
SELECT_JOIN_COUNT("selectJoinCount", "查询满足条件总记录数",
"<script>\n%s SELECT COUNT(%s) FROM %s %s %s %s %s\n</script>"),
SELECT_JOIN_ONE("selectJoinOne", "返回一条记录",
"<script>\n%s SELECT %s FROM %s %s %s %s %s\n</script>"),