OA Project notes-implementing CRUD from frameworks such as the Build Interface DAO Impl action jsp

Source: Internet
Author: User

1, Design Basedao and Basedaoimpl 1, design interface Basedao 1, each entity should have a corresponding DAO interface, encapsulating the database operations on this entity. Example entity DAO interface implementation Class ======================================================== User--Userdao-Userdaoimpl Role--&gt ; Roledao--Roledaoimpl Department---Departmentdao, Departmentdaoimpl article--Articledao Ledaoimpl ... 2, in each DAO interface should have a basic method of adding and deleting, but each DAO interface is written again is duplicated code, you can extract these methods into a parent interface, defined as: public interface Basedao {Save (Entity:t): void Delete (id:serializable): void Update (ENTITY:T): void GetById (id:serializable): T getbyids (ids:serializable[ ]): List findAll (): List} 3, Description: 1, using generic technology, can be more convenient when used, such as: User user = Userdao.getbyid (1L); No forced Transformation 2,getbyid (Id:long) and Getbyids (ids:long[]) do not merge into a method with variable parameters, because it is inconvenient to use, for example, it is inconvenient to get an object based on an ID frequently: List li st = Userdao.getbyids (1L); User user = List.size () > 0? List.get (0): null. 3, after the DAO interface can inherit this Basedao, to have the basic additions and deletions to the method, such as: Userdao extends Basedao Roledao extends Basedao 2, design implementation class Basedaoimpl 1, each DAO interface must have the corresponding implementation class to, all the methods defined in the DAO interface are implemented in each Daoimpl, including, of course, the public additions and deletions method (the 6 basic methods defined in Basedao). 2, every Daoimpl is implemented in the public method is clearly duplicated, so it can also be extracted for a parent class Basedaoimpl, in Basedaoimpl to implement the Basedao interface of all methods, Our Daoimpl only need to inherit him to write the public method without repeating the implementation. The declaration is as follows: public class Basedaoimpl implements Basedao {@Resource private sessionfactory sessionfactory;//Let Spring inject SESSIONFA Ctory private Class Clazz; To find a way to get T's class public void Save (T entity) {} public void Delete (Long id) {} public void update (T entity) {} public T GETBYID (Long ID) {} public list getbyids (long[] IDs) {} public list FindAll () {}} When implementing the method, the code to get the session is: Sessionfactory. Getcurrentsession () 3, in the acquisition of Clazz there are two methods 1, method one: 1, the clazz is declared as protected modifier, this kind of class can be accessed, 2, in each subclass of the construction method to pass the value of this property, such as: public Roledaoimpl () {clazz = Role.class;} public Userdaoimpl () {clazz = User.class;} 2, method Two: Use reflection as: 1, in the default constructor method of Basedaoimpl, write as The following code is available: Parameterizedtype pt = (parameterizedtype) this.getclass (). Getgenericsuperclass (); Clazz = (Class) pt.getactualtypearguments () [0]; 2, Description: Basedaoimpl can not be used directly, can only use his subclass, otherwise this code is invalid.1, design entity/table design entities----JavaBean-----hbm.xml----build table 2, which analyzes several features that correspond to several requests. 3, implement the function: 1, write the action class, write the method in the action, determine the method in the service. 2, write the service method, determine the method in DAO. 3, write the DAO method. 4, write JSP ============================ request number Address bar forwarding 1 invariant redirect 2 change add and change to find a total of 4 functions, requires 6 requests. Therefore, the corresponding 6 action methods are required, and each action method handles a request. The function method name returns a value corresponding to the page----------------------------------------------------list () list.jsp remove Delete () toList add page Addu I () Addui addui.jsp Adding Add () toList modify Page Editui () Editui editui.jsp Modify edit () toList role_list--------------------------- -----------------------------------------------------------------------------notes: Public interfaces in the design of public interfaces and their implementation classes (additional deletions), Use generics, do not specify type implementation class implements public interface, implement methods in common interface and modify (class Basedaoimpl implements Basedao) design entity/table Design Entity--JavaBean---HBM The. XML-to-build table analysis has several features that correspond to several requests. Implement the function: 1, write the action class, write the method in the action, and determine the method in the service. 2, write the service method, determine the method in DAO. 3, write the DAO method. 4, write the JSP to put the object in the value stack map (JSP with # get value): Actioncontext.getcontext (). Put ("Rolelist", rolelist); Add Information method://1, encapsulate to object role role=new role (); Role.setname (model.geTname ()); Role.setdescription (Model.getdescription ()); 2, Save to Database Roleservice.save (role); or directly save the return "ToList"; Edit Information page Save method: Save the object to the object stack: Actioncontext.getcontext (). Getvaluestack (). push (role); Get Stack object: Public Role Getmodel () {return model;}

OA Project notes-implement CRUD from frameworks such as the Build Interface DAO Impl action jsp

Related Article

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.