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 made up 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 display message, 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 corresponding contact the between user interface and model; The input that it accepts the user feeds back to model with input, and then realizes the calculation 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, promptly submits static data to logical layer; And logical layer is handled these data, and the result after will handling is presented at presentation layer.
Fig. 1 is the incident 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 needing the requirement of deal with data to issue model 103, and model 103 is issued view 101 with data processed result after revising and finishing.It can respond a plurality of views by a plurality of controllers are set, so that 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 using one of the web application needs and the distinct design of MVC of HTTP request pattern thus, 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 it is different with Fig. 1, it not only plays a part bridge, also bear the effect of data processing, it can come and as required data be handled according to needs reading of data from persistent layer module 203 of presentation layer module 201, passes to presentation layer module 201 then; Perhaps revise data, and upgrade persistent layer module 203 according to the data demand of presentation layer module 201.Persistent layer module 203 also can be called model, only is responsible for the most basic database manipulation, promptly retrieves (inquiry), inserts, deletes, revises.
Fig. 2 is the incident 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 data relevant with database and the data that have nothing to do with 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 logic layer module 202 responds the data processing that realizes that Web uses to presentation layer module 201 after with described data processing.
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 under 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 big set, and logic layer module 202 is calculated according to this set then.Be implemented as follows:
1) the persistent layer module 203:
public?List?find(Date?start,Date?end){
private?Entity?Manager?manager;
return?manager.createQuery(“selecta.Type,a.Quantity?from?SALE?a?where
a.time>=:pa1and?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 their quantity one by one and calculates ratio in quantity according to the difference of stage property type.
In model system 200, the access of 203 responsible data of persistent layer module, logic layer module 203 processing said data.When described quanlity is very big, the sales data of one month the stage property that logic layer module 203 need be handled is 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 to have high performance processor and waste staff's quality time thus.
Model system 200 data processing methods two: by reading one by one with repeatedly reading of 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;
returnmanager.createQuery(“select?a.Type,a.Quantity?from?SALE?a?where
a.time>=:pa1and?a.time<=:pa2and?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 30 times again thus, can narrow down to 1 to the size of LIST in theory, also just become one by one to fetch data.Can solve the waste of storage space problem that is produced in the data processing method one thus.But only be only applicable to the few and situation rapidly 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 the 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.With each time for reading be 0.1 second be benchmark, want 1000 seconds for 10,000, and in fact this time might be longer, especially under the situation of distributed data base.
In summary, the realization technical scheme of existing MVC model system and MVC model system data processing obviously exists inconvenience and defective, so be necessary to be improved on reality is used.
Summary of the invention
At above-mentioned defective, the present invention's first purpose is to provide a kind of MVC model system data processing method, and this method data occupancy space is little, and data processing time is short, the efficient height.
The present invention's second purpose is to provide a kind of MVC model system, and this MVC model system data occupancy space is little, and data processing time is short, the efficient height.
In order to realize above-mentioned 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 of persistent layer resume module with database.
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 that have nothing to do with 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 handle the described data that have nothing to do with 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 handled 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 of described session Bean by EJB3.0 injects data to simple traditional JAVA object class, thereby realizes the processing of persistent layer module to described data relevant with database.
According to described method, described session Bean realizes the processing of persistent layer module to the relevant data of database by calling storing process.
According to described method, further comprise after the described step C:
The data relevant with database after D, persistent layer module will be handled return to logic layer module;
The relevant data with database with after irrelevant data of database and the persistent layer resume module after E, described logic layer module handle self return to the data processing request of presentation layer module with the transmission of response presentation layer module
In order to realize above-mentioned 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 used to handle the described data relevant with database.
According to described MVC model system, described logic layer module receives data distribution it is entrained after the described data processing request and is data relevant with database and the data that have nothing to do with database, 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 is provided with 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 handled the described data relevant with database by described session Bean with simple traditional JAVA object class.
According to described MVC model system, the QL of described session Bean by EJB3.0 injects data to simple traditional JAVA object class, thereby realizes the processing of persistent layer module to described data relevant with database.
The statistics of MVC model system of the present invention in according to the actual requirements, with the data separating of the required processing of logic layer module is data relevant with database and the data that have nothing to do with database, and will the data relevant transfer to the persistent layer module and handle with database, make full use of the peculiar function of database thus as summation, characteristics such as it is rapid to sort have reduced the volume of logic layer module and persistent layer module data, improve the speed of program run greatly, data processing time is short thus, the efficient height.
Description of drawings
Fig. 1 is the incident flow graph of the MVC model system that provides of prior art one;
Fig. 2 is the incident flow graph of the MVC model system that provides of prior art two;
Fig. 3 is the incident 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 is provided with module map;
Fig. 5 is a 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,, the present invention is further elaborated below in conjunction with drawings and Examples.Should be appreciated that specific embodiment described herein only in order to explanation the present invention, and be not used in qualification the present invention.
Basic thought of the present invention is, with the data separating of the required processing of logic layer module is data relevant with database and the data that have nothing to do with database, thereby and will the data relevant transfer to the persistent layer module and handle and realize that the data occupancy space is little with database, 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.The data that this data processing request is carried the data relevant with database and had nothing to do with database should comprise service logic with the data that database has nothing to do.
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, be about to the data that data are divided into the data relevant with database and have nothing to do with database according to the data type different pairs according to shunting, and will the data relevant be sent to persistent layer module 303 and handle with database, and oneself handle according to demand with the irrelevant data of database be service logic etc.
Persistent layer module 303 is used for the described data relevant with database that processing logic layer module 302 sends.
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 Bean3.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 we can say and concentrated ibatis and the hibernate realization advantage in 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 handling a part of data, thereby reduced the data transmission of magnanimity, save the time loss that memory headroom and transmission are brought, finally can improve the effect of overall operation speed.
Fig. 4 is that of persistent layer module 303 preferably is provided with module map.Be provided with database interface (doubleRemote.java), the Bean (doubleBean.java) that realizes interface and the class (double.java) of Database field correspondence by EJB3.0 in this persistent layer module 303.
Database interface (doubleRemote.java) is the teleaction service interface, mainly be that definition logic layer module 302 method that needs and the interface that data processing is provided 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 the SQL statement of database correspondence, almost corresponding with the standard SQL function 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, logic layer module goes database root to get this user's password according to user name then,, 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 login successfully, otherwise then fails.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 land 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 inject the processing that data procedures realizes 303 pairs of data relevant with database of persistent layer module with the QL that replaces EJB by calling storing process.
Call storing process and can make the data-handling efficiency height, and system 300 is 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.
The class of Database field correspondence (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 their attribute by data element.Data element is the notion 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 create table in the database by dual mode: a kind of is to create class earlier, and stipulates the attribute of each field in class with the form of data element, then program run will be seen in database that this has shown.Another kind of mode is to create earlier table in database, and then it is corresponding with it to create the POJO class.
When persistent layer module 303 handle will the data relevant with database after, pass result back logic layer module 302, logic layer module 302 is further handled the described data of passing back, and data and the persistent layer module 303 data back presentation layer modules 301 relevant with database that handle with database has nothing to do then himself handled respond with the data processing request that presentation layer module 301 is sent.
Fig. 5 is a 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 handled 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.This method specifically comprises as follows:
Step S601 is provided with 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 that have nothing to do with 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 handles the described data that have nothing to do with database.
Step S606, persistent layer module 303 is handled according to session Bean and POJO object class after receiving the data relevant with database.
In the present embodiment, the QL of the session Bean of persistent layer module 303 by EJB3.0 injects data to simple traditional JAVA object class, thereby realizes the processing of persistent layer module to 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;
returnmanager.createQuery(“select?a.Type,sum(a.Quantity)from?SALE?a
where?a.time>=:pa1and?a.time<=:pa2group?bya.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 handled return to logic layer module 302.
Step S608, logic layer module 302 is further handled the described data relevant with database.
Step S609, the relevant data with database with after irrelevant data of database and the persistent layer resume module after logic layer module 302 is handled self return to the data processing request of presentation layer module 301 with 301 transmissions of response presentation layer module.
Table 2 is to adopt 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 respectively, 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, the data processing method that first embodiment of the invention provided can be saved data processing time greatly as can be seen, improves data-handling efficiency.
The MVC model system data processing method and first embodiment that second embodiment of the invention provides are similar, its flow process can be referring to Fig. 6, different with first embodiment is: among the step S607, the session Bean of second embodiment provided by the invention realizes the processing of the data that 303 pairs of databases of persistent layer module are relevant by calling storing process.Specific as follows:
1. the implementation method of persistent layer module 303:
public?Object?find(int?kind,Date?start,Date?end){
private?Enti?tyManager?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_countint(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, as long as the name of calling storing process among the session Bean just can realize the statistics of data, and use storing process, helping 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, with the data separating of the required processing of logic layer module is data relevant with database and the data that have nothing to do with database, and will the data relevant transfer to the persistent layer module and handle with database, make full use of the peculiar function of database thus as summation, characteristics such as it is rapid to sort, reduced the volume of logic layer module and persistent layer module data, improve the speed of program run greatly, data processing time is short thus, the efficient height.
Certainly; the present invention also can have other various embodiments; under 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 various corresponding changes and distortion according to the present invention, but these corresponding changes and distortion all should belong to the protection domain of the appended claim of the present invention.