1. Design basedao and basedaoimpl
1) Design Interface basedao
Each entity should have a corresponding Dao interface, which encapsulates database operations on this entity. Each Dao interface should have a basic addition, deletion, modification, and query method, but each DAO interface should write the code repeatedly.Extracted to a parent interface, Defined:
1 package COM. atguigu. surveypark. dao; 2 Import Java. util. list; 3/** 4 * basedao interface 5 */6 public interface basedao <t> {7 // write operation 8 Public void saveentity (t ); 9 Public void saveorupdateentity (T); 10 public void updateentity (T); 11 Public void deleteentity (t); 12 public void batchentitybyhql (string hql, object... objects); // update in batches according to hql statements 13 // execute native SQL statement 14 public void executesql (string SQL, object... objects); 15 16 // read operation 17 Public t loadentity (integer ID); 18 public t getentity (integer ID); 19 public list <t> findentitybyhql (string hql, object... objects); 20 // Single-value retrieval ensures that there is only one query result record 21 public object uniqueresult (string hql, object... objects); 22 // execute the native SQL query (you can specify whether the object is encapsulated) 23 public list executesqlquery (class clazz, string SQL, object... objects); 24}
2) design implementation class basedaoimpl
Each Dao interface must have corresponding implementation classes. In each daoimpl, all methods defined in the DaO interface must be implemented, of course, this also includes public addition, deletion, modification, and query methods (basic methods defined in basedao ). Each daoimpl implements a public method display that is repeated, so you can alsoExtracted as a parent class basedaoimplAll methods of the basedao interface are implemented in basedaoimpl. We only need to inherit the daoimpl to implement public methods without repeated writing.
The statement is as follows:
1 package COM. atguigu. surveypark. dao. impl; 2 Import Java. lang. reflect. parameterizedtype; 3 Import Java. util. list; 4 Import javax. annotation. resource; 5 import Org. hibernate. sessionfactory; 6 Import COM. atguigu. surveypark. dao. basedao; 7/** 8 * abstract DAO implementation, specifically used to inherit 9 */10 @ suppresswarnings ("unchecked ") 11 public abstract class basedaoimpl <t> implements basedao <t> {12 @ resource13 private sessionfactory; // inject sessionfactory14 private class into spring <t> clazz; 15 public basedaoimpl () {16 // obtain the generic superclass (Return Value: parameterized type) 17 parameterizedtype type = (parameterizedtype) This. getclass (). getgenericsuperclass (); 18 clazz = (class <t>) type. getactualtypearguments () [0]; 19} 20 21 protected session getsession () {// get session22 return sessionfactory. getcurrentsession (); 23} 24 // omitted the implementation of the method in the docking port 25}
Here we useGeneric Technology, You need to determine the specific type of the generic t clazz during program execution.There are two methods to obtain clazz:★★★★
Method 1:
I. Declare clazz as the protected modifier so that the sub-classes can access it,
Ii. Pass the value of this attribute in the constructor of each subclass, for example:
Public roledaoimpl () {clazz = role. Class ;}
Public userdaoimpl () {clazz = user. Class ;}
Method 2:UseReflection Method(That is, the method used in basedaoimpl ):
I. Write the following code in the default basedaoimpl constructor:
Parameterizedtype Pt = (parameterizedtype) This. getclass (). getgenericsuperclass ();
Clazz = (class) pt. getactualtypearguments () [0];
Ii. Note: Even if basedaoimpl is not defined as an abstract class, it cannot be used directly. It can only use its subclass. Otherwise, this code will be invalid. The clazz type can be obtained in the basedaoimpl constructor only after its subclass specifies the generic type.
2. Design baseservice and baseserviceimpl
1) Design Interface baseservice
When designing baseservice, an unwritten rule is to copy all the basic methods in basedao to baseservic. If there is a business method that needs to be extended, then independently design the corresponding service interface, then inherit the baseservice interface.
Therefore, baseservice is defined as follows:
1 package COM. atguigu. surveypark. service; 2 Import Java. util. list; 3/** 4 * Basic Service Interface 5 */6 public interface baseservice <t> {7 // write operation 8 Public void saveentity (t ); 9 Public void saveorupdateentity (T); 10 public void updateentity (T); 11 Public void deleteentity (t); 12 public void batchentitybyhql (string hql, object... objects); 13 // execute the native SQL statement 14 public void executesql (string SQL, object... objects); 15 16 // read operation 17 Public t loadentity (integer ID); 18 public t getentity (integer ID); 19 public list <t> findentitybyhql (string hql, object... objects); 20 // Single-value retrieval ensures that there is only one query result record 21 public object uniqueresult (string hql, object... objects); 22 // query all objects 23 public list <t> findallentities (); 24 public list executesqlquery (class clazz, string SQL, object... objects); 25}
2) design implementation class baseserviceimp
Baseserviceimpl also adopts generic technology to facilitate program implementation. Baseserviceimpl injects Dao through spring, and then calls the implementation of the basic method in basedaoimpl through Dao to complete basic business operations.
The specific implementation is as follows:
1 package COM. atguigu. surveypark. service. impl; 2 Import Java. lang. reflect. parameterizedtype; 3 Import Java. util. list; 4 Import javax. annotation. resource; 5 import COM. atguigu. surveypark. dao. basedao; 6 Import COM. atguigu. surveypark. service. baseservice; 7/*** abstract service implementation, it is used to inherit 9 */10 public abstract class baseserviceimpl <t> implements baseservice <t> {11 private basedao <t> Dao; 12 private class <t> clazz; 13 @ suppresswarnings ("unchecked") 14 public baseserviceimpl () {15 parameterizedtype = (parameterizedtype) This. getclass (). getgenericsuperclass (); 16 clazz = (class <t>) type. getactualtypearguments () [0]; 17} 18 @ resource // inject dao19 public void setdao (basedao <t> Dao) {20 this. dao = Dao; 21} 22 // omitted the implementation of the method in the docking port 23}
Note: The Implementation class of the basedao interface is injected here, And the annotation @ resource is added to the setdao method. This is because basedao has multiple implementation classes. Therefore, when Dao injection is performed in spring, multiple matching implementation classes of basedao meet the conditions. Therefore, the @ resource annotation cannot be added to attributes, instead, add it to the set method. In this way, you can override the setdao method in a specific service implementation class and add the name attribute in the @ resource annotation to specify the specific DAO implementation class to be injected. As follows:
1 // userservice interface 2 public interface userservice extends baseservice <user >{} 3 // userserviceimpl implementation Class 4 @ Service ("userservice ") 5 public class userserviceimpl extends baseserviceimpl <user> implements userservice {6/** 7 * rewrite this method to overwrite the annotation of this method in the superclass and specify to inject the specified Dao object, otherwise, spring 8 * cannot determine which Dao to inject --- there are multiple Dao that meet the conditions. 9 */10 @ Resource (name = "userdao") 11 Public void setdao (basedao <user> Dao) {12 super. setdao (DAO); 13} 14}
Survey Management System-(4) Generic extraction and implementation of DAO and service layers