1.数据库配置类
package com.zdlt.auth.api.common.druid; import java.util.Properties; import static com.alibaba.druid.pool.DruidDataSourceFactory.*; /** * 数据库配置类 * * @author shiyanjun * @since 2019-12-15 */ public class DbConfig { /** * 获取数据库配置 * * @return 配置信息 */ public static Properties getDbConfig() { Properties properties = new Properties(); properties.put(PROP_URL, "jdbc:mysql://localhost:3306/authapi"); properties.put(PROP_USERNAME, "root"); properties.put(PROP_PASSWORD, "123456"); properties.put(PROP_INITIALSIZE, "5"); properties.put(PROP_MINIDLE, "5"); properties.put(PROP_MAXACTIVE, "10"); properties.put(PROP_MAXWAIT, "10000"); return properties; } }
2.数据库操作工具类
package com.zdlt.auth.api.common.druid; import lombok.extern.slf4j.Slf4j; import javax.sql.DataSource; import java.sql.*; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** * 数据库操作工具类 * * @author shiyanjun * @since 2019-12-15 */ @Slf4j public class DbUtil { /** * 获取数据库连接 * * @param dataSource 数据源 * @return 连接 * @throws SQLException */ public static Connection getConnection(DataSource dataSource) throws SQLException { return dataSource.getConnection(); } /** * 获取模型项实例数据源 * * @return 数据源 * @throws Exception */ public static DataSource getDMInstanceDataSource() { return DMInstanceDb.INSTANCE.getDataSource(); } /** * 执行查询SQL语句 * * @param dataSource 数据源 * @param querySql 查询SQL语句 * @return 查询结果 */ public static List<Map<String, Object>> executeQuery(DataSource dataSource, String querySql) { if (dataSource == null) { throw new RuntimeException("DataSource not be null!"); } if (querySql == null || querySql.trim().length() == 0) { throw new RuntimeException("Query SQL not be empty!"); } List<Map<String, Object>> resultList = new ArrayList<>(); Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { connection = DbUtil.getConnection(dataSource); statement = connection.createStatement(); resultSet = statement.executeQuery(querySql); ResultSetMetaData metaData = resultSet.getMetaData(); resultList = getResultMap(resultSet, metaData); } catch (SQLException e) { e.printStackTrace(); } finally { DbUtil.closeResource(resultSet, statement, connection); } return resultList; } /** * 解析结果集 * * @param rs 结果集 * @param md 结果集元数据 * @return 表数据 * @throws SQLException */ private static List<Map<String, Object>> getResultMap(ResultSet rs, ResultSetMetaData md) throws SQLException { List<Map<String, Object>> resultList = new ArrayList<>(); int columnCount = md.getColumnCount(); while (rs.next()) { Map<String, Object> resultMap = new LinkedHashMap<>(); for (int i = 1; i <= columnCount; i++) { String columnName = md.getColumnLabel(i); Object columnVal = getType(rs, md, columnName, i); resultMap.put(columnName, columnVal); } resultList.add(resultMap); } return resultList; } /** * 根据字段名称和字段类型获取字段的值 * * @param rs 结果集 * @param md 结果集元数据 * @param columnName 字段名称 * @param index 字段序号 * @return 字段的值 * @throws SQLException */ private static Object getType(ResultSet rs, ResultSetMetaData md, String columnName, int index) throws SQLException { int columnType = md.getColumnType(index); switch (columnType) { case Types.ARRAY: return rs.getArray(columnName); case Types.BIGINT: return rs.getInt(columnName); case Types.BOOLEAN: return rs.getBoolean(columnName); case Types.BLOB: return rs.getBlob(columnName); case Types.DOUBLE: return rs.getDouble(columnName); case Types.FLOAT: return rs.getFloat(columnName); case Types.INTEGER: return rs.getInt(columnName); case Types.NVARCHAR: return rs.getNString(columnName); case Types.VARCHAR: return rs.getString(columnName); case Types.TINYINT: return rs.getInt(columnName); case Types.SMALLINT: return rs.getInt(columnName); case Types.DATE: return rs.getDate(columnName); case Types.TIMESTAMP: return rs.getTimestamp(columnName); default: return rs.getObject(columnName); } } /** * 关闭资源 * * @param statement 语句执行器 * @param connection 连接 */ public static void closeResource(Statement statement, Connection connection) { closeStatement(statement); closeConnection(connection); } /** * 关闭资源 * * @param resultSet 结果集 * @param statement 语句执行器 * @param connection 连接 */ public static void closeResource(ResultSet resultSet, Statement statement, Connection connection) { closeResultSet(resultSet); closeStatement(statement); closeConnection(connection); } /** * 关闭结果集 * * @param resultSet 结果集 */ public static void closeResultSet(ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 关闭语句执行器 * * @param statement 语句执行器 */ public static void closeStatement(Statement statement) { if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 关闭连接 * * @param connection 连接 */ public static void closeConnection(Connection connection) { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
3.数据库业务枚举单例
package com.zdlt.auth.api.common.druid; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.alibaba.fastjson.JSON; import javax.sql.DataSource; import java.util.List; import java.util.Map; /** * 模型实例数据库枚举 * * @author shiyanjun * @since 2019-12-15 */ public enum DMInstanceDb { INSTANCE; // 模型项实例数据源 private static DataSource dataSource; static { if (dataSource == null) { try { dataSource = DruidDataSourceFactory.createDataSource(DbConfig.getDbConfig()); } catch (Exception e) { e.printStackTrace(); } } } /** * 获取数据源 * * @return */ public DataSource getDataSource() { return dataSource; } /** * 测试 * * @param args */ public static void main(String[] args) { String sql = "select * from system_dict"; // 使用枚举单例对象DMInstanceDb.INSTANCE获取数据源 DataSource dataSource = DMInstanceDb.INSTANCE.getDataSource(); List<Map<String, Object>> mapList = DbUtil.executeQuery(dataSource, sql); System.out.println(JSON.toJSONString(mapList)); } }