CN112783915A - Data transmission object mapping method and unit based on SpringMVC + Mybatis framework - Google Patents
Data transmission object mapping method and unit based on SpringMVC + Mybatis framework Download PDFInfo
- Publication number
- CN112783915A CN112783915A CN201911065319.1A CN201911065319A CN112783915A CN 112783915 A CN112783915 A CN 112783915A CN 201911065319 A CN201911065319 A CN 201911065319A CN 112783915 A CN112783915 A CN 112783915A
- Authority
- CN
- China
- Prior art keywords
- data
- data transmission
- transmission object
- class
- configuration file
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 230000005540 biological transmission Effects 0.000 title claims abstract description 67
- 238000000034 method Methods 0.000 title claims abstract description 54
- 238000013507 mapping Methods 0.000 title claims abstract description 19
- 238000012546 transfer Methods 0.000 claims description 8
- 238000010586 diagram Methods 0.000 description 5
- 230000002085 persistent effect Effects 0.000 description 2
- 238000012545 processing Methods 0.000 description 2
- 238000013461 design Methods 0.000 description 1
- 238000011161 development Methods 0.000 description 1
- 238000005538 encapsulation Methods 0.000 description 1
- 230000006870 function Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 238000004806 packaging method and process Methods 0.000 description 1
- 230000002688 persistence Effects 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/20—Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
- G06F16/24—Querying
- G06F16/242—Query formulation
- G06F16/2433—Query languages
Abstract
The embodiment of the scheme provides a data transmission object mapping method and unit based on a SpringMVC + Mybatis framework. According to the technical scheme, when the query condition of the DTO of the output transmission object only relates to the field of one database table, only the data meeting the query condition is needed to be directly combined, and inner join operation is not needed to be carried out on all tables, so that the efficiency is improved; when the query condition for the data transmission object DTO relates to a plurality of data tables, the data transmission object classes corresponding to the plurality of data tables can be combined by using SQL statements in the xml file in the data access layer DAO, and the xml configuration file is directly called during query, so that the efficiency is improved.
Description
Technical Field
The embodiment of the invention relates to the technical field of data transmission, in particular to a data transmission object mapping method based on a SpringMVC + Mybatis framework.
Background
The springMVC is a full-function MVC module which is arranged in the Spring of the open source framework and used for constructing a WEB application program, provides a dispatcher servlet function front-end controller for dispatching requests, provides flexible configuration handler mapping, view parsing, language environment and theme parsing, supports file uploading, and has strong flexibility, non-invasiveness and configurability.
Mybatis is an excellent persistence tier framework that supports normal SQL queries, stored procedures, and advanced mapping, which removes almost all manual setup of JDBC code and parameters and encapsulation of results set retrieval. Mybatis can use simple XML or annotations for configuration and raw mapping to map interfaces and Java's POJO (plan Old Java objects) to records in the database.
Mybatis as an ORM (Object-relationship Mapping) framework tool can implement the Mapping relationship between the persistent class and the data table and convert the operations of saving, modifying, deleting and the like on the persistent Object into the operations on the data. However, when processing and submitting data, SpringMVC sometimes needs to be not simply a class mapped by a certain database table, but a class formed by combining certain fields in certain database tables, and these classes are called dto (data Transfer object).
Disclosure of Invention
In view of this, the present invention provides a data transmission object mapping method based on a SpringMVC + Mybatis framework.
In order to solve the above problem, an embodiment of the present invention provides a data transmission object mapping method based on a SpringMVC + Mybatis framework, where the method includes:
judging whether the data to be searched under the current query condition only relates to a single database table;
if not, combining the data transmission object DTO classes corresponding to the plurality of database tables in the xml configuration file; and the xml configuration file is called when the query condition is executed, and the data transmission object DTO is obtained.
In one embodiment, if yes, querying data of a corresponding class from the related single database table according to a query condition, and combining the data of the class with data responded by other classes to generate a data transmission object.
In one embodiment, the method further comprises the steps of:
the data transfer response is invoked when a query condition is executed.
In one embodiment, the xml configuration file is generated according to the generation rules of the Mybatis automatic generation tool.
In an embodiment, in the step of combining the data transmission object classes corresponding to the plurality of database tables in the xml configuration file, the data transmission object classes corresponding to the plurality of database tables are combined through an SQL statement.
In an embodiment, the step of querying the data of the corresponding class from the related single database table according to the query condition, and combining the data of the class with the data responded by other classes to generate the data transmission object includes:
defining a user data transmission object class, and declaring a required field and a padding method and an obtaining getter method for each field pair in the class;
and filling data corresponding to the fields in the user data transmission object class into the data transmission object by using a filling method.
In one embodiment, the previous step of padding data corresponding to fields in the class of user data transfer objects into the data transfer objects using a padding method comprises:
and inquiring data corresponding to fields in the user data transmission object class based on a method for calling a Service layer by a Controller layer in a SpringMVC framework.
In one embodiment, the step of combining the data transmission object classes corresponding to the plurality of database tables in the xml configuration file comprises:
defining a user data transmission object class, and declaring a required field and a padding method and an obtaining getter method for each field pair in the class;
automatically generating an xml configuration file in a newly-built DAO class file in a DAO file format at a data access layer DAO of a SpringMVC frame according to a Mybatis frame, and declaring a query method corresponding to a field in a user data transmission object class in the DAO class file;
and combining the data transmission object classes corresponding to the plurality of database tables in the xml file by using SQL sentences.
In one embodiment, the step of combining the data transmission object classes corresponding to the plurality of database tables in the xml configuration file further includes:
defining a query method at a Service layer of a SpringMVC framework, and calling an xml configuration file of a DAO layer for query.
The embodiment of the invention further provides a data transmission object mapping unit based on a SpringMVC + Mybatis framework, the unit is arranged in the SpringMVC + Mybatis framework, and the unit comprises:
the judging module is used for judging whether the data required to be searched by the current query condition only relates to a single database table;
if so, inquiring corresponding data from the related single database table according to the inquiry condition, and combining the data with other responsive data to generate a data transmission object;
if not, combining the data transmission object classes corresponding to the plurality of database tables in the xml configuration file;
and the calling module is used for inquiring according to the inquiry condition, calling the data transmission object or the xml configuration file and finishing the mapping of the data transmission object.
The technical scheme of the embodiment of the invention has the following advantages:
by adopting the technical scheme, when the query condition of the DTO of the output transmission object only relates to the field of one database table, only the data meeting the query condition needs to be directly subjected to field combination, and inner join operation is not needed to be performed on all tables, so that the efficiency is improved; when the query condition for the data transmission object DTO relates to a plurality of data tables, the data transmission object classes corresponding to the plurality of data tables can be combined by using SQL statements in the xml file in the data access layer DAO, and the xml configuration file is directly called during query, so that the efficiency is improved.
Drawings
In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings needed to be used in the description of the embodiments or the prior art will be briefly introduced below, it is obvious that the drawings in the following description are only embodiments of the present application, and for those skilled in the art, other drawings can be obtained according to the provided drawings without creative efforts.
FIG. 1 is a schematic diagram of the SpringMVC + Mybatis framework with a data transmission subtended DTO added;
FIG. 2 is a schematic diagram of three data tables according to an embodiment;
fig. 3 is a schematic diagram of a data transmission object mapping method based on a SpringMVC + Mybatis framework according to this scheme.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
As an alternative example of the present disclosure, FIG. 1 illustrates a block diagram of a computer system architecture; it should be noted that the block diagram is shown to facilitate understanding of the disclosure of the embodiments of the present invention, which are not limited to the architecture shown in fig. 1.
The method can be executed on a computer system provided with a SpringMVC + Mybatis program framework. As shown in fig. 1, in the present solution, a data transmission object DTO is introduced into a framework of a conventional SpringMVC + Mybatis program, and meanwhile, a layer of package combination DTO needs to be added. The DTO of the data transmission object is mapped with the corresponding field in the database table, so that the execution efficiency is improved, and the development difficulty is reduced.
In this scheme, the query condition for the data transmission object is not only a class mapped by a certain database table, but may also relate to a class formed by combining some fields in a plurality of database tables. The specific situation is as follows:
when the query condition of the data transmission object DTO only relates to a field of a database table mapped by the query condition, a Controller layer (or a Service layer) in SpringMVC queries data of a corresponding class from a certain related database table according to the query condition by calling a Service layer (or a DAO layer), and then combines the data of the class with data responded by other classes by using a program statement, thereby obtaining the data transmission object DTO. The program statements can be written in a custom mode.
When the query condition of the DTO relates to a field which is not only one database table, an xml configuration file corresponding to the DTO class of the data transmission object is newly built in a DAO layer of the data access object according to the automatic generation tool generation rule of Mybatis, and SQL statements are written in the configuration file to combine a plurality of database tables to generate the corresponding DTO class of the data transmission object. When the method is used, the Service layer in SpringMVC directly calls the method defined in the xml file newly built in the DAO layer of the data access object, and the DTO of the data transmission object can be directly obtained.
If the query condition of the DTO of the data transmission object only relates to a field of a database table mapped by the DTO, the method is simple and convenient to realize by adopting a first mode, is very suitable for quickly developing software, and the content of the DTO of the data transmission object, which relates to the Service, is placed in a Controller layer (or a Service layer) to be assembled, so that the initial design purpose of a SpringMVC frame is met, and the idea of layered calling of software engineering is also met. In addition, when the query condition of the data transmission object DTO only relates to the field of one database table, the method only needs to carry out field combination on the data meeting the query condition, and does not need to carry out internal connection join operation on all the tables, so the efficiency is higher. However, when the query condition of the DTO involves multiple tables, the processing and packaging at the Controller layer (or Service layer) is complex and inconvenient to develop, and the method of combining after querying each database table according to different query conditions is also inefficient, so the second method using the above scheme may obtain high efficiency.
It can be seen from the above description that the number of the data tables directly affects the strategy for acquiring the DTO of the data transmission object, and therefore, as shown in fig. 3, according to the present solution, whether the data to be searched according to the current query condition only relates to a single data table is determined, and the strategy for acquiring the DTO of the data transmission object is correspondingly matched according to the determination result, so that the requirements of different queries are better adapted, and the execution efficiency is improved.
The present solution is further illustrated by the following examples;
this example takes three database tables as an example. The three tables are respectively a user information table, a role information table and a permission information table, and the field contents and the association relations of the three tables are shown in fig. 2.
A user DTO class is assembled, which includes a user name field of the user information table, a role name field of the role information table, and an authority name field of the authority information table. Two cases will now be described:
when the query condition is judged to be only the field of the 'user name' (the query condition only relates to the field of one database table mapped by the DTO), the following strategy is adopted:
a1, defining a UserDTO class, and declaring required fields in the class, namely userNm, roleNm and dutyNm, and a setter and a getter method corresponding to each field;
a2, calling a method of a Service layer at a Controller layer to inquire user information table data meeting the condition of a 'userNm' field;
a3, filling the 'userNm' field of the data into the DTO by a setter method of the UserDTO, and obtaining role information table data by secondary query according to the 'role ID' field;
a4, filling a 'roleMn' field of the role information table data into a user DTO (data transfer service) through a setter method of the user DTO, and inquiring for three times according to a 'dutyiD' field to obtain authority information table data;
a5, filling the 'dutyNm' field of the authority information table data into the DTO, thus completing the combined mapping of UserDTO.
When the query condition is judged to contain fields of 'user name' and 'role name' (the query condition relates to fields of a plurality of database tables mapped by the DTO), the following strategies are adopted:
b1, defining a UserDTO class, and declaring required fields, namely userNm, roleNm and dutyNm, and a setter and a getter method corresponding to each field;
b2, creating a DAO file xml file in the DAO layer according to the format of the DAO file automatically generated by Mybatis, declaring an inquiry method corresponding to each field of usertdo in the DAO file, and combining usertdo classes-select userNm, roleNm, dutyNm from userttable inner joint in rolletable on userttable in the xml file;
b3, finally defining a method queryUserByCondition (userNm, roleNm) at the Service layer, wherein the input parameters are query conditions, and the method calls the DAO layer to realize specific query. Thus, the combined mapping of UserDTO is completed.
For the above detailed logic implementation of the logic circuit unit, reference may be made to the corresponding descriptions of the method portions, and all the related portions in the description may be referred to correspondingly, which is not repeated herein
Although the embodiments of the present invention have been disclosed, the present invention is not limited thereto. Various changes and modifications may be effected therein by one skilled in the art without departing from the spirit and scope of the invention as defined in the appended claims.
Claims (10)
1. A data transmission object mapping method based on a SpringMVC + Mybatis framework is characterized by comprising the following steps:
judging whether the data to be searched under the current query condition only relates to a single database table;
if not, combining the data transmission object DTO classes corresponding to the plurality of database tables in the xml configuration file; and the xml configuration file is called when the query condition is executed, and the data transmission object DTO is obtained.
2. The method of claim 1, wherein if yes, the data of the corresponding class is queried from the single database table concerned according to the query condition, and the data of the class is combined with the data responded by other classes to generate the data transmission object.
3. The method of claim 2, wherein the method further comprises the steps of:
the data transfer response is invoked when a query condition is executed.
4. The method of claim 1, wherein the xml configuration file is generated according to generation rules of a Mybatis auto-generation tool.
5. The method according to claim 1, wherein in the step of combining the data transmission object classes corresponding to the plurality of database tables in the xml configuration file, the data transmission object classes corresponding to the plurality of database tables are combined by SQL statements.
6. The method according to any one of claims 1 to 5, wherein the step of querying the data of the corresponding class from the single database table concerned according to the query condition, and combining the data of the class with the data responded by other classes to generate the data transmission object comprises:
defining a user data transmission object class, and declaring a required field and a padding method and an obtaining getter method for each field pair in the class;
and filling data corresponding to the fields in the user data transmission object class into the data transmission object by using a filling method.
7. The method of claim 6, wherein the previous step of padding data corresponding to fields in the class of user data transfer objects with a padding method comprises:
and inquiring data corresponding to fields in the user data transmission object class based on a method for calling a Service layer by a Controller layer in a SpringMVC framework.
8. The method of claims 1 to 5, wherein the step of combining the DTO classes of the data transfer objects corresponding to the plurality of database tables in the xml configuration file comprises:
defining a user data transmission object class, and declaring a required field and a padding method and an obtaining getter method for each field pair in the class;
automatically generating an xml configuration file in a newly-built DAO class file in a DAO file format at a data access layer DAO of a SpringMVC frame according to a Mybatis frame, and declaring a query method corresponding to a field in a user data transmission object class in the DAO class file;
and combining the data transmission object classes corresponding to the plurality of database tables in the xml file by using SQL sentences.
9. The method of claim 8, wherein the step of combining the data transport object classes corresponding to the plurality of database tables in the xml configuration file further comprises:
defining a query method at a Service layer of a SpringMVC framework, and calling an xml configuration file of a DAO layer for query.
10. A data transmission object mapping unit based on a SpringMVC + Mybatis framework, the unit is arranged in the SpringMVC + Mybatis framework, and the unit comprises:
the judging module is used for judging whether the data required to be searched by the current query condition only relates to a single database table;
if so, inquiring corresponding data from the related single database table according to the inquiry condition, and combining the data with other responsive data to generate a data transmission object;
if not, combining the data transmission object classes corresponding to the plurality of database tables in the xml configuration file;
and the calling module is used for inquiring according to the inquiry condition, calling the data transmission object or the xml configuration file and finishing the mapping of the data transmission object.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911065319.1A CN112783915A (en) | 2019-11-04 | 2019-11-04 | Data transmission object mapping method and unit based on SpringMVC + Mybatis framework |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911065319.1A CN112783915A (en) | 2019-11-04 | 2019-11-04 | Data transmission object mapping method and unit based on SpringMVC + Mybatis framework |
Publications (1)
Publication Number | Publication Date |
---|---|
CN112783915A true CN112783915A (en) | 2021-05-11 |
Family
ID=75748618
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201911065319.1A Pending CN112783915A (en) | 2019-11-04 | 2019-11-04 | Data transmission object mapping method and unit based on SpringMVC + Mybatis framework |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN112783915A (en) |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN114547067A (en) * | 2022-01-20 | 2022-05-27 | 北京元年科技股份有限公司 | SQL query statement generation method, device and equipment based on general query structure |
CN114756554A (en) * | 2022-06-13 | 2022-07-15 | 中建电子商务有限责任公司 | Data query processing method based on MyBatis framework |
Citations (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN110134427A (en) * | 2018-02-08 | 2019-08-16 | 北京京东尚科信息技术有限公司 | A kind of method and apparatus generating code file |
-
2019
- 2019-11-04 CN CN201911065319.1A patent/CN112783915A/en active Pending
Patent Citations (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN110134427A (en) * | 2018-02-08 | 2019-08-16 | 北京京东尚科信息技术有限公司 | A kind of method and apparatus generating code file |
Non-Patent Citations (5)
Title |
---|
嘟哒: "MyBatis 多表联合查询及优化", pages 1 - 17, Retrieved from the Internet <URL:https://blog.csdn.net/u012661010/article/details/76408566?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170115972416800227490859%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=170115972416800227490859&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-7-76408566-null-null.nonecase&utm_term=springMVC%20mybatis%20%E5%A4%9A%E8%A1%A8%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2&spm=1018.2226.3001.4450> * |
夏晨SEASON: "MyBatis单表查询操作——XML", Retrieved from the Internet <URL:https://blog.csdn.net/qq_43296599/article/details/84308803> * |
张钟浩;魏金海;彭章友;: "基于SSM的车辆保险系统设计和实现", 工业控制计算机, no. 05 * |
时月梅;: "基于Spring MVC、MyBatis实现数据分页显示处理", 信息与电脑(理论版), no. 19 * |
欧阳宏基;葛萌;陈伟;: "基于JDBC的数据持久化层性能优化研究", 网络新媒体技术, no. 05 * |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN114547067A (en) * | 2022-01-20 | 2022-05-27 | 北京元年科技股份有限公司 | SQL query statement generation method, device and equipment based on general query structure |
CN114756554A (en) * | 2022-06-13 | 2022-07-15 | 中建电子商务有限责任公司 | Data query processing method based on MyBatis framework |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US7451136B2 (en) | System and method for searching multiple disparate search engines | |
CN104216912A (en) | Method and device for achieving non-intrusive service form workflow | |
CN105718593A (en) | Database query optimization method and system | |
CN111125440B (en) | Monad-based persistent layer composite condition query method and storage medium | |
CN112783915A (en) | Data transmission object mapping method and unit based on SpringMVC + Mybatis framework | |
CN110674205B (en) | Single table query method, device, terminal and readable storage medium | |
CN111897824A (en) | Data operation method, device, equipment and storage medium | |
JPH0667867A (en) | Data base accessing system for application program | |
US20140095550A1 (en) | Converting source objects to target objects | |
CN116166700A (en) | Query method, device, system, electronic equipment and storage medium | |
US20060074934A1 (en) | Utilization of display profiles with abstract queries | |
US7325003B2 (en) | Method and system for mapping datasources in a metadata model | |
CN112860802A (en) | Database operation statement processing method and device and electronic equipment | |
CN101241503B (en) | Database personalized method | |
WO2024001029A1 (en) | Method and apparatus for maintaining blockchain data, electronic device, and storage medium | |
CN112541001A (en) | Data query method, device, storage medium and equipment | |
CN115994146A (en) | Hybrid data storage engine system, data storage method and access method | |
CN116340337A (en) | Database-independent SQL sentence generation method | |
US6725213B1 (en) | Method and mechanism for providing external procedures to a database system | |
CN111221860A (en) | Mixed query optimization method and device based on big data | |
CN115114325A (en) | Data query method and device, electronic equipment and storage medium | |
US20220374423A1 (en) | Running method and device for stored procedure, database system and storage medium | |
CN112540995A (en) | Data updating method, electronic device and storage medium | |
CN107729410A (en) | A kind of database engine suitable for power equipment big data | |
CN105117571A (en) | Design and simulation method for ramjet engine and simulation platform data bus |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |