• JDBC封装查询单个和查询多个


    Mybatis在转化时候可以将数据库任意类型全转字符串是没有问题的

    下面封装存在一个问题就是需要数据库字段类型与实体类字段类型一致

    实体类

    //String columnName = metaData.getColumnName(i + 1); 这个方法返回实际列名
    String columnLabel = metaData.getColumnLabel(i + 1);//该方法返回别名,没有别名就返回列名

    因此用metaData.getColumnLabel(i + 1);比较合适

    工具类

    1. package com.utils;
    2. import java.lang.reflect.Field;
    3. import java.sql.*;
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. import java.util.ResourceBundle;
    7. /**
    8. * @author hrui
    9. * @date 2023/10/13 13:49
    10. */
    11. public class DBUtils {
    12. private static ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
    13. private static String driver=bundle.getString("jdbc.driver");
    14. private static String url=bundle.getString("jdbc.url");
    15. private static String username=bundle.getString("jdbc.username");
    16. private static String password=bundle.getString("jdbc.password");
    17. static{
    18. try {
    19. Class.forName(driver);
    20. } catch (ClassNotFoundException e) {
    21. e.printStackTrace();
    22. }
    23. }
    24. //通用查询多个
    25. public static <T> List<T> selectList(Class<T> clazz, String sql, Object...args){
    26. Connection conn=null;
    27. PreparedStatement ps=null;
    28. ResultSet rs=null;
    29. try {
    30. conn=DBUtils.getConnection();
    31. ps=conn.prepareStatement(sql);
    32. for(int i=0;i<args.length;i++){
    33. ps.setObject(i+1, args[i]);
    34. }
    35. rs = ps.executeQuery();
    36. ResultSetMetaData metaData = rs.getMetaData();
    37. int columnCount = metaData.getColumnCount();
    38. List<T> list=new ArrayList<>();
    39. while(rs.next()){
    40. T t = clazz.newInstance();
    41. for(int i=0;i<columnCount;i++){
    42. Object object = rs.getObject(i + 1);
    43. String columnLabel = metaData.getColumnLabel(i + 1);
    44. if(columnLabel.contains("_")){
    45. int index = columnLabel.indexOf("_");
    46. String replace = columnLabel.replace("_", "");
    47. char c = Character.toUpperCase(replace.charAt(index));
    48. columnLabel=replace.substring(0, index)+c+replace.substring(index+1);
    49. }
    50. Field field = clazz.getDeclaredField(columnLabel);
    51. field.setAccessible(true);
    52. field.set(t,object);
    53. }
    54. list.add(t);
    55. }
    56. return list;
    57. } catch (Exception e) {
    58. e.printStackTrace();
    59. }finally {
    60. DBUtils.closed(conn,ps,rs);
    61. }
    62. return null;
    63. }
    64. //通用查询单个
    65. public static <T> T selectOne(Class<T> clazz,String sql,Object...args){
    66. Connection conn=null;
    67. PreparedStatement ps=null;
    68. ResultSet rs=null;
    69. try {
    70. conn=DBUtils.getConnection();
    71. ps=conn.prepareStatement(sql);
    72. for(int i=0;i<args.length;i++){
    73. ps.setObject(i+1, args[i]);
    74. }
    75. rs = ps.executeQuery();
    76. ResultSetMetaData metaData = rs.getMetaData();
    77. int columnCount = metaData.getColumnCount();
    78. if(rs.next()){
    79. T t = clazz.newInstance();
    80. for(int i=0;i<columnCount;i++){
    81. Object object = rs.getObject(i + 1);
    82. String columnLabel = metaData.getColumnLabel(i + 1);
    83. if(columnLabel.contains("_")){
    84. int index = columnLabel.indexOf("_");
    85. String replace = columnLabel.replace("_", "");
    86. char c = Character.toUpperCase(replace.charAt(index));
    87. columnLabel=replace.substring(0, index)+c+replace.substring(index+1);
    88. }
    89. Field field = clazz.getDeclaredField(columnLabel);
    90. field.setAccessible(true);
    91. field.set(t,object);
    92. }
    93. return t;
    94. }
    95. } catch (Exception e) {
    96. e.printStackTrace();
    97. }finally {
    98. DBUtils.closed(conn,ps,rs);
    99. }
    100. return null;
    101. }
    102. public static Connection getConnection() throws SQLException {
    103. Connection connection = DriverManager.getConnection(url, username, password);
    104. return connection;
    105. }
    106. //通用增删改方法
    107. public static int update(String sql,Object...args){
    108. Connection conn =null;
    109. PreparedStatement ps=null;
    110. int count=0;
    111. try {
    112. conn = DBUtils.getConnection();
    113. ps = conn.prepareStatement(sql);
    114. for(int i=0;i<args.length;i++){
    115. ps.setObject(i+1, args[i]);
    116. }
    117. count = ps.executeUpdate();
    118. //ps.execute();
    119. } catch (SQLException e) {
    120. e.printStackTrace();
    121. }finally {
    122. DBUtils.closed(conn,ps,null);
    123. }
    124. return count;
    125. }
    126. public static void closed(Connection conn, Statement st, ResultSet rs){
    127. if(rs!=null){
    128. try {
    129. rs.close();
    130. } catch (SQLException e) {
    131. e.printStackTrace();
    132. }
    133. }
    134. if(st!=null){
    135. try {
    136. st.close();
    137. } catch (SQLException e) {
    138. e.printStackTrace();
    139. }
    140. }
    141. if(conn!=null){
    142. try {
    143. conn.close();
    144. } catch (SQLException e) {
    145. e.printStackTrace();
    146. }
    147. }
    148. }
    149. }

    jdbc.properties

    测试

  • 相关阅读:
    Java基础—Node类型的变化
    贪心算法在找零问题中的应用
    python-paddle报错‘ErnieTokenizer‘ object is not subscriptable
    QT QThread 多线程操作
    Go对Json的支持[encoding/json]
    List对象集合按照对象某一属性字段排序
    【深蓝学院】手写VIO第7章--VINS初始化和VIO系统--作业
    Golang 必知必会Go Mod命令
    矩阵点乘multiply()函数和矩阵乘法dot()函数
    基于SSM开发在线学习网站(前台+后台)
  • 原文地址:https://blog.csdn.net/tiantiantbtb/article/details/133827722