指标库:根据表名列名分页查询数据
This commit is contained in:
parent
def22049e4
commit
7c47311649
@ -44,4 +44,11 @@ public class TableColumnController {
|
||||
List<ColumnVo> result = tableColumnService.getColumnsByTableName(tableName);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
@Operation(summary = "根据表名列名分页查询数据")
|
||||
@PostMapping("/getDataByTableName")
|
||||
public R<Page<?>> getDataByTableName(@RequestBody JSONObject params) {
|
||||
Page<?> result = tableColumnService.getDataByTableName(params);
|
||||
return R.ok(result);
|
||||
}
|
||||
}
|
||||
|
@ -23,4 +23,6 @@ public interface TableColumnService extends IService<TableColumn> {
|
||||
List<TableVo> getTablesInfo();
|
||||
|
||||
List<ColumnVo> getColumnsByTableName(String tableName);
|
||||
|
||||
Page<?> getDataByTableName(JSONObject params);
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
package iet.ustb.sf.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
|
||||
import iet.ustb.sf.domain.Columns;
|
||||
import iet.ustb.sf.domain.TableColumn;
|
||||
import iet.ustb.sf.domain.Tables;
|
||||
@ -14,6 +17,7 @@ import iet.ustb.sf.util.CheckUtils;
|
||||
import iet.ustb.sf.vo.ColumnVo;
|
||||
import iet.ustb.sf.vo.TableVo;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@ -118,6 +122,28 @@ public class TableColumnServiceImpl extends ServiceImpl<TableColumnMapper, Table
|
||||
CheckUtils.checkTableName(tableName);
|
||||
return tableColumnMapper.getColumnsByTableName(tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<?> getDataByTableName(JSONObject params) {
|
||||
String columns = params.getString("columns");
|
||||
String tableName = params.getString("tableName");
|
||||
int pageIndex = Optional.ofNullable(params.getInteger("pageIndex")).orElse(1);
|
||||
int pageSize = Optional.ofNullable(params.getInteger("pageSize")).orElse(10);
|
||||
if (StringUtils.isEmpty(columns)) {
|
||||
return new Page<>(pageIndex, pageSize);
|
||||
}
|
||||
CheckUtils.checkColumns(columns, tableName);
|
||||
|
||||
String countSql = "SELECT COUNT(1) FROM " + tableName;
|
||||
long total = SqlRunner.db().selectCount(countSql);
|
||||
Page<Map<String, Object>> page = new Page<>(pageIndex, pageSize);
|
||||
page.setTotal(total);
|
||||
|
||||
long offset = (long) (pageIndex - 1) * pageSize;
|
||||
String sql = "SELECT " + columns + " FROM " + tableName + " LIMIT " + pageSize + " OFFSET " + offset;
|
||||
|
||||
return SqlRunner.db().selectPage(page, sql);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -39,4 +39,16 @@ public class CheckUtils {
|
||||
long cnt = SqlRunner.db().selectCount(sql);
|
||||
ThrowUtils.throwIf(cnt <= 0, new RuntimeException("表" + tableName + "不存在!"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 列名验证
|
||||
*
|
||||
* @param columns 列名(多个列表以逗号分割)
|
||||
* @param tableName 表名
|
||||
*/
|
||||
public static void checkColumns(String columns, String tableName) {
|
||||
checkEmpty(columns, "列名");
|
||||
String sql = "SELECT " + columns + " FROM " + tableName + " limit 5";
|
||||
SqlRunner.db().selectList(sql);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user