mybatis-plus 聚合查詢
原因
由于查詢條件太多懶得寫xml
在已經(jīng)寫好的LambdaQueryWrapper查詢條件下,封裝groupCount類似語句
代碼
使用
LambdaQueryWrapper<DBEntity> query = Wrappers.lambdaQuery();
List<DBEntity> count = baseMapper.groupCount(query, col(DBEntity::getSomeThing));
col方法
private String col(SFunction<DBEntity, ?> column) {
SerializedLambda resolve = LambdaUtils.resolve(column);
Map<String, ColumnCache> columnMap = LambdaUtils.getColumnMap(resolve.getImplClassName());
String implMethodName = resolve.getImplMethodName();
ColumnCache columnCache = columnMap.get(StringUtils.resolveFieldName(implMethodName).toUpperCase());
return columnCache.getColumn();
}
mapper接口
List<DBEntity> groupCount(@Param(Constants.WRAPPER) Wrapper<DBEntity> userWrapper, @Param("col") String col);
xml文件
<select id="groupCount" resultType="DBEntity">
select count(${col}) as count,${col} from t_alert_config ${ew.customSqlSegment}
group by ${col}
</select>
class DBEntity{
@TableField(value = "count", strategy = FieldStrategy.IGNORED, exist = false, select = false)
private Long count;
//數(shù)據(jù)庫實體映射字段
private String someThing;
public Long getCount() {
return count;
}
public DBEntity setCount(Long count) {
this.count = count;
return this;
}
public String getSomeThing() {
return someThing;
}
public DBEntity setSomeThing(String someThing) {
this.someThing = someThing;
return this;
}
}
結(jié)果
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@154ce972] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1822440743 wrapping com.mysql.jdbc.JDBC4Connection@693ed572] will not be managed by Spring
==> Preparing: select count(case_id) as count,case_id from xxxx WHERE dept_num = ? group by case_id
==> Parameters: 01001001(String)
<== Columns: count, case_id
<== Row: 996, 0
<== Row: 1, 34
<== Row: 1, 35
<== Total: 3

浙公網(wǎng)安備 33010602011771號