好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解Javaee Dao层的抽取

有时候我们在实现不同功能的时候回看到很多的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

查看更多关于详解Javaee Dao层的抽取的详细内容...

  阅读:39次