框架学习

JdbcTemplate读取SQL语句查询结果

@Autowired
private JdbcTemplate jdbcTemplate;

@PostMapping("/sqlTools")
public RespData sqlTools(@RequestParam String dbName){

    String sql = "select table_name from information_schema.tables where table_schema = '" + dbName + "'";
    System.out.println(sql);
    List<List<Map<String, Object>>> result = jdbcTemplate.execute(sql, (CallableStatementCallback<List<List<Map<String, Object>>>>) cs -> {
        List<List<Map<String, Object>>> list = new ArrayList<>();
        boolean execute = cs.execute();

        while (execute) {
            ResultSet resultSet = cs.getResultSet();
            List<Map<String, Object>> subList = new ArrayList<>();
            while (resultSet.next()) {
                ResultSetMetaData meta = resultSet.getMetaData();
                int colcount = meta.getColumnCount();
                Map<String, Object> map = new HashMap<>();
                for (int i = 1; i <= colcount; i++) {
                    String name = meta.getColumnLabel(i);
                    Object tableName = resultSet.getObject(i);
                    map.put(name, tableName);

                    if(hasColumn(dbName, tableName.toString(),"is_delete")){
                        System.out.println(tableName.toString());
                    }
                }
                subList.add(map);
            }
            list.add(subList);
            execute = cs.getMoreResults();
        }
        return list;
    });
    return RespDataUtil.ok(result);
}