feat(chat): 增加数据库SQL访问接口

This commit is contained in:
v-zhangjc9
2025-06-05 19:53:25 +08:00
parent a35980a5f4
commit 90fea22de5
6 changed files with 145 additions and 3 deletions

View File

@@ -0,0 +1,28 @@
package com.lanyuanxiaoyao.service.info.controller;
import com.lanyuanxiaoyao.service.info.service.JdbcService;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author lanyuanxiaoyao
* @version 20250605
*/
@RestController
@RequestMapping("/jdbc")
public class JdbcController {
private final JdbcService jdbcService;
public JdbcController(JdbcService jdbcService) {
this.jdbcService = jdbcService;
}
@PostMapping("")
public ImmutableList<ImmutableMap<String, String>> jdbc(@RequestBody String sql) {
return jdbcService.jdbc(sql);
}
}

View File

@@ -0,0 +1,43 @@
package com.lanyuanxiaoyao.service.info.service;
import java.util.List;
import java.util.Map;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
/**
* 执行JDBC
*
* @author lanyuanxiaoyao
* @version 20250605
*/
@Service
public class JdbcService {
private static final Logger logger = LoggerFactory.getLogger(JdbcService.class);
private final JdbcTemplate jdbcTemplate;
public JdbcService(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public ImmutableList<ImmutableMap<String, String>> jdbc(String sql) {
logger.info("Executing SQL: {}", sql);
List<Map<String, Object>> lines = jdbcTemplate.queryForList(sql);
return Lists.immutable.ofAll(lines)
.collect(map -> {
MutableMap<String, String> item = Maps.mutable.empty();
for (Map.Entry<String, Object> entry : map.entrySet()) {
item.put(entry.getKey(), String.valueOf(entry.getValue()));
}
return item.toImmutable();
});
}
}