From b32202b1c4804b124801848cab74a310ab9e0bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AF=86=E7=A0=81=E6=BC=8F=E6=B4=9E?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=8C=E6=88=90?= <> Date: Mon, 9 Dec 2024 23:14:00 +0800 Subject: [PATCH] add jdbc --- pom.xml | 7 ++ .../controller/JdbcDemoController.java | 75 ++++++++++++++++ .../datasource/DataSource.java | 18 ++++ .../dto/ResponseDto.java | 43 ++++++++++ .../service/ScheduledService.java | 20 +++++ .../mqttclienttemperature/utils/JdbcUtil.java | 85 +++++++++++++++++++ .../utils/PoolManager.java | 81 ++++++++++++++++++ 7 files changed, 329 insertions(+) create mode 100644 src/main/java/com/rehome/mqttclienttemperature/controller/JdbcDemoController.java create mode 100644 src/main/java/com/rehome/mqttclienttemperature/datasource/DataSource.java create mode 100644 src/main/java/com/rehome/mqttclienttemperature/dto/ResponseDto.java create mode 100644 src/main/java/com/rehome/mqttclienttemperature/utils/JdbcUtil.java create mode 100644 src/main/java/com/rehome/mqttclienttemperature/utils/PoolManager.java diff --git a/pom.xml b/pom.xml index e8a973e..caaef57 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,13 @@ mysql-connector-java runtime + + + com.alibaba + druid + 1.1.9 + + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/com/rehome/mqttclienttemperature/controller/JdbcDemoController.java b/src/main/java/com/rehome/mqttclienttemperature/controller/JdbcDemoController.java new file mode 100644 index 0000000..b89c609 --- /dev/null +++ b/src/main/java/com/rehome/mqttclienttemperature/controller/JdbcDemoController.java @@ -0,0 +1,75 @@ +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增加依赖如下: + * + * + * com.alibaba + * fastjson + * 1.2.62 + * + * + * com.alibaba + * druid + * 1.1.9 + * + * + * + * mysql + * mysql-connector-java + * + * + */ + +/** + *源码下载 + * 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; + } +} + diff --git a/src/main/java/com/rehome/mqttclienttemperature/datasource/DataSource.java b/src/main/java/com/rehome/mqttclienttemperature/datasource/DataSource.java new file mode 100644 index 0000000..5629268 --- /dev/null +++ b/src/main/java/com/rehome/mqttclienttemperature/datasource/DataSource.java @@ -0,0 +1,18 @@ +package com.rehome.mqttclienttemperature.datasource; + + +import lombok.Data; + +/** + * 数据源实体类 + * + */ +@Data +public class DataSource { + String id; + String name; + String url; + String username; + String password; + String driver; +} diff --git a/src/main/java/com/rehome/mqttclienttemperature/dto/ResponseDto.java b/src/main/java/com/rehome/mqttclienttemperature/dto/ResponseDto.java new file mode 100644 index 0000000..e758d29 --- /dev/null +++ b/src/main/java/com/rehome/mqttclienttemperature/dto/ResponseDto.java @@ -0,0 +1,43 @@ +package com.rehome.mqttclienttemperature.dto; + + +import lombok.Data; +/** + * 返回值包装类 + */ +@Data +public class ResponseDto { + String msg; + Object data; + boolean success; + public static ResponseDto apiSuccess(Object data) { + ResponseDto dto = new ResponseDto(); + dto.setData(data); + dto.setSuccess(true); + dto.setMsg("接口访问成功"); + return dto; + } + + public static ResponseDto successWithMsg(String msg) { + ResponseDto dto = new ResponseDto(); + dto.setData(null); + dto.setSuccess(true); + dto.setMsg(msg); + return dto; + } + + public static ResponseDto successWithData(Object data) { + ResponseDto dto = new ResponseDto(); + dto.setData(data); + dto.setSuccess(true); + return dto; + } + + public static ResponseDto fail(String msg) { + ResponseDto dto = new ResponseDto(); + dto.setSuccess(false); + dto.setMsg(msg); + return dto; + } +} + diff --git a/src/main/java/com/rehome/mqttclienttemperature/service/ScheduledService.java b/src/main/java/com/rehome/mqttclienttemperature/service/ScheduledService.java index 22c3c69..850344f 100644 --- a/src/main/java/com/rehome/mqttclienttemperature/service/ScheduledService.java +++ b/src/main/java/com/rehome/mqttclienttemperature/service/ScheduledService.java @@ -2,8 +2,14 @@ package com.rehome.mqttclienttemperature.service; +import com.google.gson.Gson; +import com.rehome.mqttclienttemperature.controller.JdbcDemoController; +import com.rehome.mqttclienttemperature.dto.ResponseDto; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import javax.annotation.Resource; + /** * @author huangwenfei @@ -14,4 +20,18 @@ import org.springframework.stereotype.Component; @Component public class ScheduledService { + @Resource + private JdbcDemoController jdbcDemoController; + + /** + * @date 2022-03-16 09:41 + * @description: 从中央气象台获取省份列表 + * @Param: null + */ + @Scheduled(cron = "0/10 * * * * *") + public void getNmcWeatherProvince() { + ResponseDto responseDto = jdbcDemoController.queryList(); + System.out.println(new Gson().toJson(responseDto)); + } + } \ No newline at end of file diff --git a/src/main/java/com/rehome/mqttclienttemperature/utils/JdbcUtil.java b/src/main/java/com/rehome/mqttclienttemperature/utils/JdbcUtil.java new file mode 100644 index 0000000..4399eed --- /dev/null +++ b/src/main/java/com/rehome/mqttclienttemperature/utils/JdbcUtil.java @@ -0,0 +1,85 @@ +package com.rehome.mqttclienttemperature.utils; + + +import com.alibaba.druid.pool.DruidPooledConnection; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.rehome.mqttclienttemperature.datasource.DataSource; +import com.rehome.mqttclienttemperature.dto.ResponseDto; +import lombok.extern.slf4j.Slf4j; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +@Slf4j +public class JdbcUtil { + /** + * 执行sql并返回结果 + * + * @param datasource 数据源连接 + * @param sql 语句 + */ + public static ResponseDto executeSql(DataSource datasource, String sql) { + return executeSql(datasource,sql,new ArrayList()); + } + + /** + * 执行sql并返回结果 + * + * @param datasource 数据源连接 + * @param sql 语句 + * @param jdbcParamValues + */ + public static ResponseDto executeSql(DataSource datasource, String sql, List jdbcParamValues) { + log.info(sql); + log.info(JSON.toJSONString(jdbcParamValues)); + DruidPooledConnection connection = null; + try { + connection = PoolManager.getPooledConnection(datasource); + PreparedStatement statement = connection.prepareStatement(sql); + for (int i = 1; i <= jdbcParamValues.size(); i++) { + statement.setObject(i, jdbcParamValues.get(i - 1)); + } + boolean hasResultSet = statement.execute(); + if (hasResultSet) { + ResultSet rs = statement.getResultSet(); + int columnCount = rs.getMetaData().getColumnCount(); + List columns = new ArrayList<>(); + for (int i = 1; i <= columnCount; i++) { + String columnName = rs.getMetaData().getColumnLabel(i); + columns.add(columnName); + } + List list = new ArrayList<>(); + while (rs.next()) { + JSONObject jo = new JSONObject(); + columns.stream().forEach(t -> { + try { + Object value = rs.getObject(t); + jo.put(t, value); + + } catch (SQLException e) { + e.printStackTrace(); + } + }); + list.add(jo); + } + return ResponseDto.apiSuccess(list); + } else { + int updateCount = statement.getUpdateCount(); + return ResponseDto.apiSuccess("sql修改数据行数:" + updateCount); + } + } catch (Exception e) { + e.printStackTrace(); + return ResponseDto.fail(e.getMessage()); + } finally { + try { + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/main/java/com/rehome/mqttclienttemperature/utils/PoolManager.java b/src/main/java/com/rehome/mqttclienttemperature/utils/PoolManager.java new file mode 100644 index 0000000..f281c9e --- /dev/null +++ b/src/main/java/com/rehome/mqttclienttemperature/utils/PoolManager.java @@ -0,0 +1,81 @@ +package com.rehome.mqttclienttemperature.utils; + + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.DruidPooledConnection; +import com.rehome.mqttclienttemperature.datasource.DataSource; +import lombok.extern.slf4j.Slf4j; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + + +/** + * 数据库连接池工具类 + */ +@Slf4j +public class PoolManager { + + private static Lock lock = new ReentrantLock(); + + private static Lock deleteLock = new ReentrantLock(); + + //所有数据源的连接池存在map里 + static Map map = new HashMap<>(); + + public static DruidDataSource getJdbcConnectionPool(DataSource ds) { + if (map.containsKey(ds.getId())) { + return map.get(ds.getId()); + } else { + lock.lock(); + try { + log.info(Thread.currentThread().getName() + "获取锁"); + if (!map.containsKey(ds.getId())) { + DruidDataSource druidDataSource = new DruidDataSource(); + druidDataSource.setName(ds.getName()); + druidDataSource.setUrl(ds.getUrl()); + druidDataSource.setUsername(ds.getUsername()); + druidDataSource.setPassword(ds.getPassword()); + druidDataSource.setDriverClassName(ds.getDriver()); + druidDataSource.setConnectionErrorRetryAttempts(3); //失败后重连次数 + druidDataSource.setBreakAfterAcquireFailure(true); + + map.put(ds.getId(), druidDataSource); + log.info("创建Druid连接池成功:{}", ds.getName()); + } + return map.get(ds.getId()); + } catch (Exception e) { + return null; + } finally { + lock.unlock(); + } + } + } + + //删除数据库连接池 + public static void removeJdbcConnectionPool(String id) { + deleteLock.lock(); + try { + DruidDataSource druidDataSource = map.get(id); + if (druidDataSource != null) { + druidDataSource.close(); + map.remove(id); + } + } catch (Exception e) { + log.error(e.toString()); + } finally { + deleteLock.unlock(); + } + + } + + public static DruidPooledConnection getPooledConnection(DataSource ds) throws SQLException { + DruidDataSource pool = PoolManager.getJdbcConnectionPool(ds); + DruidPooledConnection connection = pool.getConnection(); +// log.info("获取连接成功"); + return connection; + } +}