將ResultSet中得到的一行或多行結(jié)果集封裝成對(duì)象的實(shí)例
首先說(shuō)一下這個(gè)使用場(chǎng)景,我們?cè)谑褂胘dbc連接數(shù)據(jù)庫(kù)的時(shí)候,執(zhí)行查詢語(yǔ)句時(shí)候會(huì)得到一個(gè)結(jié)果集,如果想要再獲取這個(gè)結(jié)果集中的值,就需要我們將他轉(zhuǎn)換成一個(gè)對(duì)象,然后通過(guò)對(duì)象的get和set方法來(lái)獲取到數(shù)據(jù)庫(kù)中的值。
public class BaseDao <E> { private Class<?> cls; public BaseDao() { //得到父類的泛型 Type sType=getClass().getGenericSuperclass(); //得到實(shí)際的類型參數(shù)數(shù)組 Type[] generics=((ParameterizedType) sType).getActualTypeArguments(); //得到第一個(gè)泛型的Class cls=(Class<?>) (generics[0]); }
/** * 單表多條查詢,將查詢到的多條記錄傳入一個(gè)對(duì)象,然后再將這些存入一個(gè)集合中,返回這個(gè)集合 * @param sql 傳入對(duì)應(yīng)的sql查詢語(yǔ)句 * @param parameters 傳入對(duì)應(yīng)的占位符的值 * @return 返回查詢到的記錄轉(zhuǎn)化成的對(duì)象的集合 */ //Object...parameters是sql語(yǔ)句中對(duì)應(yīng)的占位符的值,是一個(gè)不定長(zhǎng)可變參數(shù),我們需要寫一個(gè)函數(shù)來(lái)獲取他 public List<E> list(String sql,Object...parameters) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; List<E> list = new ArrayList<>(); try { conn = JdbcUtil.getConnection(); st = conn.prepareStatement(sql); setParameters(st, parameters); rs = st.executeQuery(); while(rs.next()) { //將獲取到的結(jié)果集存入一個(gè)對(duì)象中,這個(gè)我們也單獨(dú)寫一個(gè)函數(shù)來(lái)實(shí)現(xiàn) E obj = oneRowToObject(rs); //然后將對(duì)象存入一個(gè)集合中返回 list.add(obj); } } catch (Exception e) { e.printStackTrace(); } finally { JdbcUtil.closeAll(rs, st, conn); } return list; }
首先來(lái)寫一下獲取不定長(zhǎng)可變參數(shù)的方法
/** * 設(shè)置占位符 * @param st 預(yù)處理 * @param parameters 占位符數(shù)組 * @return 返回存儲(chǔ)占位符對(duì)應(yīng)的對(duì)象的數(shù)組 */ private void setParameters(PreparedStatement st, Object[] parameters) { //判斷是否有結(jié)果集,結(jié)果集中是否有記錄 if(parameters!=null&¶meters.length>0) { for(int i=0;i<parameters.length;i++) { try { st.setObject(i+1,parameters[i] ); } catch (SQLException e) { e.printStackTrace(); } } } }
然后再把一個(gè)結(jié)果集轉(zhuǎn)化成一個(gè)對(duì)象的方法寫一下
* 把得到的一列數(shù)據(jù)存入到一個(gè)對(duì)象中 * @param rs * @return * @throws InstantiationException * @throws IllegalAccessException * @throws SQLException * @throws NoSuchMethodException * @throws SecurityException * @throws IllegalArgumentException * @throws InvocationTargetException */ @SuppressWarnings("unchecked") private E oneRowToObject(ResultSet rs) throws InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { E obj; obj=(E) cls.newInstance(); //獲取結(jié)果集元數(shù)據(jù)(獲取此 ResultSet 對(duì)象的列的編號(hào)、類型和屬性。) ResultSetMetaData rd=rs.getMetaData(); for (int i = 0; i < rd.getColumnCount(); i++) { //獲取列名 String columnName=rd.getColumnLabel(i+1); //組合方法名 String methodName="set"+columnName.substring(0, 1).toUpperCase()+columnName.substring(1); //獲取列類型 int columnType=rd.getColumnType(i+1); Method method=null; switch(columnType) { case java.sql.Types.VARCHAR: case java.sql.Types.CHAR: method=cls.getMethod(methodName, String.class); if(method!=null) { method.invoke(obj, rs.getString(columnName)); } break; case java.sql.Types.INTEGER: case java.sql.Types.SMALLINT: method=cls.getMethod(methodName, int.class); if(method!=null) { method.invoke(obj, rs.getInt(columnName)); } break; case java.sql.Types.BIGINT: method=cls.getMethod(methodName, long.class); if(method!=null) { method.invoke(obj, rs.getLong(columnName)); } break; case java.sql.Types.DATE: case java.sql.Types.TIMESTAMP: try { method=cls.getMethod(methodName, Date.class); if(method!=null) { method.invoke(obj, rs.getTimestamp(columnName)); } } catch(Exception e) { method=cls.getMethod(methodName, String.class); if(method!=null) { method.invoke(obj, rs.getString(columnName)); } } break; case java.sql.Types.DECIMAL: method=cls.getMethod(methodName, BigDecimal.class); if(method!=null) { method.invoke(obj, rs.getBigDecimal(columnName)); } break; case java.sql.Types.DOUBLE: case java.sql.Types.NUMERIC: method=cls.getMethod(methodName, double.class); if(method!=null) { method.invoke(obj, rs.getDouble(columnName)); } break; case java.sql.Types.BIT: method=cls.getMethod(methodName, boolean.class); if(method!=null) { method.invoke(obj, rs.getBoolean(columnName)); } break; default: break; } } return obj; }
使用的話就是寫一個(gè)實(shí)體類Dao繼承BaseDao
public class UserDao extends BaseDao <User>{
}
測(cè)試一下:
public class test { public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, SQLException, IntrospectionException { UserDao userdao = new UserDao(); List<User> list=userdao.list("select * from user"); System.out.println("uid\t"+"uname\t"+"state\t"+"flag"); for (User user : list) { System.out.println(user.getUid()+"\t"+user.getUname()+"\t"+user.getState()+"\t"+user.getFlag()); } } }
以上這篇將ResultSet中得到的一行或多行結(jié)果集封裝成對(duì)象的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java使用JNA(Java Native Access)調(diào)用dll的方法
java使用JNA(Java Native Access)調(diào)用windows系統(tǒng)的dll文件的例子2013-11-11Spring Cloud中各組件超時(shí)總結(jié)
在大家學(xué)習(xí)spring cloud的時(shí)候組件是必不可少的一部分,下面這篇文章主要給大家介紹了關(guān)于Spring Cloud中各組件超時(shí)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-11-11Java中的static關(guān)鍵字修飾屬性和方法(推薦)
這篇文章主要介紹了Java中的static關(guān)鍵字修飾屬性和方法,包括哪些成員屬性可以被static修飾,靜態(tài)屬性的訪問(wèn)方法示例詳解,需要的朋友可以參考下2022-04-04java中json-diff簡(jiǎn)單使用及對(duì)象是否一致詳解
這篇文章主要為大家介紹了java中json-diff簡(jiǎn)單使用及對(duì)象是否一致對(duì)比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03JAVA獲取rabbitmq消息總數(shù)過(guò)程詳解
這篇文章主要介紹了JAVA獲取rabbitmq消息總數(shù)過(guò)程詳解,公司使用的是rabbitMQ,需要做監(jiān)控預(yù)警的job去監(jiān)控rabbitMQ里面的堆積消息個(gè)數(shù),如何使用rabbitMQ獲取監(jiān)控的隊(duì)列里面的隊(duì)列消息個(gè)數(shù)呢,需要的朋友可以參考下2019-07-07Spring WebFlux使用函數(shù)式編程模型構(gòu)建異步非阻塞服務(wù)
這篇文章主要介紹了Spring WebFlux使用函數(shù)式編程模型構(gòu)建異步非阻塞服務(wù),重點(diǎn)介紹如何使用函數(shù)式編程模型創(chuàng)建響應(yīng)式 RESTful 服務(wù),這種編程模型與傳統(tǒng)的基于 Spring MVC 構(gòu)建 RESTful 服務(wù)的方法有較大差別,感興趣的朋友跟隨小編一起看看吧2023-08-08JAVA CountDownLatch(倒計(jì)時(shí)計(jì)數(shù)器)用法實(shí)例
這篇文章主要介紹了JAVA CountDownLatch(倒計(jì)時(shí)計(jì)數(shù)器)用法實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Java創(chuàng)建可執(zhí)行的Jar文件的方法實(shí)踐
創(chuàng)建的可執(zhí)行Jar文件實(shí)際就是在原始Jar的清單文件中添加了Main-Class的配置,本文主要介紹了Java創(chuàng)建可執(zhí)行的Jar文件的方法實(shí)踐,感興趣的可以了解一下2023-12-12