使用Spring Boot接受HTTP GET/POST请求的一个SQL并返回结果

这里说的意思是:我向我的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

结果如下:

根据结果看来,应该没有问题了。

至此,想要达到的结果已经达到了。

posted @ 2019-01-25 09:59  zifeiy  阅读(1701)  评论(0编辑  收藏  举报