有時候我們在實現不同功能的時候回看到很多的dao層的增加、修改、刪除、查找都很相似,修改我們將他們提取basedao
一、提取前
1. 提取前的linkdao層:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public interface linkmandao { integer findcount(detachedcriteria detachedcriteria); list<linkman> findbypage(detachedcriteria detachedcriteria, integer startindex, integer pagesize); void save(linkman linkman); linkman findbyid( long lkm_id); void update(linkman linkman); void delete(linkman linkman); } |
2. 提取前的linkdaoimpl:
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
39
40
41
42
|
@repository public class linkmandaoimpl implements linkmandao { @autowired private hibernatetemplate hibernatetemplate; @override public integer findcount(detachedcriteria detachedcriteria) { //select count(*) from linkman detachedcriteria.setprojection(projections.rowcount()); list< long > list = (list< long >) hibernatetemplate.findbycriteria(detachedcriteria); if (list != null && list.size() > 0 ) { return list.get( 0 ).intvalue(); } return null ; } @override public list<linkman> findbypage(detachedcriteria detachedcriteria, integer startindex, integer pagesize) { detachedcriteria.setprojection( null ); return (list<linkman>) hibernatetemplate.findbycriteria(detachedcriteria, startindex, pagesize); } @override public void save(linkman linkman) { hibernatetemplate.save(linkman); } //dao層根據id查找聯系人 @override public linkman findbyid( long lkm_id) { return hibernatetemplate.get(linkman. class , lkm_id); } //dao層更新聯系人信息 @override public void update(linkman linkman) { hibernatetemplate.update(linkman); } //dao層刪除聯系人 @override public void delete(linkman linkman) { hibernatetemplate.delete(linkman); } } |
3. 提取前的customerdao
1
2
3
4
5
6
7
8
9
|
public interface customerdao{ void save(customer customer); integer findcount(detachedcriteria detachedcriteria); list<customer> findbypage(detachedcriteria detachedcriteria, integer startindex, integer pagesize); customer findbyid( long cust_id); void delete(customer customer); void update(customer customer); list<customer> findall(); } |
4.提取前的customerdaoimpl
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
@repository public class customerdaoimpl implements customerdao { //注入hibernatetemplate模板 @autowired private hibernatetemplate hibernatetemplate; /** * dao層保存客戶信息實現方法 * <p>title: customerdaoimpl</p> * <p>description: </p> * @param customer * @see com.sshcrm.dao.customerdao#savecustomer(com.sshcrm.pojo.customer) */ @override public void savecustomer(customer customer) { hibernatetemplate.save(customer); } //根據條件查詢結果集的總記錄數 @override public integer findcount(detachedcriteria detachedcriteria) { //select count(*) from customer detachedcriteria.setprojection(projections.rowcount()); list< long > list = (list< long >) hibernatetemplate.findbycriteria(detachedcriteria); if (list != null && list.size() > 0 ) { return list.get( 0 ).intvalue(); } return null ; } //根據查詢條件查詢總頁數 @override public list<customer> findbypage(detachedcriteria detachedcriteria, integer startindex, integer pagesize) { //由于在統計總記錄數的時候已經修改了發送的sql語句,在此需要需要清空 detachedcriteria.setprojection( null ); return (list<customer>) hibernatetemplate.findbycriteria(detachedcriteria, startindex, pagesize); } @override public customer findbyid( long cust_id) { return hibernatetemplate.get(customer. class , cust_id); } @override public void delete(customer customer) { hibernatetemplate.delete(customer); } @override public void update(customer customer) { hibernatetemplate.update(customer); } @override public list<customer> findall() { return (list<customer>) hibernatetemplate.find( "from customer" ); } } |
5.可以看到customerdaoimpl和linkmandaoimpl方法很相似,所以需要提取
二、利用在子類中傳遞真正的class類型來提取basedao,編寫泛型
1. basedao層
1
2
3
4
5
6
7
8
9
|
public interface basedao<t> { void save(t t); void update(t t); void delete(t t); public t findbyid(serializable id); public list<t> findall(); public integer findcount(detachedcriteria detachedcriteria); public list<t> findbypage(detachedcriteria detachedcriteria, integer startindex, integer pagesize); } |
2. basedaoimpl層
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
public class basedaoimpl<t> implements basedao<t> { private class clazz; //提供構造方法,在構造方法中讓繼承的子類向方法中傳入具體類型class public basedaoimpl( class clazz) { this .clazz = clazz; } //注入hibernatetemplate模板 @autowired private hibernatetemplate hibernatetemplate; //保存信息 @override public void save(t t) { hibernatetemplate.save(t); } //更新信息 @override public void update(t t) { hibernatetemplate.update(t); } //刪除信息 @override public void delete(t t) { hibernatetemplate.delete(t); } //根據id查詢信息 @override public t findbyid(serializable id) { return (t) hibernatetemplate.get( this .clazz, id); } //查詢所有信息 @override public list<t> findall() { return (list<t>) hibernatetemplate.find( "from " + this .clazz.getsimplename()); } //查詢count(*)行記錄數 @override public integer findcount(detachedcriteria detachedcriteria) { detachedcriteria.setprojection(projections.rowcount()); list< long > list = (list< long >) hibernatetemplate.findbycriteria(detachedcriteria); if (list != null && list.size() > 0 ) { return list.get( 0 ).intvalue(); } return null ; } //分頁查詢信息 @override public list<t> findbypage(detachedcriteria detachedcriteria, integer startindex, integer pagesize) { detachedcriteria.setprojection( null ); return (list<t>) hibernatetemplate.findbycriteria(detachedcriteria, startindex, pagesize); } } |
3. 提取后的linkmandao
1
2
3
|
public interface linkmandao extends basedao<linkman>{ } |
4. 提取后的linkmandaoimpl
1
2
3
4
5
6
7
8
9
10
11
|
@repository public class linkmandaoimpl extends basedaoimpl<linkman> implements linkmandao { //提供構造參數,在構造方法中傳入具體類型的class public linkmandaoimpl() { super (linkman. class ); } @autowired private hibernatetemplate hibernatetemplate; } |
5.提取后的customerdao
1
2
3
|
public interface customerdao extends basedao<customer> { } |
6. 提取后的customerdaoimpl
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@repository public class customerdaoimpl extends basedaoimpl<customer> implements customerdao { //提供構造參數,在構造方法中傳入具體的class public customerdaoimpl() { super (customer. class ); // todo auto-generated constructor stub } //注入hibernatetemplate模板 @autowired private hibernatetemplate hibernatetemplate; } |
7. 如果這樣抽取完成以后,那么在編寫dao的時候如果里面都是一些crud的操作,在dao中只需要提供構造方法即可。
三、如果將通用的dao編寫的更好,連構造方法都不想要了?。。⌒枰趺醋???? 泛型反射
1 解決方案二:通過泛型的反射抽取通用的dao
l 如果現在將dao中的構造方法去掉,將父類的通用的dao中提供無參數的構造即可,但是需要在無參數的構造中需要獲得具體類型的class才可以-----涉及到泛型的反射了。
l 回顧一下泛型:
泛型 :通用的類型。
<> :念為 typeof
list<e> :e稱為類型參數變量
arraylist<integer> :integer稱為是實際類型參數
arraylist<integer> :arraylist<integer>稱為參數化類型
需要做的時候在父類的構造方法中獲得子類繼承父類上的參數化類型中的實際類型參數
泛型反射的步驟:
第一步:獲得代表子類對象的class
第二步:查看api
type[] getgenericinterfaces(); :獲得帶有泛型的接口,可以實現多個接口。
type getgenericsuperclass(); :獲得帶有泛型的父類,繼承一個類。
第三步:獲得帶有泛型的父類
第四步:將帶有泛型的父類的類型轉成具體參數化的類型
第五步:通過參數化類型的方法獲得實際類型參數
2. 代碼實現
2.1 修改basedaoimpl里面的無參構造方法:
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
public class basedaoimpl<t> implements basedao<t> { private class clazz; //提供構造方法,在構造方法中讓繼承的子類向方法中傳入具體類型class /** * 不想子類上有構造方法,必須在父類中提供無參的構造,在無參構造中獲取具體的類型class * 具體類型中的class是參數類型中的實際類型 參數 */ public basedaoimpl() { //反射:第一步獲得class class clazz = this .getclass(); //正在被調用的那個類的class,customerdaoimpl或linkmandaoimpl //具體查看jdk的api type type = clazz.getgenericsuperclass(); //參數化類型basedaoimpl<customer>,basedaoimpl<linkman> //得到的type就是一個參數化類型,將type強轉為參數化類型 parameterizedtype ptype = (parameterizedtype) type; //通過參數化類型獲得實際類型參數,得到一個實際類型參數的數組 type[] types = ptype.getactualtypearguments(); //只獲得第一參數類型即可 this .clazz = ( class ) types[ 0 ]; //得到customer,linkman } //注入hibernatetemplate模板 @autowired private hibernatetemplate hibernatetemplate; //保存信息 @override public void save(t t) { hibernatetemplate.save(t); } //更新信息 @override public void update(t t) { hibernatetemplate.update(t); } //刪除信息 @override public void delete(t t) { hibernatetemplate.delete(t); } //根據id查詢信息 @override public t findbyid(serializable id) { return (t) hibernatetemplate.get( this .clazz, id); } //查詢所有信息 @override public list<t> findall() { return (list<t>) hibernatetemplate.find( "from " + this .clazz.getsimplename()); } //查詢count(*)行記錄數 @override public integer findcount(detachedcriteria detachedcriteria) { detachedcriteria.setprojection(projections.rowcount()); list< long > list = (list< long >) hibernatetemplate.findbycriteria(detachedcriteria); if (list != null && list.size() > 0 ) { return list.get( 0 ).intvalue(); } return null ; } //分頁查詢信息 @override public list<t> findbypage(detachedcriteria detachedcriteria, integer startindex, integer pagesize) { detachedcriteria.setprojection( null ); return (list<t>) hibernatetemplate.findbycriteria(detachedcriteria, startindex, pagesize); } } |
2.2 現在linkdao和customerdao不用改變,修改linkdaoimpl和customerdaoimpl
1
2
3
4
5
6
7
8
9
10
|
@repository public class linkmandaoimpl extends basedaoimpl<linkman> implements linkmandao { //提供構造參數,在構造方法中傳入具體的class /* public linkmandaoimpl() { super(linkman.class); }*/ @autowired private hibernatetemplate hibernatetemplate; } |
1
2
3
4
5
6
7
8
9
10
11
12
|
@repository public class customerdaoimpl extends basedaoimpl<customer> implements customerdao { //提供構造參數,在構造方法中傳入具體的class /*public customerdaoimpl() { super(customer.class); // todo auto-generated constructor stub }*/ //注入hibernatetemplate模板 @autowired private hibernatetemplate hibernatetemplate; } |
2.3 后面如果dao層有特殊方法是可以在比如customerdaoimpl中去實現,相似的就不需要了,以此來到達抽取dao層
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/ya-qiang/p/9377053.html