這里說的意思是:我向我的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
結果如下:
根據結果看來,應該沒有問題了。
至此,想要達到的結果已經達到了。