这里说的意思是:我向我的Web服务器发送一个请求(因为GET请求的一些限制,所以最好使用POST请求,不过这里作为测试还是使用GET请求),请求中带一个sql参数,它对应查询的数据。然后我的Spring Boot服务器会根据这个sql返回对应的结果。

在写到这里的时候我并不知道结果是怎么样的,但是我的经验(虽然是很少的经验)冥冥之中告诉我是可以实现的。

毕竟不是很难。

所以我接下来开始做了。

首先进入:https://start.spring.io/

创建一个com.zifeiy.test的Spring Boot项目,并且包含了依赖:WebMySQLMyBatis

然后我们在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并返回结果的更多相关文章

  1. Spring boot接受json赋值给java对象

    Spring boot接受json赋值给java对象 新建 模板 小书匠 前言 写这个东西,一方面是我自己在做项目的时候,对json的使用还不是十分的熟悉,对spring boot的使用也不是很熟悉, ...

  2. Java Spring Boot VS .NetCore (一)来一个简单的 Hello World

    系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...

  3. Java Spring Boot VS .NetCore (二)实现一个过滤器Filter

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  4. Spring Boot中如何扩展XML请求和响应的支持

    在之前的所有Spring Boot教程中,我们都只提到和用到了针对HTML和JSON格式的请求与响应处理.那么对于XML格式的请求要如何快速的在Controller中包装成对象,以及如何以XML的格式 ...

  5. Spring Boot中使用AOP记录请求日志

    这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...

  6. 两个spring boot项目war部署到tomcat 其中一个无法正常启动

    Spring Boot的spring.jmx资源管理是默认打开的,而两个项目同时使用会冲突 需要在第二个.或者第三个springboot项目中增加如下配置: 1:application.propert ...

  7. Spring Boot系列教程二:创建第一个web工程 hello world

    一.创建工程 创建名称为"springboot_helloworld"的spring boot工程, new->Spring Starter Project,直接上图     ...

  8. Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)

    Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  9. spring Boot + MyBatis + Maven 项目,日志开启打印 sql

    在 spring Boot + MyBatis + Maven 项目中,日志开启打印 sql 的最简单方法,就是在文件 application.properties 中新增: logging.leve ...

随机推荐

  1. SQL Server 默认跟踪(Default Trace)介绍使用

    背景 当数据库的表.存储过程经常别修改,当这些修改造成BUG的时候,很多开发都不承认是他们干的,那我们有没办法找出谁干的呢? SQL Server有Default Trace默认跟踪,数据库记录信息到 ...

  2. spring-data-jpa一对多多对一多对多关联

    一对多.多对一 Country类 @Entity @Table(name = "Country") public class Country { @Id //sequence id ...

  3. zabbix基本介绍

    来源是 觅安教育 大家有兴趣可以去哔哩哔哩搜搜. Open-falcon是由小米公司开源 比如windows,linux,unix,openBSD,AIX,solaris,Mac等操作系统,都可以安装 ...

  4. 配置asgi来达到能处理websocket

    在项目中使用了webscoket进行实时通讯,但是生产环境又使用了django+nginx+uwsgi的部署方式,我们都知道uwsgi并不能处理websocket请求,所以需要asgi服务器来处理we ...

  5. AtCoder Grand Contest 021题解

    传送门 \(A\) 咕咕 ll n,res;bool fl; int main(){ scanf("%lld",&n),fl=1; while(n>9)res+=9, ...

  6. 【MySQL 读书笔记】“order by”是怎么工作的?

    针对排序来说,order by 是我们使用非常频繁的关键字.结合之前我们对索引的了解再来看这篇文章会让我们深刻理解在排序的时候,是如何利用索引来达到少扫描表或者使用外部排序的. 先定义一个表辅助我们后 ...

  7. linux 常用性能优化

    linux 常用性能优化 1. 优化内核相关参数 配置文件/etc/sysctl.conf 配置方法直接将参数添加进文件每条一行. sysctl -a可以查看默认配置 sysctl -p 执行并检测是 ...

  8. 使用docker安装wazuh

    使用docker安装wazuh centos下安装wazuh 官方文档: https://documentation.wazuh.com/3.9/installation-guide/installi ...

  9. Oracle,regexp_replace函数,replace函数

    replace函数(不知支持正则表达式)语法: replace(原字段,“原字段旧内容“,“原字段新内容“,) select replace(原字段,'原字段旧内容','原字段新内容') from T ...

  10. Pytest权威教程01-安装及入门

    目录 安装及入门 安装 Pytest 创建你的第一个测试用例 执行多条测试用例 断言抛出了指定异常 使用类组织多条测试用例 函数测试中请求使用独立的临时目录 进一步阅读 返回: Pytest权威教程 ...