BaseDao.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.sf.dao;
  2. import java.lang.reflect.Field;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.ResultSetMetaData;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class BaseDao<T> {
  12. /**
  13. * 通用增删改
  14. * @throws SQLException
  15. */
  16. public void update(Connection connection,String sql,Object...args) throws SQLException {
  17. //获取PreparedStatement并且预编译sql语句
  18. PreparedStatement ps = connection.prepareStatement(sql);
  19. //给SQL语句中的占位符赋值
  20. if(args!=null &&args.length>0) {
  21. for (int i = 0; i < args.length; i++) {
  22. ps.setObject(i+1, args[i]);
  23. }
  24. }
  25. //执行
  26. ps.executeUpdate();
  27. }
  28. /**
  29. * 查询列表
  30. * @throws SQLException
  31. * @throws IllegalAccessException
  32. * @throws InstantiationException
  33. * @throws SecurityException
  34. * @throws NoSuchFieldException
  35. */
  36. public List<T> getList(Connection connection,Class<T> clazz,String sql,Object...args) throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException{
  37. List<T> list = new ArrayList<>();
  38. //获取PreparedStatement并且预编译sql语句
  39. PreparedStatement ps = connection.prepareStatement(sql);
  40. //给SQL语句中的占位符赋值
  41. if(args!=null &&args.length>0) {
  42. for (int i = 0; i < args.length; i++) {
  43. ps.setObject(i+1, args[i]);
  44. }
  45. }
  46. //执行
  47. ResultSet rs = ps.executeQuery();
  48. //获取数据库表的元数据信息
  49. ResultSetMetaData rsmd = rs.getMetaData();
  50. //获取表的列的总数
  51. int columnCount = rsmd.getColumnCount();
  52. //从结果集中获取每一个数据
  53. while(rs.next()) {
  54. //创建T对象
  55. T t = clazz.newInstance();
  56. //获取列名,jdbc提供了使用列的下标获取列名的方式
  57. for (int i = 0; i < columnCount; i++) {
  58. String columnLabel = rsmd.getColumnLabel(i+1);
  59. //通过列名获取列值
  60. Object columnValue = rs.getObject(columnLabel);
  61. //获取T对象中属性的对象
  62. Field field = clazz.getDeclaredField(columnLabel);//获取当前类中的属性列表
  63. //开启权限
  64. field.setAccessible(true);
  65. //给当前属性赋值
  66. field.set(t, columnValue);
  67. }
  68. list.add(t);
  69. }
  70. return list;
  71. }
  72. /**
  73. * 查询单个对象
  74. * @throws SQLException
  75. * @throws IllegalAccessException
  76. * @throws IllegalArgumentException
  77. * @throws SecurityException
  78. * @throws NoSuchFieldException
  79. * @throws InstantiationException
  80. */
  81. public T get(Connection connection,Class<T> clazz,String sql,Object...args) throws SQLException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, InstantiationException {
  82. T t = null;
  83. //获取PreparedStatement并且预编译sql语句
  84. PreparedStatement ps = connection.prepareStatement(sql);
  85. //给SQL语句中的占位符赋值
  86. if(args!=null &&args.length>0) {
  87. for (int i = 0; i < args.length; i++) {
  88. ps.setObject(i+1, args[i]);
  89. }
  90. }
  91. //执行
  92. ResultSet rs = ps.executeQuery();
  93. //获取数据库表的元数据信息
  94. ResultSetMetaData rsmd = rs.getMetaData();
  95. //获取表的列的总数
  96. int columnCount = rsmd.getColumnCount();
  97. //从结果集中获取每一个数据
  98. if(rs.next()) {
  99. //创建T对象
  100. t = clazz.newInstance();
  101. //获取列名,jdbc提供了使用列的下标获取列名的方式
  102. for (int i = 0; i < columnCount; i++) {
  103. String columnLabel = rsmd.getColumnLabel(i+1);
  104. //通过列名获取列值
  105. Object columnValue = rs.getObject(columnLabel);
  106. //获取T对象中属性的对象
  107. Field field = clazz.getDeclaredField(columnLabel);//获取当前类中的属性列表
  108. //开启权限
  109. field.setAccessible(true);
  110. //给当前属性赋值
  111. field.set(t, columnValue);
  112. }
  113. }
  114. return t;
  115. }
  116. /**
  117. * 获取数据库表的总记录数
  118. * @throws SQLException
  119. * SELECT COUNT(*) FROM employee;
  120. */
  121. public int getCount(Connection connection,String sql) throws SQLException {
  122. Statement statement = connection.createStatement();
  123. ResultSet rs = statement.executeQuery(sql);
  124. if(rs.next()) {
  125. int count = rs.getInt("count");
  126. return count;
  127. }
  128. return 0;
  129. }
  130. }