java springboot mybatis plus 3.4 实现执行任意 sql 语句
试了 SqlRunner 一直失败,不知道原因,于是试了如下方法,完美解决。
@Autowired private SqlSessionFactory sqlSessionFactory; public List<Map<String, Object>> executeSql(String sql) throws SQLException { try(var sqlSession = sqlSessionFactory.openSession();) { try(var connection = sqlSession.getConnection();) { List<Map<String, Object>> resultList = new ArrayList<>(); var statement = connection.createStatement(); var resultSet = statement.executeQuery(sql); var metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount(); while (resultSet.next()) { Map<String, Object> rowMap = new HashMap<>(); for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnLabel(i); Object columnValue = resultSet.getObject(i); rowMap.put(columnName, columnValue); } resultList.add(rowMap); } return resultList; } } }
桂棹兮兰桨,击空明兮溯流光。