|
|
package com.rehome.mqttclienttemperature.controller;
|
|
|
|
|
|
|
|
|
import com.rehome.mqttclienttemperature.datasource.DataSource;
|
|
|
import com.rehome.mqttclienttemperature.dto.ResponseDto;
|
|
|
import com.rehome.mqttclienttemperature.utils.JdbcUtil;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* 背景
|
|
|
* 现在的数据层的开发,大多会使用如MyBatis或JPA之类的开发工具。这些开发工具给我们的开发过程中带来了极大的便利。
|
|
|
* 但是在一些极端的场景下往往原生的jdbc方式操作数据库更灵活,性能更高。由于部分场景下MyBatis或JPA之类无法满足我的需求,所以我打算自己封装一套查数据库的工具类。
|
|
|
*
|
|
|
* 我们会用到fastjson,druid,mysql所以pom.xml增加依赖如下:
|
|
|
*
|
|
|
* <dependency>
|
|
|
* <groupId>com.alibaba</groupId>
|
|
|
* <artifactId>fastjson</artifactId>
|
|
|
* <version>1.2.62</version>
|
|
|
* </dependency>
|
|
|
* <dependency>
|
|
|
* <groupId>com.alibaba</groupId>
|
|
|
* <artifactId>druid</artifactId>
|
|
|
* <version>1.1.9</version>
|
|
|
* </dependency>
|
|
|
* <!-- Mysql驱动包 -->
|
|
|
* <dependency>
|
|
|
* <groupId>mysql</groupId>
|
|
|
* <artifactId>mysql-connector-java</artifactId>
|
|
|
* </dependency>
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
*源码下载
|
|
|
* https://download.csdn.net/download/lxyoucan/85094574
|
|
|
*
|
|
|
* 参考
|
|
|
* https://github.com/freakchick/DBApi
|
|
|
*
|
|
|
*SpringBoot中封装jdbc工具类
|
|
|
* https://blog.csdn.net/lxyoucan/article/details/124042295
|
|
|
*
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
@RestController
|
|
|
public class JdbcDemoController {
|
|
|
public static DataSource ds = new DataSource();
|
|
|
static {
|
|
|
//配置数据源
|
|
|
ds.setId("1");
|
|
|
ds.setName("mysql");
|
|
|
//ds.setUrl("jdbc:mysql://192.168.3.7:3306/appserver?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2B8");
|
|
|
ds.setUrl("jdbc:mysql://192.168.3.7:3306/appserver?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true");
|
|
|
ds.setUsername("root");
|
|
|
ds.setPassword("Skyinno251,");
|
|
|
ds.setDriver("com.mysql.cj.jdbc.Driver");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询测试
|
|
|
* @return
|
|
|
*/
|
|
|
//@RequestMapping("/api/list")
|
|
|
public ResponseDto queryList()
|
|
|
{
|
|
|
String sql = "show tables";
|
|
|
ResponseDto responseDto = JdbcUtil.executeSql(ds,sql);
|
|
|
return responseDto;
|
|
|
}
|
|
|
}
|
|
|
|