Let the domain object (Domain Object) vertically

xiaoxiao2021-03-06  14

- In combination with WebWork2 and Hibernate simplified system structure, improve development efficiency

Primer

If the narrow understanding, many times, a job running on the B / S platform is to accept the information input from the browser side. Store the database after processing, or from the database to extract data processing and format the post-pass The browser is displayed to the user. However, let the application designer is very frustrating: the browser is very weak, only information that is extremely low in complexity, such as the form of text information; the mainstream database is a relational database, does not directly store the face The field model data of the object; the application system is often designed, developed in an object-oriented manner. From the Form that can only pass the simple data type, to the object-oriented system, then to the relational database, we must cross the annoying system boundaries, that is, we must perform multiple forms of morphological conversion of the same data to implement system functions. In order to solve this problem, the system design a lot of solutions, such as: using the FormBean package from the browser to pass the simple type data (or some common type of shape, but said, or simply type, not Field model Domain model, with VO (different design is different, but the idea is the same) to make a one-to-one mapping of Tables-JavaBeans for relational databases.

In this way, the Table of the browser's FORM and relational database has been object-oriented. In a sense, it plays a function of an adapter, hidden the bottom layer of the browser's FORM and the database. Information simplifies system design and has achieved good results during a considerable period of time. However, as the complexity of business system is increased, this solution shows many flaws:

Due to the introduction of FORMBEAN, VO, the two types of Domain Object are not related to Domain Object, the developer is scattered attention to the field of object itself, and the complexity of the system has increased the system's complexity, and the system's operational efficiency is reduced, and the coupling of the system, Necessary effects have been negative. It can be considered that this is no longer the best, most effective solution, and we need to improve this treatment.

Entry

In recent years, there have been many mature solutions:

OGNL (http://www.ognl.org) and other settings, get the problem of the language of Java Object Properties solved the problem of the UI system such as the browser with the core business module interaction effect, and many front-end controllers (Front Controller It is good to achieve OGNL, such as webwork2, etc .; Hibernate (http://www.hibernate.org) O / R Mapping has solved uncoordinated from an object system to the relational database.

As a result, in the development, we don't have to be forced to convert between different JavaBeans when crossing the logical level.

achieve

Establish a detailed and meticulous business model, such as: EMPLOYEE

Package info.powersoft.sample;

Import java.util.date;

/ **

*

description:

*

Copyright: Copyright (c) 2005

* @Author Guangzong.xu

* @since

2005-3-17

* @version 1.0

* /

/ **

* @Author Guangzong.xu

*

* Todo to change the template for this generated Type Comment Go To * Window - Preferences - Java - Code Style - Code Templates

* /

PUBLIC CLASS EMPLOYEEEE {

Private long id;

PRIVATE STRING NAME;

PRIVATE DATE DATEOFBIRTH;

PRIVATE DEPARTMENT

/ **

* @Return Returns the dateofbirth.

* /

Public Date getDateOfbirth () {

Return DateOfbirth;

}

/ **

* @Param DateOfbirth The dateofbirth to set.

* /

Public void setdateOfbirth (date dateofbirth) {

THIS.DATEOFBIRTH = DATEOFBIRTH;

}

/ **

* @Return Returns the department.

* /

Public department getDepartment () {

Return Department;

}

/ **

* @Param Department The Department To Set.

* /

Public void setDepartment (department department) {

THIS.DEPARTMENT = Department;

}

/ **

* @Return Returns the ID.

* /

Public long getId () {

Return ID;

}

/ **

* @Param id the ID to set.

* /

Public void setid (long id) {

THIS.ID = ID;

}

/ **

* @Return Returns the Name.

* /

Public string getname () {

Return Name;

}

/ **

* @Param Name the name to set.

* /

Public void setname (String name) {

THIS.NAME = Name;

}

}

Department

Package info.powersoft.sample;

/ **

*

description:

*

Copyright: Copyright (c) 2005

* @Author Guangzong.xu

* @since

2005-3-17

* @version 1.0

* /

/ **

* @Author Guangzong.xu

*

* Todo to change the Template for this generated Type Comment Go To to

* WINDOW - Preferences - Java - Code Style - Code Templates

* /

Public class divance {

Private long id;

PRIVATE STRING NAME;

Private Department SuperDepartment;

/ **

* @Return Returns the ID.

* /

Public long getId () {

Return ID;

}

/ **

* @Param id the ID to set.

* /

Public void setid (long id) {this.id = ID;

}

/ **

* @Return Returns the Name.

* /

Public string getname () {

Return Name;

}

/ **

* @Param Name the name to set.

* /

Public void setname (String name) {

THIS.NAME = Name;

}

/ **

* @Return Returns The SuperDepartment.

* /

Public department getsuperDepartment () {

Return SuperDepartment;

}

/ **

* @Param SuperDepartment The SuperDepartment To Set.

* /

Public void setsuperDepartment (department superDepartment) {

THIS.SUPERDEPARTMENT = SuperDepartment;

}

}

DAO

Package info.powersoft.sample;

/ **

*

description:

*

Copyright: Copyright (c) 2005

* @Author Guangzong.xu

* @since

2005-3-17

* @version 1.0

* /

/ **

* @Author Guangzong.xu

*

* Todo to change the Template for this generated Type Comment Go To to

* WINDOW - Preferences - Java - Code Style - Code Templates

* /

Public interface Employeedao {

Long InsertemPloyee (EMPLOYEE EMPLOYEE);

}

Use WebWork2 as a Front Controller, and set the service model as a variable in the corresponding action, set the corresponding getter, setter, such as

Package info.powersoft.sample;

/ **

*

description:

*

Copyright: Copyright (c) 2005

* @Author Guangzong.xu

* @since

2005-3-17

* @version 1.0

* /

/ **

* @Author Guangzong.xu

*

* Todo to change the Template for this generated Type Comment Go To to

* WINDOW - Preferences - Java - Code Style - Code Templates

* /

Public class employeaction {

Private Employeedao Employeedao;

PRIVATE EMPLOYEE EMPLOYEE

/ **

* @Return Returns the Employeedao.

* /

Public employeedao geteMPloyeedao () {

Return Employeedao;

}

/ **

* @Param Employeedao the Employeedao to set.

* /

Public void setEmployeedao (Employeedao Employeedao) {this.employeedao = Employeeda

}

/ **

* @Return Returns the Employee.

* /

Public Employee getEmployee () {

Return Employee;

}

/ **

* @Param Employee The Employee To Set.

* /

Public void setemployee (Employe Employee) {

this.employee = Employee;

}

Public String Execute () throws exception {

Employeedao.insertemployee (Employee);

Return "Success";

}

}

Where EMPLOYEDAO is DAO for stored domain objects into the database, and specific implementations can be used by Hibernate. This is familiar, not here.

The page sent to the browser is

test </ title></p> <p></ hEAD></p> <p><body></p> <p><form name = "form1" method = "post" action = "employee.action"></p> <p><p></p> <p><Input name = "Employee.name" type = "text"></p> <p></ p></p> <p><p></p> <p><Input name = "Employee.dateOfbirth" type = "text"></p> <p></ p></p> <p><p></p> <p><Input name = "Employee.Department.id" type = "text"></p> <p></ p></p> <p><p></p> <p><Input Type = "Submit" name = "submit" value = "submit"></p> <p></ p></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>We can notice the name of the text, text input box (INPUT TYPE = "text") is similar to such something: "Employee.Department.ID", the interception of WebWork2 intercepts, automatically put these FORM The fields in the field are assembled to our Employee objects for our convenient call, eliminating the conversion between FormBean to Domain Object. In the same reason, we can also send these domain objects to the view layer to format the display (this is to use some representations that support specific functions, such as JSP2.0, FreeMarker, etc.)</p> <p>Finish</p> <p>At this point, we have completed the input from the browser to the storage into the database. In this process, we have been the same domain object (Domain Object) to carry the information needed by the application, without being forced to perform data conversion, the code is relatively simple, structure It is relatively clear, and at the same time, the implementation efficiency is also improved compared with the traditional way.</p> <p>Of course, this article only introduces this implementation from the infarction, and the code provided does not run directly. If you have a good idea during the process of experience, or encounter problems, welcome to communicate with me Boyxgz@msn.com</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-46597.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="46597" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.051</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'xwSXNOMW61A2VjEmdCKeVwu7qwhAU0DsrZCM1UDabPvS_2FjASDq7zkgnN1hv3nbT6AROSR3arzJS2CCV_2FcRqAPw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>