|
@@ -6,6 +6,7 @@ import com.sf.sysdemo.util.JDBCUtil;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.sql.*;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
|
|
|
// 提供一个基础的dao类
|
|
@@ -14,15 +15,90 @@ public class BaseDao {
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
BaseDao dao = new BaseDao();
|
|
|
+
|
|
|
+// SysUser user = dao.baseQueryByOne(SysUser.class, "select * from sys_user where uid = 1");
|
|
|
+// SysUser user = dao.baseQueryByOne(SysUser.class, "select * from sys_user where uid = ?", 1);
|
|
|
+// System.out.println(user);
|
|
|
+
|
|
|
+ String insertSql = "insert into sys_user(username,password) values(?,?)";
|
|
|
+ int update = dao.baseUpdate(insertSql, "wangwu", "123456");
|
|
|
+ System.out.println(update);
|
|
|
+
|
|
|
List<Object> list = dao.baseQuery(SysUser.class, "select * from sys_user");
|
|
|
System.out.println(list);
|
|
|
}
|
|
|
|
|
|
+ // 这是查询返回一行数据
|
|
|
+ public <T> T baseQueryByOne(Class<T> clazz, String sql, Object... args){
|
|
|
+ T t = null;
|
|
|
+
|
|
|
+ Connection connection = JDBCUtil.getConnection();
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ preparedStatement = connection.prepareStatement(sql);
|
|
|
+ // 让sql语句支持参数
|
|
|
+ for (int i = 0; i < args.length; i++) {
|
|
|
+ Object arg = args[i];
|
|
|
+ preparedStatement.setObject(i + 1, arg);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行查询
|
|
|
+ resultSet = preparedStatement.executeQuery();
|
|
|
+ // 获取返回结果的元数据
|
|
|
+ ResultSetMetaData metaData = resultSet.getMetaData();
|
|
|
+ // 返回的实际是一个表格 这里获取表格有多少列
|
|
|
+ int columnCount = metaData.getColumnCount();
|
|
|
+
|
|
|
+ if (resultSet.next()) {
|
|
|
+ // 这里从确定的 通过sys_user查询的结果 可以映射为一个SysUser类的对象
|
|
|
+ // 变成不确定的 通过一个Class类型 映射为一个动态类型的对象
|
|
|
+ Object cls = 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(cls, value);
|
|
|
+ }
|
|
|
+ return (T)cls;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally{
|
|
|
+ if(resultSet != null){
|
|
|
+ try {
|
|
|
+ resultSet.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(preparedStatement != null){
|
|
|
+ try {
|
|
|
+ preparedStatement.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ JDBCUtil.releaseConnection(connection);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 这是查询返回多行数据
|
|
|
// 泛型方法 不确定返回类型 如果一行数据对应的类型是 T 那么这里支持返回多行数据
|
|
|
// Object ... args 是可变参数 实际上是一个数组
|
|
|
public <T> List<T> baseQuery(Class clazz, String sql, Object... args){
|
|
|
List<T> list = new ArrayList<T>();
|
|
|
|
|
|
+// System.out.println("args: " + Arrays.toString(args));
|
|
|
+
|
|
|
Connection connection = JDBCUtil.getConnection();
|
|
|
PreparedStatement preparedStatement = null;
|
|
|
ResultSet resultSet = null;
|
|
@@ -61,8 +137,52 @@ public class BaseDao {
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
|
+ }finally{
|
|
|
+ if(resultSet != null){
|
|
|
+ try {
|
|
|
+ resultSet.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(preparedStatement != null){
|
|
|
+ try {
|
|
|
+ preparedStatement.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ JDBCUtil.releaseConnection(connection);
|
|
|
}
|
|
|
// 一堆关闭
|
|
|
return list;
|
|
|
}
|
|
|
+
|
|
|
+ // 通用的增删改
|
|
|
+ public int baseUpdate(String sql, Object... args){
|
|
|
+ Connection connection = JDBCUtil.getConnection();
|
|
|
+ PreparedStatement preparedStatement = null;
|
|
|
+ int result = 0;
|
|
|
+
|
|
|
+ try {
|
|
|
+ preparedStatement = connection.prepareStatement(sql);
|
|
|
+ for (int i = 0; i < args.length; i++) {
|
|
|
+ preparedStatement.setObject(i + 1, args[i]);
|
|
|
+ }
|
|
|
+ // 执行更新 返回更改的行数
|
|
|
+ result = preparedStatement.executeUpdate();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ if(preparedStatement != null){
|
|
|
+ try {
|
|
|
+ preparedStatement.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ JDBCUtil.releaseConnection(connection);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|