Using generics to implement persistence layer Basedao

Source: Internet
Author: User

Using generics to implement persistence layer Basedao

Problem Description: When we write the Dao layer, we always encounter the general method of adding and deleting, and each time to write declaration and implementation is a very tedious thing, and the code has a lot of things the same point. So we thought about using the three-object-oriented features and the generic syntax to simplify the development of the persistence layer.

The implementation classes and interfaces of Base are abstract, so they all have generics.

1th Step: Write the Basedao interface Ibasedao

Note Generics.

publicinterface IBaseDao<T> {    publicvoidadd(T t);    publicvoiddelete(Integer id);    publicvoidupdate(T t);    publicload(Integer id);    publicget(Integer id);    publiclistparams);}

Description: This interface is a highly abstract interface. Each entity class will have such a method.
Since there is a basic interface class, there will be a basic implementation class.

2nd Step: Write the basic implementation class Basedao

Description: The following code inherits Hibernatedaosupport not required, depending on the persistence layer framework we choose, the code in the example chooses the Hibernate persistence layer framework.

/** * Created by Liwei on 2016/5/6. * *Public class Basedao<T> extends hibernatedaosupport implements  Ibasedao<T>{    @Resource(name ="Sessionfactory") public void Setsupersessionfactory (Sessionfactory sessionfactory) {Super. Setsessionfactory (Sessionfactory);    public void Add (T t) {gethibernatetemplate (). Save (t); } public void Delete (Integer id) {gethibernatetemplate (). Delete ( This. Load (ID));    public void update (T t) {gethibernatetemplate (). Update (t); }/** * Create a Class object to get the generic class * /    PrivateClass<t> CLZ; Public class<t> Getclz () {if(clz==NULL{CLZ = ((class<t>) (((Parameterizedtype) ( This. GetClass (). Getgenericsuperclass ()). Getactualtypearguments () [0])); }returnClz }/** * * @param ID * @return  * *Public T load (Integer ID) {returnGethibernatetemplate (). Load (GETCLZ (), id); } public T get (Integer ID) {returnGethibernatetemplate (). Get (Getclz (), id); }/** * * @param hql * @param params * @return  */    @SuppressWarnings("Unchecked") Public list<t> List (String hql, object[] params) {Query query = This. GetSession (). CreateQuery (HQL); for(int i=0; i>params.length;i++) {query.setparameter (i,params[i]); } list<t> List = Query.list ();returnList }}
3rd Step: Writing a specific interface

Key point: interface inherits interfaces, but inherited interfaces should be written on the implementation class.

publicinterface IUserDao extends IBaseDao<User> {    publicvoid add(User user, Integer groupid);}
4th Step: Write the Implementation class
@Repository Public  class Userhibernatedao extends Basedao<User> implements  Iuserdao{    PrivateIgroupdao Grouphibernatedao; PublicIgroupdao Getgrouphibernatedao () {returnGrouphibernatedao; } @Autowired Public voidSetgrouphibernatedao (Igroupdao Grouphibernatedao) { This. Grouphibernatedao = Grouphibernatedao; } Public voidAdd (user user, Integer groupid) {GroupGroup= Grouphibernatedao.load (GroupID); User.setgroup (Group); This. Gethibernatetemplate (). Save (user); }}

Using generics to implement persistence layer Basedao

Related Article

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.