When using the struts+hibernate+spring framework, discovering the code for the data access layer generated by spring for the application is very repetitive, which is very uncomfortable, basically how many DAO, each DAO is a duplicate method, is to change the specific corresponding Pojo class just, this feeling is very contrary to the principle of Java code reuse, so I want to do something to improve it, so that the reuse rate of the code a bit higher, the real embodiment of Java in the method of the elegance of reuse, I think of the Java reflection mechanism to implement, The following is the specific code snippet (for the Java reflection mechanism does not understand the friend, you can refer to my original written about the Java reflection mechanism of the article).
Look at one end of the code, which is used to save diary this object, if you have a user, or department, or more objects that need to be saved, then the following method in your corresponding Userdao,departmentdao class will still appear again , I don't know if you look cool, at least I feel very redundant. public void Save (diary transientinstance) ... {
Log.debug ("Saving Diary instance");
Try ... {
Gethibernatetemplate (). Save (Transientinstance);
Log.debug ("Save Successful");
catch (RuntimeException re) ... {
Log.error ("Save Failed", re);
throw re;
}
}
Then I'm going to make improvements, and I let all the specific DAO classes implement a Basedao class, and in the Basedao class the above methods will look like this:/** * */**
* Save Objects
*
* @param Object
*/
public void Save (object) ... {
Log.info ("Save" + Object.getclass (). GetName () + "instance");
Try ... {
Gethibernatetemplate (). Save (object);
Log.info ("Save" + Object.getclass (). GetName () + "instance success");
catch (RuntimeException re) ... {
Log.info ("Save" + Object.getclass (). GetName () + "instance failed", re);
throw re;
}
}
The above method does not really use the Java reflection mechanism, but only when the object is printed, a little bit, then look at the following code, you will feel the benefits of the Java reflection mechanism.
Query objects by ID:/**
* Query objects by ID
*
* @param Object
* @param ID
* @return
*/
Public Object FindByID (Class Object, Java.lang.Integer ID) {
Log.info ("Pass:" + ID + "Query" + object.getcanonicalname () + "instance");
try {
object instance = (object) gethibernatetemplate (). Get (
Object.getcanonicalname (), id);
return instance;
catch (RuntimeException re) {
Log.info ("through:" + ID + "Query" + object.getcanonicalname () + "instance failed",
RE);
throw re;
}
}
Always remember hibernate to query objects by ID, such as Get ("Teacher", New Integer (1)), so hard-coded way in front of the Java reflection Mechanism has become so flexible, really fall in love with the Java reflection mechanism is not said.
Then look down.
According to the example query object, there is nothing to say, even if no Java reflection can be completed, but with reflection, more convenient for us to know where the problem
/**
* Follow Example Inquiry
*
* @param instance
* @return
*/
Public List Findbyexample (Object instance) {
Log.info ("Use example query" + instance.getclass (). GetName () + "instance");
try {
List results = Gethibernatetemplate (). Findbyexample (instance);
Log.info ("Use example query" + instance.getclass (). GetName ()
+ "Successful instance, query out the number of records is:" + results.size ());
return results;
catch (RuntimeException re) {
Log.info ("Use example query" + instance.getclass (). GetName () + "instance failed",
RE);
throw re;
}
}
And then there's the best case for using reflection.
Query by attribute: It's more common to do queries in this way, but if you're using JDBC, you don't know how many string versions of SQL We're going to write, and now there's spring to help manage, it's really much easier to see
Take this approach seriously and find that the code for this method is still quite fluent and elegant, at least I would be very cumbersome to write, which uses the Java reflection mechanism, so that the method to become more general, in the project, I in accordance with the different conditions for count, did not seriously look at this method, Because then encountered the problem is if the use of HQL to do the query, if the conditions of Chinese appear, the Chinese condition will be treated as garbled, and then forced no way, incredibly written in the following code: Query query = NULL;
String hql = "SELECT count (*) from Student as S";
query = Session.createquery (HQL);
if (Stu.getstatuid ()!= null) ... {
hql=hql+ "Where s.studentstatu.statuid=" +stu.getstatuid ();
Query=session.createquery (HQL);
}
if (STU.GETCLASSID ()!= null) ... {
hql=hql+ "and s.classes.classesid=" +stu.getclassid ();
query=