A kind of MVC model system data processing method and MVC model system
Technical field
The present invention relates to technical field of data processing, relate in particular to a kind of MVC (Model ViewController, model view controller) model system data processing method and MVC model system.
Background technology
MVC (Model View Controller, the model view controller) model system generally is comprised of model, view and view controller, it allows each inter-module loosely coupling of each part of composition system, so that program development is more flexible. and reduce repeated code and code is reused, wherein:
View shows the user to the information of representation model data and logical relation and state with particular form, and its model obtains demonstration information, for identical information a plurality of different display formats or view can be arranged.
Controller is the interactive operation of process user and software. its responsibility is the propagation of any variation during control supplies a model, and guarantees the corresponding relationship between user interface and model; It accepts user's input, and input feedback to model, and then is realized calculating control to model. and be the parts that make model and view co-ordination.
Model is preserved by view and is shown, by the data of controller control; It has encapsulated the calculated relationship of the very corn of a subject data, logic and function. be independent of concrete interface expression and I/O operation.
Described MVC model system is followed 3 layers of principle: i.e. presentation layer, logical layer and persistent layer, described three layers fully separate to reach the division of labor clear and definite, purpose easy to maintenance.Described presentation layer is corresponding with view, and corresponding with controller and the described persistent layer of described logical layer is corresponding with model.
The common function of logical layer and persistent layer is to provide data to presentation layer, and difference is that persistent layer is responsible for storing data, namely submits static data to logical layer; And logical layer is processed these data, and the result after will processing is presented at presentation layer.
Fig. 1 is the event flow graph of the MVC model system of classics.View 101 and controller 102 are mutual, and view 101 is received order and just sent request to controller 102, and controller 102 is issued view 101 with data processed result needing the requirement of deal with data to issue model 103 after model 103 modifications are complete.It can respond a plurality of views by a plurality of controllers are set, in order to allow them share same model.Thus, the MVC model system has played great role in the application program that realizes the unit real-time update, the visual series of picture microsoft, but there is following defective simultaneously in it: in Web uses, because view is to generate in the browser of client, controller and model then are at server end, cause thus using one of web application needs and the distinct design of MVC of HTTP request/response pattern, and it is the non-constant of extensibility in large-scale corporation's type is used, in case customer requirement changes, and just needs wholesale revision.
For this reason, prior art has proposed a kind of MVC model system, and this system 200 comprises as shown in Figure 2: presentation layer module 201, logic layer module 202 and persistent layer module 203, wherein:
Logic layer module 202, also can be called controller, but different from Fig. 1, it not only plays a part bridge, also bear the effect that data are processed, it can come and as required data be processed according to needs reading out data from persistent layer module 203 of presentation layer module 201, then passes to presentation layer module 201; Perhaps according to the data demand Update Table of presentation layer module 201, and upgrade persistent layer module 203.Persistent layer module 203 also can be called model, only is responsible for the most basic database manipulation, namely retrieves (inquiry), inserts, deletes, revises.
Fig. 2 is the event flow graph of MVC model system 200, presentation layer module 201 sends request of data to logic layer module 202, this request of data is carried and is comprised the data relevant with database and the data of independent of database, logic layer module 202 is transmitted to persistent layer module 203 with request of data, persistent layer module 203 is returned undressed data to logic layer module 202 by database manipulations such as inquiries, thereby after logic layer module 202 is processed described data presentation layer module 201 is responded the data processing that realizes that Web uses.
The power and responsibility of this model system 200 is distributed clearer and more definite, and has weakened the coupling between service logic interface and the data-interface, is convenient to expansion, is easy to safeguard.But this model system 200 is high extremely long with the processing time to space requirement in the situation of needs processing mass data.
The tabulation of depositing stage property consumption in the database referring to table 1 illustrates the data handling procedure of this model system 200.Table 1 is responsible for every kind of stage property of record sales situation of every day.
Table 1
ID |
Name |
Type |
Quantity |
Date |
This model system 200 is according to the moon sales volume of every type of stage property of stage property type statistics, and calculates their ratio.Its data processing method one: persistent layer module 203 is taken out all data from database, and they are deposited in the large set, and then logic layer module 202 is calculated according to this set.Be implemented as follows:
1) the persistent layer module 203:
public?List?find(Date?start,Date?end){
private?Entity?Manager?manager;
return?manager.createQuery(“select?a.Type,a.Quantity?from?SALE?a?where
a.time>=:pa1?and?a.time<=:pa2”).
setParameter(“pa1”,start).setParameter(“pa2”,end).
getResultList();
}
2) logic layer module 202:
List?quanlity=Remote.find(monthstart,monthend);
Remote is the interface of persistent layer module 203, deposit among the quanlity January all stage properties sales situation, the variable of List type has just been stored the data in all January like this.Ensuing thing is exactly simple circulation summation, calculates one by one their quantity and calculates ratio in quantity according to the difference of stage property type.
In model system 200,203 responsible data access of persistent layer module, logic layer module 203 processing said data.When described quanlity is very large, logic layer module 203 needs the sales data of stage property to be processed month just a lot, the words of calculating with the project actual demand, had 10,000 data in one month at least, hypothesis is 100,000 in the time of experiment, and some engineering may be more, and this just means that needing length is to be at least 10,000 LIST (tabulation).If the sales situation of adding up a year use said method then extremely to waste storage space, and the 202 circular treatment times of logic layer module is long, efficient is very low, needs thus to have high performance processor and waste staff's quality time.
Model system 200 data processing methods two: by reading one by one with repeatedly reading out data is to reduce the volume of LIST, it is equivalent to cyclic part is put in the data access process, is implemented as follows:
1) the persistent layer module 203
public?List?find(Date?start,Date?end,int?type){
private?EntityManager?manager;
return?manager.createQuery(“select?a.Type,a.Quantity?from?SALE?a?where
a.time>=:pa1?and?a.time<=:pa2?and?a.Type=:pa3”).
setParameter?(“pa1”,start).setParameter(“pa2”,end)
setParameter(“pa3”,type).getResultList();
}
2) logic layer module 201
int?quantity=Object.find(monthStart,monthEnd,type);
Type is the stage property type, this data processing method can be dwindled the size of LIST at double, the multiple that dwindles is the species number of stage property, that is to say if there are 100 kinds of stage property LIST will dwindle 100 times, can also circulate to fate in month, can dwindle again 30 times thus, can narrow down to 1 to the size of LIST in theory, also just become one by one to fetch data.Can solve thus the waste of storage space problem that produces in the data processing method one.But only be only applicable to the few and rapidly situation of communicating by letter of data in these model system 200 data processing methods two, can't be applicable to distributed data base.In this data processing method two, the travelling speed of restriction enterprise-level program be logic layer module 201 in the model system 200 and the part of persistent layer module 203 mutual communications, that is to say that it is time-consuming maximum reading at every turn.Take read at every turn the time as 0.1 second as benchmark, want 1000 seconds for 10,000, and in fact this time might be longer, especially in the situation of distributed data base.
In summary, the realization technical scheme that existing MVC model system and MVC model system data are processed obviously exists inconvenience and defective, in actual use so be necessary to be improved.
Summary of the invention
For above-mentioned defective, the present invention's the first purpose is to provide a kind of MVC model system data processing method, and the method data take up room little, and data processing time is short, and efficient is high.
The present invention's the second purpose is to provide a kind of MVC model system, and these MVC model system data take up room little, and data processing time is short, and efficient is high.
In order to realize above-mentioned the first purpose, the invention provides a kind of MVC model system data processing method, this MVC model system comprises: presentation layer module, logic layer module and persistent layer module, described method comprises the steps:
A, presentation layer module send data processing request to logic layer module;
B, logic layer module are sent to the persistent layer module according to described data processing request with the data relevant with database;
C, the described data relevant with database of persistent layer resume module.
According to described method, described step B further comprises:
B1, logic layer module receive described data processing request;
B2, logic layer module are data relevant with database and the data of independent of database according to the described data processing request data distribution that it is entrained;
B3, logic layer module are sent to the persistent layer module with the described data relevant with database, and process the data of described independent of database.
According to described method, further comprise before the described steps A:
A0, teleaction service interface, session Bean and simple traditional JAVA object class are set in the persistent layer module, described simple traditional JAVA object class is corresponding one by one with table in the database;
Logic layer module is sent to the persistent layer module by described teleaction service interface with the data relevant with database among the step B2;
The persistent layer module is processed the described data relevant with database by described session Bean with simple traditional JAVA object class among the step C.
According to described method, the QL injecting data of described session Bean by EJB3.0 be to simple traditional JAVA object class, thereby realize that the persistent layer module is to the processing of described data relevant with database.
According to described method, described session Bean realizes that by Calling Stored Procedure the persistent layer module is to the processing of the relevant data of database.
According to described method, further comprise after the described step C:
The data relevant with database after D, persistent layer module will be processed return to logic layer module;
The data relevant with database after the data of the independent of database after E, described logic layer module process self and the persistent layer resume module return to the presentation layer module with the data processing request of response presentation layer module transmission
In order to realize above-mentioned the second purpose, the invention provides a kind of MVC model system, this MVC model system comprises: presentation layer module, logic layer module and persistent layer module,
Described presentation layer module is used for sending data processing request to logic layer module;
Described logic layer module is used for according to described data processing request the data relevant with database being sent to the persistent layer module;
Described persistent layer module is for the treatment of the described data relevant with database.
According to described MVC model system, the data distribution that it is entrained was the data relevant with database and the data of independent of database after described logic layer module received described data processing request, and the described data relevant with database are sent to the persistent layer module.
According to described MVC model system, described persistent layer module also further arranges teleaction service interface, session Bean and simple traditional JAVA object class, and described simple traditional JAVA object class is corresponding one by one with table in the database;
Logic layer module is sent to the persistent layer module by described teleaction service interface with the data relevant with database;
The persistent layer module is processed the described data relevant with database by described session Bean with simple traditional JAVA object class.
According to described MVC model system, the QL injecting data of described session Bean by EJB3.0 be to simple traditional JAVA object class, thereby realize that the persistent layer module is to the processing of described data relevant with database.
The statistics of MVC model system of the present invention in according to the actual requirements, be the data relevant with database and the data of independent of database with the data separating of the required processing of logic layer module, and will the data relevant with database transfer to the persistent layer module and process, take full advantage of thus the peculiar function of database such as summation, the characteristics such as it is rapid to sort have reduced the volume of logic layer module and persistent layer module data, the speed of greatly raising program operation, data processing time is short thus, and efficient is high.
Description of drawings
Fig. 1 is the event-flow graph of the MVC model system that provides of prior art one;
Fig. 2 is the event-flow graph of the MVC model system that provides of prior art two;
Fig. 3 is the event-flow graph of MVC model system provided by the invention;
Fig. 4 is that the persistent layer module that the preferred embodiment of the present invention provides arranges module map;
Fig. 5 is MVC model system data processing method process flow diagram provided by the invention;
Fig. 6 is the MVC model system data processing method process flow diagram that first, second embodiment of the present invention provides.
Embodiment
In order to make purpose of the present invention, technical scheme and advantage clearer, below in conjunction with drawings and Examples, the present invention is further elaborated.Should be appreciated that specific embodiment described herein only in order to explain the present invention, is not intended to limit the present invention.
Basic thought of the present invention is, be the data relevant with database and the data of independent of database with the data separating of the required processing of logic layer module, thereby and will the data relevant with database transfer to the persistent layer module and process and realize that data take up room little, and data processing time is short, the purpose that efficient is high.
MVC model system 300 provided by the invention as shown in Figure 3, this MVC model system 300 comprises: presentation layer module 301, logic layer module 302 and persistent layer module 303, wherein:
Presentation layer module 301 is used for sending data processing request to logic layer module 302.This data processing request is carried the data relevant with database and the data of independent of database, and the data of this independent of database comprise service logic.
Logic layer module 302 is used for according to the described data processing request that presentation layer module 301 sends the data relevant with database being sent to persistent layer module 303.
Particularly, after this logic layer module 302 receives the data processing request of presentation layer module 301 transmissions, data are divided into the data relevant with database and the data of independent of database according to shunting soon according to the data type different pairs, and will the data relevant with database be sent to persistent layer module 303 and process, and the data of oneself processing according to demand independent of database are service logic etc.
Persistent layer module 303 is for the treatment of the described data relevant with database of logic layer module 302 transmissions.
Described persistent layer module 303 is provided with teleaction service interface, session Bean and POJO (Plain OldJava Object, simple traditional JAVA object) class, and described POJO class is corresponding one by one with table in the database.
The present invention preferably adopts EJB3.0 (Enterprise Java Bean 3.0, enterprise's handle) to realize the above-mentioned set-up mode of persistent layer module 303.
JDBC in early days, ibatis afterwards also have the most popular recently Hibernate that query function is provided, but they respectively have shortcoming.The a large amount of try statement of JDBC is the main cause that the java programmer is hung back, and a large amount of configuration of ibatis with use concrete SQL statement to allow its transplantability become problem, and the HQL language ability that hibernate provides is limited.EJB3.0 has concentrated ibatis and hibernate in the realization advantage of persistent layer module 303.EJB3.0 can provide and SQL (Structured QueryLanguage, Structured Query Language (SQL)) statement is inquired about one to one, and EJB3.0 has abandoned the framework of former heavyweight, and can provide the framework of lightweight, be provided with the teleaction service interface by EJB3.0 in persistent layer module 303 thus, session Bean and POJO class can utilize the powerful processing power advantage of Database Systems self to allow it shoulder the responsibility of processing a part of data, thereby reduced the data transmission of magnanimity, save the time loss that memory headroom and transmission bring, finally can improve the effect of overall operation speed.
Fig. 4 is that of persistent layer module 303 preferably arranges module map.Be provided with Bean (doubleBean.java) and the class (double.java) corresponding to Database field of database interface (doubleRemote.java), realization interface in this persistent layer module 303 by EJB3.0.
Database interface (doubleRemote.java) is the teleaction service interface, mainly be to define logic layer module 302 method that needs and the interface that provides data to process to call for logic layer module 302, logic layer module 302 can be sent to the data relevant with database persistent layer module 303 by this interface.
The Bean (doubleBean.java) that realizes interface is also referred to as session Bean, is used for the fulfillment database interface.Can be injected in the data by the QL (Query Language, query language) that directly inlays EJB.
The QL of EJB3.0 can change into SQL statement corresponding to database, the almost function corresponding with stsndard SQL that it provides.That is to say what database can be finished, it can be finished.
For example the user logins this modal function.Common way is to obtain active user's username and password from the presentation layer module, then logic layer module goes database root to get this user's password according to user name,, user name returns the non-existent result of user if not existing, if the user exists then returns user cipher.Logic layer module is checked the password that takes out in the password of user input and the database, identically then logins successfully, on the contrary then failure.MVC model system 300 implementations provided by the invention are as follows: doubleBean realizes that interface provides following method to call for logic layer module 302,
public?Object?find(String?name,String?password){
private?EntityManager?manager;
return?manager.createQuery(″select?a.ID?from?SALE?a?where?a.name=:pa1
and?a.password=:pa2).setParameter(″pa1″,name)
.setParameter(″pa2″,password).getSingleResult();
}
If return results just can successfully not log in for empty user, this step is judged by persistent layer module 303,302 effects that a bridge is provided of logic layer module.
Described session Bean can also realize by Calling Stored Procedure the processing of 303 pairs of data relevant with database of persistent layer module with the QL injecting data process that replaces EJB.
Calling Stored Procedure can make data-handling efficiency high, and system 300 is maintainable strong, and storing process java program out of joint is without any need for change, and in like manner the java program is out of joint, and storing process is also without any need for change.
The class that Database field is corresponding (double.java) is entity B ean for the POJO class, and it is corresponding one by one with table in the database.The field of its field and database also is mapping relations, can define by data element their attribute.Data element is the concept that JDK5.0 just introduces, and it can finish the configuration file operation that in the past needs a lot of time of expense with a simple form, can finish the function that data increase progressively such as a@.Can come table in the creation database by dual mode: a kind of is to create first class, and stipulates the attribute of each field in class with the form of data element, then the program operation will be seen in database that this has shown.Another kind of mode is to create first table in database, and then it is corresponding with it to create the POJO class.
When persistent layer module 303 process will the data relevant with database after, pass result back logic layer module 302, logic layer module 302 is further processed the described data of passing back, and the data of the independent of database of then himself being handled respond with the data processing request that presentation layer module 301 is sent with the persistent layer module 303 data back presentation layer modules 301 relevant with database that handle.
Fig. 5 is MVC model system data processing method provided by the invention, and this MVC model system 300 comprises as shown in Figure 3: presentation layer module 301, logic layer module 302 and persistent layer module 303.Described method comprises as follows:
Step S501, presentation layer module 301 sends data processing request to logic layer module 302.
Step S502, logic layer module 302 is sent to persistent layer module 303 according to described data processing request with the data relevant with database.
Step S503, persistent layer module 303 is processed the described data relevant with database.
In order better to describe the present invention, first embodiment of the invention provides a kind of MVC model system data processing method as shown in Figure 6, the preferred embodiment is that the tabulation of depositing stage property consumption in the database referring to table 1 comes the moon sales volume according to every type of stage property of stage property type statistics, and calculates their ratio.MVC model system 300 in conjunction with Fig. 3 is described, and this MVC model system 300 comprises: presentation layer module 301, logic layer module 302 and persistent layer module 303.The method specifically comprises as follows:
Step S601 arranges teleaction service interface, session Bean and POJO object class in persistent layer module 303, described POJO object class is corresponding one by one with table in the database.
Step S602, presentation layer module 301 sends data processing request to logic layer module 302.
Step S603, logic layer module 302 receives described data processing request.
Step S604, logic layer module 302 is data relevant with database and the data of independent of database according to the described data processing request data distribution that it is entrained.
Step S605, logic layer module 302 is sent to persistent layer module 303 by the teleaction service interface with the described data relevant with database, and processes the data of described independent of database.
Step S606, persistent layer module 303 is processed according to session Bean and POJO object class after receiving the data relevant with database.
In the present embodiment, the QL injecting data of the session Bean of persistent layer module 303 by EJB3.0 be to simple traditional JAVA object class, thereby realize that the persistent layer module is to the processing of described data relevant with database.Detailed process is as follows:
LIST?quantity=Object.find(monthstart,monthend);
Wherein, all stage properties of LIST storage are of that month sales volume.The implementation method of Find is as follows:
public?List?find(Date?start,Date?end,int?type){
private?EntityManager?manager;
return?manager.createQuery(“select?a.Type,sum(a.Quantity)from?SALE?a
where?a.time>=:pa1?and?a.time<=:pa2?group?by a.Type”)
.setParameter(“pa1”,start).setParameter(“pa2”,end).getResultList();
}
QL can directly utilize database to calculate the summation in January of selected data thus.
Step S607, the data relevant with database after persistent layer module 303 will be processed return to logic layer module 302.
Step S608, logic layer module 302 is further processed the described data relevant with database.
Step S609, the data relevant with database after the data of the independent of database after logic layer module 302 is processed self and the persistent layer resume module return to presentation layer module 301 with the data processing request of response presentation layer module 301 transmissions.
Table 2 is to adopt respectively the data processing method of MVC model system 200 data processing methods one, MVC model system 200 data processing methods two and first embodiment of the invention to 10,000,100,000, article 1,000,000, sales data is carried out the mean value of 5 data processing times of drawing of test, unit millisecond.
Table 2
Thus, can find out that the data processing method that first embodiment of the invention provides can save data processing time greatly, improve data-handling efficiency.
MVC model system data processing method and the first embodiment that second embodiment of the invention provides are similar, its flow process can be referring to Fig. 6, different from the first embodiment is: among the step S607, the session Bean of the second embodiment provided by the invention is the processing that realizes the data that 303 pairs of databases of persistent layer module are relevant by Calling Stored Procedure.Specific as follows:
1. the implementation method of persistent layer module 303:
public?Object?find(int?kind,Date?start,Date?end){
private?EntityManager?manager;
return?manager.createNativeQuery(″{call?Calculate(?,?,?)}″)
.setParameter(1,kind).setParameter(2,start).setParameter(3,end)
.getSingleList();
}
2. storing process is achieved as follows (environment MYSQL):
create?procedure?pro_test(in p_kind int(11),
in p_start?date,
in p_end date,
out p_count int(11))
begin
select?sum(count)into?p_count?from?table?where?kind=p_kind
and?date?between?p_start?and?p_end;
end$$
In the second embodiment of the invention, the name that needs only Calling Stored Procedure among the session Bean just can realize the statistics of data, and use storing process, being conducive to the division of labor realizes, storing process efficient is high and maintainable strong, storing process java program out of joint is without any need for change, and in like manner the java program is out of joint, and storing process is also without any need for change.
In sum, the statistics of MVC model system of the present invention in according to the actual requirements, be the data relevant with database and the data of independent of database with the data separating of the required processing of logic layer module, and will the data relevant with database transfer to the persistent layer module and process, take full advantage of thus the peculiar function of database such as summation, the characteristics such as it is rapid to sort, reduced the volume of logic layer module and persistent layer module data, the speed of greatly raising program operation, data processing time is short thus, and efficient is high.
Certainly; the present invention also can have other various embodiments; in the situation that does not deviate from spirit of the present invention and essence thereof; those of ordinary skill in the art work as can make according to the present invention various corresponding changes and distortion, but these corresponding changes and distortion all should belong to the protection domain of the appended claim of the present invention.