Scenario: In a shopping site, there are first-level classification entities, level two classification entities, commodity entities, and the actions to map three entities to the database are: Delete a record, save a record, find a record based on the ID, modify the record, and so on. If we do not use reflection, according to the general wording, then we correspond to three entities of the DAO are to implement additions and deletions to change the operation. If we use the reflection mechanism we just need to define a base class generic DAO, let it do all the work, then make the other three DAO inherit the Basedao, and then through reflection to identify the specific entity type in the run process, implement the operation.
Basedao.java
import java.util.List;
public interface basedao<t> {
public void Save (T T);
public void Update (T t);
public void Delete (T t);
public T FindByID (Integer ID);
public T FindByID (String ID);
public list<t> FindAll ();
}
CategoryDao. java
Public interface CategoryDao extends basedao<category>{}
Categoryseconddao. java
Public interface Categoryseconddao extends basedao<categorysecond>{}
Productdao. java
Public interface Productdao extends basedao<product>{
List<product> findhotproduct (); There are other ways to do it.
}
Basedaoimpl.java
public class Basedaoimpl<t> extends Hibernatedaosupport implements basedao<t> {
Private Class Clazz;
Public Basedaoimpl () {
Get the Class object:
Class C = This.getclass ();
System.out.println (c);
Type type = C.getgenericsuperclass ();
Parameterizedtype PType = (parameterizedtype) type;
System.out.println (PType);
type[] types = ptype.getactualtypearguments ();
This.clazz = (Class) types[0]; Get a specific type
}
@Override
public void Delete (T t) {
This.gethibernatetemplate (). Delete (t);
}
@Override
Public list<t> FindAll () {
String hql = "from" +clazz.getsimplename ();
Return This.gethibernatetemplate (). Find (HQL);
}
@Override
Public T FindByID (Integer ID) {
Return (T) this.gethibernatetemplate (). Get (Clazz, id);
}
@Override
Public T FindByID (String ID) {
Return (T) this.gethibernatetemplate (). Get (Clazz, id);
}
@Override
public void Save (T t) {
This.gethibernatetemplate (). Save (t);
}
@Override
public void update (T t) {
This.gethibernatetemplate (). Update (t);
}
Categorydaoimpl. java
public class Categorydaoimpl extends basedaoimpl<category> implements CategoryDao {
}
Categoryseconddaoimpl. java
public class Categoryseconddaoimpl extends basedaoimpl<categorysecond> implements Categoryseconddao {}
Productdaoimpl. java
public class Productdaoimpl extends basedaoimpl<product> implements Productdao {
Public list<product> findhotproduct () {
Detachedcriteria criteria = Detachedcriteria.forclass (Product.class);
Criteria.add (Restrictions.eq ("Is_hot", 1));
Criteria.addorder (Order.desc ("pdate"));
Return This.gethibernatetemplate (). Findbycriteria (criteria, 0, 10);
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Reflection application Case