public class BasicDao<T> {
private static QueryRunner queryRunner;
static {
queryRunner = new QueryRunner();
}
//返回结果集(ArrayList集合)
public List<T> queryResultSet(String sql, Class<T> clazz, Object... parameters) {
Connection connection = JDBCUtilsByDruid.getConnection();
try {
return queryRunner.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//返回单行结果(单个对象)
public T querySingle(String sql, Class<T> clazz, Object... parameters) {
Connection connection = JDBCUtilsByDruid.getConnection();
try {
return queryRunner.query(connection, sql, new BeanHandler<T>(clazz), parameters);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//返回单行单列
public Object queryScalar(String sql, Object... parameters) {
Connection connection = JDBCUtilsByDruid.getConnection();
try {
return queryRunner.query(connection, sql, new ScalarHandler<>(), parameters);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//执行DML语句
public int update(String sql, Object... paramenters) {
Connection connection = JDBCUtilsByDruid.getConnection();
try {
int affectedRow = queryRunner.update(connection, sql, paramenters);
return affectedRow;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
}
Apache——DBUtils
最新推荐文章于 2024-08-22 20:19:10 发布