Reflection generics, DAO Design Model (required for encoding)

Source: Internet
Author: User

Dao
 PackageCom.xiaofan.reflect;Importjava.io.Serializable; Public InterfaceDao<t> {    /*** Add entity (entity--domain) data to the database *@paramT*/    voidSave (T T); /*** Modify the entities in the database *@paramT*/    voidUpdate (T T); /*** Delete records according to primary key *@paramPK*/    voidDelete (Serializable PK); /*** Query A record according to the primary key *@paramPK *@return     */T Find (Serializable PK); }
Dao

Daobase

 PackageCom.xiaofan.reflect;Importjava.io.Serializable;ImportJava.lang.reflect.ParameterizedType;Importorg.hibernate.Session; Public Abstract classDaobase<t>ImplementsDao<t> {    PrivateSession session; PrivateClass Clazz; //Reflection Generics     Publicdaobase () {Class CLZ= This. GetClass ();//specific DAO implementation class Com.xiaofan.reflect.BookDaoImpl//System.out.println (Clz.getname ());Parameterizedtype pt = (parameterizedtype) clz.getgenericsuperclass ();//daobase<book>//System.out.println (PT);Clazz = (Class) pt.getactualtypearguments () [0];//Book.class//System.out.println (clazz);    }         Public voidSave (T t) {Session.save (t); }     Public voidUpdate (T t) {session.update (t); }     Public voidDelete (Serializable pk) {T T=(T) session.get (clazz, PK);    Session.delete (t); }     PublicT Find (Serializable PK) {return(T) session.get (clazz, PK); }}
Daobase

Bookdao

 Package Com.xiaofan.reflect;  Public Interface extends Dao<book>{    /**     * Query     by criteria @param  where      */    void querybycondition (String where);}
Bookdao

Bookdaoimpl

 Package Com.xiaofan.reflect;  Public class extends Implements bookdao{        publicvoid  querybycondition (String where) {            }}
Bookdaoimpl

Book

 Package Com.xiaofan.reflect;  Public class Book {}
Book

Customerdao

 Package Com.xiaofan.reflect;  Public Interface extends Dao<customer>{}
Customerdao

Customerdaoimpl

 Package Com.xiaofan.reflect;  Public class extends Implements Customerdao {    }
Customerdaoimpl

Customer

 Package Com.xiaofan.reflect;  Public class Customer {}
Customer

Client

 Package Com.xiaofan.reflect;  Public class Client {    publicstaticvoid  main (string[] args) {        new  Bookdaoimpl ();        Bdao.querybycondition ("condition");         New Customerdaoimpl ();        Cdao.find (001);    }}
Client

Reflection generics, DAO Design Model (required for encoding)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.