Java开发中,常用工具封装
Java开发中,常用工具封装
JDBC工具
1、基于Druid
需要安装以下依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.1</version>
</dependency>
jdbc.properties 【不能是jdbc.password这种,jdbc.url 并不是 Druid 支持的标准配置项名称,因此会导致配置解析失败。】
password=zhw521
# 连接池配置
initialSize=5
maxActive=20
maxWait=30000
# 其他配置
driverClassName=com.mysql.cj.jdbc.Driver
实现代码
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUtil {
private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
private static final DataSource dataSource;
//初始化连接池
static {
Properties properties = new Properties();
InputStream resourceAsStream = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
properties.load(resourceAsStream);
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//向外提供连接池的方法
public static DataSource getDataSource() {
return dataSource;
}
//向外提供连接的方法
public static Connection getConnection() throws SQLException {
Connection connection = threadLocal.get();
if (null == connection) {
connection = dataSource.getConnection();
threadLocal.set(connection);
}
return connection;
}
public static void closeConnection() {
Connection connection = threadLocal.get();
if (null != connection) {
try {
connection.setAutoCommit(true);
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
threadLocal.remove();
}
}
}
BaseDao
代码实现
package com.zhw.schedule.dao;
import com.zhw.schedule.util.JDBCUtil;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BaseDao {
// 公共的查询方法 返回的是单个对象
public <T> T baseQueryObject(String sql, Object... args) {
T t = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
t = (T) resultSet.getObject(1);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (null != resultSet) {
resultSet.close();
}
if (null != preparedStatement) {
preparedStatement.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
JDBCUtil.closeConnection();
}
return t;
}
// 公共的查询方法 返回的是对象的集合
public <T> List<T> baseQuery(Class clazz, String sql, Object... args) {
ArrayList<T> list = new ArrayList<>();
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
try {
Connection connection = JDBCUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
while (resultSet.next()) {
Object obj = clazz.getDeclaredConstructor().newInstance();
for (int i = 1; i <= columnCount; i++) {
String columnName = metaData.getColumnLabel(i);
Object value = resultSet.getObject(columnName);
Field field = clazz.getDeclaredField(columnName);
field.setAccessible(true);
field.set(obj, value);
}
list.add((T) obj);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (null != resultSet) {
try {
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (null != preparedStatement) {
try {
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
JDBCUtil.closeConnection();
}
return list;
}
// 通用的增删改方法
public int baseUpdate(String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
int rows = 0;
try {
connection = JDBCUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
rows = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (null != preparedStatement) {
try {
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
JDBCUtil.closeConnection();
}
return rows;
}
}