The main function of using Basedao in the Hibernate framework is to reduce redundant code, which can be said to be the most common operation in DAO operations, with similar crud operations in the face of different data tables. Basedao's idea is to extract these redundant operations, so it's not necessary to register in the spring Framework's applicationcontext.xml, which may be different from other DAO. As an abstract class, using this Basedao can make our code more elegant
The first step: Create an interface just like the normal DAO
The methods implemented in the interface have Add (add), UPDATE, delete (delete), FindOne (query by ID), FINDALL (query all). In fact, can not be limited to these methods, as long as the DAO in all the operations, can be extracted, here only the basic method of extraction, here using generics, T represents all types.
import java.util.List; public interface Basedao<t> public void add (t T); public void update (T T); public void Delete (T T); public T findOne (int ID); public list<t> FindAll ();}
The second step: Implement this interface, write the specific logic of the method
This class also inherits Hibernatedaosupport, so that you can not inherit hibernatedaosupport in a specific DAO class, as long as it inherits from our writing Basedaoimpl on the line. The Basedaoimpl class uses reflection and generics.
ImportJava.lang.reflect.ParameterizedType;ImportJava.lang.reflect.Type;Importjava.util.List;ImportOrg.springframework.orm.hibernate5.support.HibernateDaoSupport;ImportCn.lynu.dao.BaseDao; Public classBasedaoimpl<t>extendsHibernatedaosupportImplementsBasedao<t> { PrivateClass PClass; PublicBasedaoimpl () {//The first step is to get the class of the current runClass Clazz = This. GetClass (); //The second step is to get the parameterized type of the parent class of the running classType type =Clazz.getgenericsuperclass ();Parameterizedtype ptype=(parameterizedtype) type; //The third step is to get the actual type parameterstype[] Types =ptype.getactualtypearguments (); Class Tclass= (Class) types[0]; This. pclass=Tclass; } //Add Public voidAdd (T t) { This. Gethibernatetemplate (). Save (t); } //Update Public voidUpdate (T t) { This. Gethibernatetemplate (). Update (t); } //Delete Public voidDelete (T t) { This. Gethibernatetemplate (). Delete (t); } //Query by ID PublicT FindOne (intID) {returnT This. Gethibernatetemplate (). Get (PClass, id); } //query All (use Getsimplename to get the abbreviation class name, otherwise, the full path of the class will be used to make an error) PublicList<t>FindAll () {return(list<t>) This. Gethibernatetemplate (). Find ("from" +pclass.getsimplename ()); }}
step three: Inherit all DAO interfaces from Basedao, all DAO implementation classes inherit from Basedaoimpl
Look at the implementation of the DAO interface (don't forget generics):
Look again at the implementation of the Daoimpl class (without inheriting Hibernatedaosupport):
Note: This way we don't have to write crud and Findone,findall methods in Userdao, but the names we use with these methods are fixed, don't use the wrong
The use of Java EE Note--basedao