1、方法中的泛型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
public static <T> T backSerializable(Class<T> clazz , String path ,String fileName){ FileInputStream fis = null ; ObjectInputStream ois = null ; Object obj = null ; try { fis = new FileInputStream(path + fileName); ois = new ObjectInputStream(fis); obj = ois.readObject(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if ( fis!= null ) fis.close(); if ( ois!= null ) ois.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return (T)obj; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public class PageHibernateCallback<T> implements HibernateCallback<List<T>>{ private String hql; private Object[] params; private int startIndex; private int pageSize; public PageHibernateCallback(String hql, Object[] params, int startIndex, int pageSize) { super (); this .hql = hql; this .params = params; this .startIndex = startIndex; this .pageSize = pageSize; } public List<T> doInHibernate(Session session) throws HibernateException, SQLException { //1 執行hql語句 Query query = session.createQuery(hql); //2 實際參數 if (params != null ){ for ( int i = 0 ; i < params.length ; i ++){ query.setParameter(i, params[i]); } } //3 分頁 query.setFirstResult(startIndex); query.setMaxResults(pageSize); return query.list(); } } |
以上這篇淺談java中定義泛型類和定義泛型方法的寫法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。