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