|
@@ -0,0 +1,299 @@
|
|
|
+package com.qmth.teachcloud.data;
|
|
|
+
|
|
|
+import com.alibaba.druid.pool.DruidDataSource;
|
|
|
+import com.qmth.teachcloud.common.bean.result.SysConfigResult;
|
|
|
+import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
|
+import com.qmth.teachcloud.common.entity.SysConfig;
|
|
|
+import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
+import com.qmth.teachcloud.common.service.CommonCacheService;
|
|
|
+import com.qmth.teachcloud.data.vo.*;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.DriverManager;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class DataUtil {
|
|
|
+
|
|
|
+ // 一页1000条
|
|
|
+ private static int PAGE_SIZE = 1000;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private CommonCacheService commonCacheService;
|
|
|
+
|
|
|
+ public List<OrgDataVo> listOrg(JdbcTemplate jdbcTemplate) {
|
|
|
+ //sql语句
|
|
|
+ String sql = "select * from sys_user";
|
|
|
+ return query(jdbcTemplate, sql, OrgDataVo.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<UserDataVo> listUser(JdbcTemplate jdbcTemplate) {
|
|
|
+ //sql语句
|
|
|
+ String sql = "select * from sys_user limit ? ?";
|
|
|
+ return page(jdbcTemplate, sql, UserDataVo.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<CourseDataVo> listCourse(JdbcTemplate jdbcTemplate) {
|
|
|
+ //sql语句
|
|
|
+ String sql = "select * from sys_user";
|
|
|
+ return query(jdbcTemplate, sql, CourseDataVo.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<ExamTaskDataVo> listExamTask(JdbcTemplate jdbcTemplate) {
|
|
|
+ //sql语句
|
|
|
+ String sql = "select * from sys_user";
|
|
|
+ return query(jdbcTemplate, sql, ExamTaskDataVo.class);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 不分页
|
|
|
+ *
|
|
|
+ * @param sql
|
|
|
+ */
|
|
|
+ private <T> List<T> query(JdbcTemplate jdbcTemplate, String sql, Class<T> tClass) {
|
|
|
+ if (sql.endsWith("limit ? ?")) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("sql中设置了分页参数");
|
|
|
+ }
|
|
|
+ List<T> list;
|
|
|
+ try {
|
|
|
+ list = jdbcTemplate.queryForList(sql, tClass);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("查询失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页
|
|
|
+ *
|
|
|
+ * @param sql
|
|
|
+ */
|
|
|
+ private <T> List<T> page(JdbcTemplate jdbcTemplate, String sql, Class<T> tClass) {
|
|
|
+ if (!sql.endsWith("limit ? ?")) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("sql中未设置分页参数");
|
|
|
+ }
|
|
|
+ //将查询的语句封装到List集合中,集合中存储每一个员工实体
|
|
|
+ List<T> listAll = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ List<T> list;
|
|
|
+ int pageNumber = 0;
|
|
|
+ do {
|
|
|
+ int offset = pageNumber * PAGE_SIZE + PAGE_SIZE;
|
|
|
+ list = jdbcTemplate.queryForList(sql, tClass, pageNumber, offset);
|
|
|
+ if (CollectionUtils.isNotEmpty(list)) {
|
|
|
+ listAll.addAll(list);
|
|
|
+ pageNumber++;
|
|
|
+ }
|
|
|
+ } while (CollectionUtils.isNotEmpty(list));
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("查询失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ return listAll;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 不分页
|
|
|
+ *
|
|
|
+ * @param sql
|
|
|
+ */
|
|
|
+ /*private <T> List<T> query(String sql, Class<T> tClass) {
|
|
|
+ if (sql.endsWith("limit ? ?")) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("sql中设置了分页参数");
|
|
|
+ }
|
|
|
+ //创建QueryRunner
|
|
|
+ QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource(createDataSourceMap()));
|
|
|
+ List<T> list;
|
|
|
+ try {
|
|
|
+ list = qr.query(sql, new BeanListHandler<>(tClass));
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("查询失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ */
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页
|
|
|
+ *//*
|
|
|
+ private <T> List<T> page(String sql, Class<T> tClass) {
|
|
|
+ if (!sql.endsWith("limit ? ?")) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("sql中未设置分页参数");
|
|
|
+ }
|
|
|
+ //创建QueryRunner
|
|
|
+ QueryRunner qr = new QueryRunner(DruidJdbcUtils.getDataSource(createDataSourceMap()));
|
|
|
+ //将查询的语句封装到List集合中,集合中存储每一个员工实体
|
|
|
+ List<T> listAll = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ List<T> list;
|
|
|
+ int pageNumber = 0;
|
|
|
+ do {
|
|
|
+ int offset = pageNumber * PAGE_SIZE + PAGE_SIZE;
|
|
|
+ list = qr.query(sql, new BeanListHandler<>(tClass), pageNumber, offset);
|
|
|
+ if (CollectionUtils.isNotEmpty(list)) {
|
|
|
+ listAll.addAll(list);
|
|
|
+ pageNumber++;
|
|
|
+ }
|
|
|
+ } while (CollectionUtils.isNotEmpty(list));
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("查询失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ return listAll;
|
|
|
+ }*/
|
|
|
+ public DataSource createDataSourceMap(Long schoolId) {
|
|
|
+ SysConfig datasourceType = commonCacheService.addSysConfigCache(schoolId, SystemConstant.DATA_DATASOURCE_TYPE);
|
|
|
+ if (datasourceType == null || StringUtils.isBlank(datasourceType.getConfigValue())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置数据库类型");
|
|
|
+ }
|
|
|
+ // 数据库HOST
|
|
|
+ SysConfig datasourceHost = commonCacheService.addSysConfigCache(schoolId, SystemConstant.DATA_DATASOURCE_HOST);
|
|
|
+ if (datasourceHost == null || StringUtils.isBlank(datasourceHost.getConfigValue())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置HOST");
|
|
|
+ }
|
|
|
+ // 数据库PORT
|
|
|
+ SysConfig datasourcePort = commonCacheService.addSysConfigCache(schoolId, SystemConstant.DATA_DATASOURCE_PORT);
|
|
|
+ if (datasourcePort == null || StringUtils.isBlank(datasourcePort.getConfigValue())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置端口");
|
|
|
+ }
|
|
|
+ // 数据库DataName
|
|
|
+ SysConfig datasourceDataName = commonCacheService.addSysConfigCache(schoolId, SystemConstant.DATA_DATASOURCE_DATANAME);
|
|
|
+ if (datasourceDataName == null || StringUtils.isBlank(datasourceDataName.getConfigValue())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置数据库名");
|
|
|
+ }
|
|
|
+ // 数据库用户名
|
|
|
+ SysConfig datasourceUserName = commonCacheService.addSysConfigCache(schoolId, SystemConstant.DATA_DATASOURCE_USERNAME);
|
|
|
+ if (datasourceUserName == null || StringUtils.isBlank(datasourceUserName.getConfigValue())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置用户名");
|
|
|
+ }
|
|
|
+ String userName = datasourceUserName.getConfigValue().trim();
|
|
|
+ // 数据库密码
|
|
|
+ SysConfig datasourcePassword = commonCacheService.addSysConfigCache(schoolId, SystemConstant.DATA_DATASOURCE_PASSWORD);
|
|
|
+ if (datasourcePassword == null || StringUtils.isBlank(datasourcePassword.getConfigValue())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置密码");
|
|
|
+ }
|
|
|
+ String password = datasourcePassword.getConfigValue().trim();
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+
|
|
|
+ String driverClassName;
|
|
|
+ String url;
|
|
|
+ if ("ORACLE".equals(datasourceType.getConfigValue())) {
|
|
|
+ driverClassName = "com.mysql.jdbc.Driver";
|
|
|
+ url = "jdbc:oracle:thin:@" + datasourceHost.getConfigValue().trim() + ":" + datasourcePort.getConfigValue().trim() + ":" + datasourceDataName.getConfigValue().trim();
|
|
|
+ } else if ("MYSQL".equals(datasourceType.getConfigValue())) {
|
|
|
+ driverClassName = "com.mysql.jdbc.Driver";
|
|
|
+ url = "jdbc:mysql://" + datasourceHost.getConfigValue().trim() + ":" + datasourcePort.getConfigValue().trim() + "/" + datasourceDataName.getConfigValue().trim() + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
|
|
|
+ } else {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("只支持Oracle和Mysql数据库");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Class.forName(driverClassName);
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ return new DriverManagerDataSource(url, userName, password);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public DataSource createDataSourceMap(List<SysConfigResult> params) {
|
|
|
+ SysConfigResult datasourceType = params.stream().filter(m->m.getCode().equals(SystemConstant.DATA_DATASOURCE_TYPE)).findFirst().orElse(null);
|
|
|
+ if (datasourceType == null || StringUtils.isBlank(datasourceType.getValue().toString())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置数据库类型");
|
|
|
+ }
|
|
|
+ String type = datasourceType.getValue().toString().trim();
|
|
|
+ // 数据库HOST
|
|
|
+ SysConfigResult datasourceHost = params.stream().filter(m->m.getCode().equals(SystemConstant.DATA_DATASOURCE_HOST)).findFirst().orElse(null);
|
|
|
+ if (datasourceHost == null || StringUtils.isBlank(datasourceHost.getValue().toString())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置HOST");
|
|
|
+ }
|
|
|
+ String host = datasourceHost.getValue().toString().trim();
|
|
|
+ // 数据库PORT
|
|
|
+ SysConfigResult datasourcePort = params.stream().filter(m->m.getCode().equals(SystemConstant.DATA_DATASOURCE_PORT)).findFirst().orElse(null);
|
|
|
+ if (datasourcePort == null || StringUtils.isBlank(datasourcePort.getValue().toString())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置端口");
|
|
|
+ }
|
|
|
+ String port = datasourcePort.getValue().toString().trim();
|
|
|
+ // 数据库DataName
|
|
|
+ SysConfigResult datasourceDataName = params.stream().filter(m->m.getCode().equals(SystemConstant.DATA_DATASOURCE_DATANAME)).findFirst().orElse(null);
|
|
|
+ if (datasourceDataName == null || StringUtils.isBlank(datasourceDataName.getValue().toString())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置数据库名");
|
|
|
+ }
|
|
|
+ String dataName = datasourceDataName.getValue().toString().trim();
|
|
|
+ // 数据库用户名
|
|
|
+ SysConfigResult datasourceUserName = params.stream().filter(m->m.getCode().equals(SystemConstant.DATA_DATASOURCE_USERNAME)).findFirst().orElse(null);
|
|
|
+ if (datasourceUserName == null || StringUtils.isBlank(datasourceUserName.getValue().toString())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置用户名");
|
|
|
+ }
|
|
|
+ String userName = datasourceUserName.getValue().toString().trim();
|
|
|
+ // 数据库密码
|
|
|
+ SysConfigResult datasourcePassword = params.stream().filter(m->m.getCode().equals(SystemConstant.DATA_DATASOURCE_PASSWORD)).findFirst().orElse(null);
|
|
|
+ if (datasourcePassword == null || StringUtils.isBlank(datasourcePassword.getValue().toString())) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("未设置密码");
|
|
|
+ }
|
|
|
+ String password = datasourcePassword.getValue().toString().trim();
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+
|
|
|
+ String driverClassName;
|
|
|
+ String url;
|
|
|
+ if ("ORACLE".equals(type)) {
|
|
|
+ driverClassName = "com.mysql.jdbc.Driver";
|
|
|
+ url = "jdbc:oracle:thin:@" + host + ":" + port + ":" + dataName;
|
|
|
+ } else if ("MYSQL".equals(type)) {
|
|
|
+ driverClassName = "com.mysql.jdbc.Driver";
|
|
|
+ url = "jdbc:mysql://" + host + ":" + port + "/" + dataName + "?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&nullCatalogMeansCurrent=true";
|
|
|
+ } else {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("只支持Oracle和Mysql数据库");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Class.forName(driverClassName);
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ return new DriverManagerDataSource(url, userName, password);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ sqlConn("0", "localhost", "3306", "teachcloud-3.4.3", "root", "12345678");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String sqlConn(String databaseType, String address, String port, String databaseName, String userName, String password) {
|
|
|
+ String driverName = null;
|
|
|
+ String url = null;
|
|
|
+ if ("0".equals(databaseType)) {
|
|
|
+ //mysql
|
|
|
+ driverName = "com.mysql.cj.jdbc.Driver";
|
|
|
+ url = "jdbc:mysql://" + address + ":" + port + "/" + databaseName + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
|
|
|
+ } else if ("1".equals(databaseType)) {
|
|
|
+ //oracle
|
|
|
+ driverName = "oracle.jdbc.driver.OracleDriver";
|
|
|
+ url = "jdbc:oracle:thin:@" + address + ":" + port + ":" + databaseName;
|
|
|
+ } else if ("2".equals(databaseType)) {
|
|
|
+ //sqlserver
|
|
|
+ driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
|
|
|
+ url = "jdbc:sqlserver://" + address + ":" + port + ";DatabaseName=" + databaseName;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Class.forName(driverName);
|
|
|
+ Connection connection = DriverManager.getConnection(url, userName, password);
|
|
|
+ connection.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return "测试失败";
|
|
|
+ }
|
|
|
+ return "测试成功";
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<ExamStudentDataVo> listExamStudent(JdbcTemplate jdbcTemplate) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|