使用Spring Boot接受HTTP GET/POST请求的一个SQL并返回结果
这里说的意思是:我向我的Web服务器发送一个请求(因为GET请求的一些限制,所以最好使用POST请求,不过这里作为测试还是使用GET请求),请求中带一个sql参数,它对应查询的数据。然后我的Spring Boot服务器会根据这个sql返回对应的结果。
在写到这里的时候我并不知道结果是怎么样的,但是我的经验(虽然是很少的经验)冥冥之中告诉我是可以实现的。
毕竟不是很难。
所以我接下来开始做了。
首先进入:https://start.spring.io/
创建一个com.zifeiy.test的Spring Boot项目,并且包含了依赖:Web、MySQL、MyBatis。

然后我们在Eclipse中导入test项目。
这里跳过我们的MySQL安装过程,所以你在使用之前需要确保已经安装并启动了MySQL服务器,并且有一个名为testdb的database。
然后我们在applications.property文件中对数据库连接进行一下配置:
# DB Configuration
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=password
# logging
logging.level.com.zifeiy.demo=debug
# port
server.port=8092
以上内容有一些信息,包括连接到了本地的MySQL数据库,database为testdb,MySQL的用户名为root,密码为password,项目启动后的端口是8092。
新建一个名为com.zifeiy.test.mapper的package,然后在里面新建一个名为GeneralMapper的interface,暂时不对GeneralMapper做任何更改。
然后我们再新建一个名为com.zifeiy.test.mapper.provider的package,然后建一个名为GeneralMapperProvider的class,内容如下:
package com.zifeiy.test.mapper.provider;
public class GeneralMapperProvider {
public String select(String sql) {
return sql;
}
}
然后我们再回过头去修改GeneralMapper的内容如下:
package com.zifeiy.test.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import com.zifeiy.test.mapper.provider.GeneralMapperProvider;
@Mapper
public interface GeneralMapper {
@SelectProvider(method = "select", type = GeneralMapperProvider.class)
List<Object> select(@Param("sql") String sql);
}
然后新建一个名为com.zifeiy.test.service的package,然后在里面新建一个名为GeneralService的接口:
package com.zifeiy.test.service;
import java.util.List;
public interface GeneralService {
List<Object> select(String sql);
}
然后新建一个名为com.zifeiy.test.service.impl的包,然后在里面新建一个名为GeneralServiceImpl的类,让它实现GeneralService接口:
package com.zifeiy.test.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zifeiy.test.service.GeneralService;
import com.zifeiy.test.mapper.GeneralMapper;
@Service
@Transactional
public class GeneralServiceImpl implements GeneralService {
@Autowired
private GeneralMapper generalMapper;
@Override
public List<Object> select(String sql) {
return this.generalMapper.select(sql);
}
}
然后新建一个名为com.zifeiy.test.controller的package,然后在里面新建一个名为GeneralController的类:
package com.zifeiy.test.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.zifeiy.test.service.GeneralService;
@RestController
@RequestMapping("/")
public class GeneralController {
@Autowired
private GeneralService generalService;
@RequestMapping("/select")
public List<Object> select(@RequestParam(value = "sql", required = true) String sql) {
return this.generalService.select(sql);
}
}
然后我是使用我们的测试数据,首先是第一个链接:
http://localhost:8092/select?sql=select 1
结果如下:

第二个链接:
http://localhost:8092/select?sql=select * from information_schema.tables
结果如下:

发现不对,初步估计是mapper、service、controller中返回的List<Object>有问题,尝试将其改成List<Map<Object,Object>,然后再次运行。
第一个链接:
http://localhost:8092/select?sql=select 1
结果如下:

第二个链接:
http://localhost:8092/select?sql=select * from information_schema.tables
结果如下:

根据结果看来,应该没有问题了。
至此,想要达到的结果已经达到了。
使用Spring Boot接受HTTP GET/POST请求的一个SQL并返回结果的更多相关文章
- Spring boot接受json赋值给java对象
Spring boot接受json赋值给java对象 新建 模板 小书匠 前言 写这个东西,一方面是我自己在做项目的时候,对json的使用还不是十分的熟悉,对spring boot的使用也不是很熟悉, ...
- Java Spring Boot VS .NetCore (一)来一个简单的 Hello World
系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...
- Java Spring Boot VS .NetCore (二)实现一个过滤器Filter
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Spring Boot中如何扩展XML请求和响应的支持
在之前的所有Spring Boot教程中,我们都只提到和用到了针对HTML和JSON格式的请求与响应处理.那么对于XML格式的请求要如何快速的在Controller中包装成对象,以及如何以XML的格式 ...
- Spring Boot中使用AOP记录请求日志
这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...
- 两个spring boot项目war部署到tomcat 其中一个无法正常启动
Spring Boot的spring.jmx资源管理是默认打开的,而两个项目同时使用会冲突 需要在第二个.或者第三个springboot项目中增加如下配置: 1:application.propert ...
- Spring Boot系列教程二:创建第一个web工程 hello world
一.创建工程 创建名称为"springboot_helloworld"的spring boot工程, new->Spring Starter Project,直接上图 ...
- Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)
Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- spring Boot + MyBatis + Maven 项目,日志开启打印 sql
在 spring Boot + MyBatis + Maven 项目中,日志开启打印 sql 的最简单方法,就是在文件 application.properties 中新增: logging.leve ...
随机推荐
- HDU - 4507 - 吉哥系列故事——恨7不成妻(数位DP,数学)
链接: https://vjudge.net/problem/HDU-4507 题意: 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都 ...
- [Kubernetes] Pod Health
Kubernetes relies on Probes to determine the health of a Pod container. A Probe is a diagnostic perf ...
- SSM整合Dubbo案例
一.Consumer子类 MyController类 @Controller @RequestMapping("/login") public class MyController ...
- PHP 鸟哥:我也曾经是“不适合”编程的人
网名:雪候鸟,大家尊称鸟哥,惠新宸 @Laruence, 是国内最有影响力的 PHP 技术专家,PHP 开发组核心成员,PECL 开发者,Zend 公司外聘顾问.他曾供职于雅虎,百度,现在新浪微博任平 ...
- LibreOJ #524. 「LibreOJ β Round #4」游戏
二次联通门 : LibreOJ #524. 「LibreOJ β Round #4」游戏 /* LibreOJ #524. 「LibreOJ β Round #4」游戏 找找规律就会发现.. 当有X的 ...
- LibreOJ #6212. 「美团 CodeM 决赛」melon
二次联通门 : LibreOJ #6212. 「美团 CodeM 决赛」melon /* LibreOJ #6212. 「美团 CodeM 决赛」melon MDZZ 这是决赛题?? */ #incl ...
- (8)打鸡儿教你Vue.js
监听属性 监听属性 watch 通过 watch 来响应数据的变化 <div id = "app"> <p style = "font-size:25p ...
- ehcache.xml 配置文件备忘录(不建议出现中文注释,此处备忘)
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...
- Spring|IOC启动流程
1.IOC启动流程 IOC的启动流程分为两个阶段,第一阶段是容器的启动阶段,第二阶段是Bean实例化阶段. 容器的启动阶段:加载配置信息,分析配置信息,其他 Bean实例化阶段:实例化对象,装配依赖, ...
- windows上hexo: command not found
使用hexo写博客已经有好几个月了,今天突然出现hexo: command not found,应该与我白天的时候调一下环境变量等有关.在对应的path添加环境变量,即可解决该问题.我的环境变量路径为 ...