Java Reflection application Case

Source: Internet
Author: User

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

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.