CN116204598A - Java-based full-automatic object relation mapping method, system, equipment and storage medium - Google Patents

Java-based full-automatic object relation mapping method, system, equipment and storage medium Download PDF

Info

Publication number
CN116204598A
CN116204598A CN202310170138.5A CN202310170138A CN116204598A CN 116204598 A CN116204598 A CN 116204598A CN 202310170138 A CN202310170138 A CN 202310170138A CN 116204598 A CN116204598 A CN 116204598A
Authority
CN
China
Prior art keywords
mapping
database table
entity class
java
sql
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
Application number
CN202310170138.5A
Other languages
Chinese (zh)
Inventor
李维
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Si Tech Information Technology Co Ltd
Original Assignee
Beijing Si Tech Information Technology Co Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Beijing Si Tech Information Technology Co Ltd filed Critical Beijing Si Tech Information Technology Co Ltd
Priority to CN202310170138.5A priority Critical patent/CN116204598A/en
Publication of CN116204598A publication Critical patent/CN116204598A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/28Databases characterised by their database models, e.g. relational or object models
    • G06F16/284Relational databases
    • G06F16/288Entity relationship models
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/242Query formulation
    • G06F16/2433Query languages
    • G06F16/2448Query languages for particular applications; for extensibility, e.g. user defined types
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Engineering & Computer Science (AREA)
  • Databases & Information Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Data Mining & Analysis (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Mathematical Physics (AREA)
  • Computational Linguistics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention discloses a Java-based full-automatic object relation mapping method, a system, equipment and a storage medium, which comprise the steps of receiving entity class objects and mapping the relation between the entity class objects and a database table; based on the relation mapping result of the entity class object and the database table, obtaining mapped logic keywords; operating a database table; based on different operation types, splicing the logic keywords according to the standard of the sql to obtain an sql statement; based on the sql statement and the row data in the database table, a corresponding entity class object is obtained. Aiming at the scene of adding, deleting and modifying database tables, a user can complete JDBC operation by completely using object-oriented development thinking without writing sql sentences.

Description

Java-based full-automatic object relation mapping method, system, equipment and storage medium
Technical Field
The invention relates to the technical field of telecommunications, in particular to a Java-based full-automatic object relation mapping method, system, equipment and storage medium.
Background
The most popular ORM frameworks are Mybaits and Hibernate, which are well-established ORM frameworks. Mybaits uses almost pure SQL and encapsulates SQL input parameters and result types well; hibernate is a fully automatic ORM framework and has remarkable development efficiency in cooperation with notes. Mybaits is semi-automatic, meaning that any database operation you do requires handwriting an SQL, even the simplest operation. Insert, update, delete, select or paging query sentences with the same logic are all required to be written in the mapper mapping file corresponding to the entity class (table) once, so that the development efficiency is low; mybaits 'SQL is a mapping file written in "mapper.xml', basically one class needs to correspond to one mapper interface or mapper mapping file. When there are many entity classes and corresponding database tables, a large number of mapper. Xml files and mapper interfaces are laid down in the engineering system, which is especially confusing for engineering, and if the database table structure is changed, for example, a field is newly added, mybaits need to modify entity classes first, then modify mapping information of entity classes in the mapper files, and finally adjust related SQL statements. This is a very cumbersome step. Hibernate is not very good for the user experience because if writing is complicated, it is difficult for the user to judge whether the HQL is not known to write pairs; the query of Hibernate can be performed by all fields in the table, and the HQL is finally converted into SQL because of the mechanism of the HQL, so that the performance is not too high; of course, hibernate also supports the direct use of SQL, and the development modes are different, so that the need for converting thought is still needed. The default type returned after SQL execution is the type of array structure, and inconvenient corresponds to the column name of the query.
Disclosure of Invention
Aiming at the defects existing in the problems, the invention provides a Java-based full-automatic object relation mapping method, a system, equipment and a storage medium.
In order to achieve the above object, the present invention provides a Java-based full-automatic object relationship mapping method, comprising:
receiving an entity class object, and mapping the relation between the entity class object and a database table;
based on the relation mapping result of the entity class object and the database table, obtaining mapped logic keywords;
operating the database table;
based on different operation types, splicing the logic keywords according to the standard of sql to obtain sql sentences;
and obtaining a corresponding entity class object based on the sql statement and row data in the database table.
Preferably, the relationship between the entity class object and the database table is mapped through a ClassMapper class, and the relationship mapping includes mapping between entity class names and database table names, mapping between field names and column names, and mapping between primary keys and mapping between notes of other fields.
Preferably, the logical key includes a mapped column name, value and field type of the non-null field, and the classification type includes a query condition WHERE, a paging LIMIT, a ranking order_by, and an aggregate group_by.
Preferably, the operation types include adding, deleting, modifying and checking.
The invention also provides a Java-based full-automatic object relation mapping system, which comprises:
the mapping module is used for receiving the entity class object and mapping the relation between the entity class object and the database table;
the keyword module is used for acquiring mapped logic keywords based on the relation mapping result of the entity class object and the database table;
the operation module is used for operating the database table;
the splicing module is used for splicing the logic keywords according to the standard of the sql based on different operation types to obtain an sql statement;
and the acquisition module is used for acquiring the corresponding entity class object based on the sql statement and the row data in the database table.
Preferably, the relationship between the entity class object and the database table is mapped through a ClassMapper class, and the relationship mapping includes mapping between entity class names and database table names, mapping between field names and column names, and mapping between primary keys and mapping between notes of other fields.
Preferably, the logical key includes a mapped column name, value and field type of the non-null field, and the classification type includes a query condition WHERE, a paging LIMIT, a ranking order_by, and an aggregate group_by.
Preferably, the operation types include adding, deleting, modifying and checking.
The invention also provides an apparatus comprising at least one processing unit and at least one storage unit, wherein the storage unit stores a computer program which, when executed by the processing unit, causes the processing unit to perform the method described above.
The present invention also provides a storage medium storing a computer program executable by an electronic device, which when run on the electronic device causes the electronic device to perform the above-described method.
Compared with the prior art, the invention has the beneficial effects that:
aiming at the scene of adding, deleting and modifying database tables, a user can complete JDBC operation by completely using object-oriented development thinking without writing sql sentences.
Drawings
Fig. 1 is a flowchart of the Java-based fully automatic object relationship mapping method of the present invention.
Detailed Description
For the purpose of making the objects, technical solutions and advantages of the embodiments of the present invention more apparent, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is apparent that the described embodiments are some embodiments of the present invention, but not all embodiments of the present invention. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
Description of the terms
Easy Jpa: applicant defines a named lightweight, fully automatic Object Relational Mapping (ORM) Java framework;
JPA: API (Java Persistence API) of a Java persistence layer is one of Java EE standards, describes the mapping relation of an object-relation table by using notes or XML, and persists entity objects in a database in a running period;
ORM: an object relational mapping (Object Relational Mapping) for enabling conversion between data of different types of systems in an object oriented programming language;
JDBC: a Java database connection (Java Database Connectivity), an application program interface in Java language that is used to specify how client programs access a database, provides methods such as querying and updating data in the database;
DAO: a data access interface (Data Access Object) defining logic for interacting with the database, which is generally at a level intermediate the business logic and the database resources.
Referring to fig. 1, the invention provides a Java-based full-automatic object relation mapping method, which is applied to easy jpa, wherein the easy jpa is provided with easy jpa dao, namely, an add/delete/modify access port, and the method comprises the following steps:
receiving an entity class object and mapping the relation between the entity class object and a database table;
specifically, easyJpa dao is responsible for receiving the entity class object, then analyzing the entity class object, and encapsulating the entity class object into an EasyJpa object; when the object is analyzed, two things are to be completed: completing the relation mapping between the entity class object and the database table, and completing the relation mapping by a ClassMapper class; query condition construction is done by the EasyJpa class.
The relation mapping of the entity class object and the database table comprises mapping of entity class names and database table names, mapping of field names and column names, and mapping of primary keys and mapping of notes of other fields. The result of the ClassMapper mapping is cached, and the relationship mapping data needed by the subsequent logic can be directly obtained from the cache.
Based on the relation mapping result of the entity class object and the database table, obtaining mapped logic keywords;
specifically, according to the previous mapping result of the face relation, the column name, the value and the field type of the mapping of the non-null field are recorded, and then easy jpa records these logical keys and then classifies. The types of categorization include query condition WHERE, paging LIMIT, ordering ORDER_BY, aggregate GROUP_BY, and so forth. The processing of the WHERE key is complex, AND logic judgment needs to be carried out on the conditions of AND AND OR respectively: i.e. only AND, only OR, both AND OR.
Operating the database table, wherein the operation comprises adding, deleting, modifying and checking;
based on different operation types, splicing the logic keywords according to the standard of the sql to obtain an sql statement;
specifically, the keyword contents are sorted according to the standard of sql, and by taking "select" stitching as an example, the following steps are adopted: SELECT FROM WHERE GROUP BY ORDER BY LIMIT. Finally, the SQL sentence processing statement and the preprocessing parameters are assembled. Such as: preprocessing the sql statement: select a, b from user where a =? and (b=? What is? . The preprocessing parameter is "? "value in" and type of column.
Based on the sql statement and the row data in the database table, a corresponding entity class object is obtained.
Specifically, easyjpa encapsulates the native jdbc execution style into an easyjpaexebutor object. And the method is used for executing the spliced Sql statement. There are two types of execution ways to execute SQL statements: the single Connection (Connection) and the data source (datasource) are packaged differently, and the easyJpa essentially uses native jdbc logic. When performing a conditional query operation, a user may define a rowmap implementation class for encapsulating logic for results of the conditional query. Under the condition of condition query, the easyJpa also analyzes each line of data after JDBC execution returns, automatically packages the line of data objects into corresponding entity class objects according to the object mapping relation recorded by the ClassMapper, and returns the obtained entity class objects.
In this embodiment, the relationship between the entity class object and the database table is automatically mapped, if the service code complies with the command specification, easyJpa can automatically map the Java field command format aaaabbbbccc to the command format aaa_bbb_ccc of the database when performing object relationship mapping, and vice versa. Of course, easy Jpa is compatible with Java. Persistence standards api, including @ Table, @ Colum, etc., and mapping relationships can be specified by annotation. In addition to the automatic mapping between names, the mapping between attributes is also done automatically, covering all basic types, supporting also several specific types, such as the database's timestamp type can be mapped automatically to the Java's Date type. For mapping of SQL execution results, the user can return the type by himself. Easy jpa will self-identify the return type. And then returning the data analysis result to be: entity class objects or entity class object arrays or Map arrays. For example:
for such return results { student_name } ' a ', ' therapeutic_name } ' b ', the user may need to return two objects: student and Teacher. Then only one rowmap implementation class needs to be created: new AutoTypeRowMapper (student. Class, teacher. Class), easyjpa automatically identifies which entity class the column attributes in the line data correspond to, and encapsulates into new entity objects.
Assume that a User table and User entity class are provided, and the contents are as follows:
-// mysql user table
CREATE TABLE`user`(
`user_id`int(11)NOT NULLAUTO_INCREMENT,
`user_name`varchar(255)DEFAULT NULL,
`birthday`datetime DEFAULT NULL,
`height`float DEFAULT NULL,
`phone_no`bigint(20)DEFAULT NULL,
PRIMARY KEY(`user_id`)
)DEFAULT CHARSET=utf8
User class of/(java)
public class User{
@Id
private Integer userId;
private String userName;
private Date birthday;
private Long phoneNo;
private Float height;
The/(get timer method, here abbreviated)
}
First, the easy JpaDao object is initialized through a database connection, which includes the steps of:
the// initialize easy JpaDao object, typically suggesting it as a global variable
static EasyJpaDao easyJpaDao=new EasyJpaDaoImpl(localhostConnection());
Creating a jdbc connection to a database based on jdbc api of Java
static Connection localhostConnection(){
String jdbcDriver="com.mysql.jdbc.Driver";
String jdbcUrl= "jdbc: mysql:// database ip: port/database name";
string jdbcUserName = "your database username";
string jdbcPassword = "your database password";
try{
Class.forName(jdbcDriver);
Connection connection =DriverManager.getConnection(jdbcUrl,jdbcUserName,jdbcPass word);
connection. SetAutoCommit (true); automatic commit of/(update/insert/delete)
return connection;
}catch (Exception e){
throw new RuntimeException(e);
}
}
The various cases of different operation types are as follows:
1. increment operation
@Test
public void test01(){
User user=new User();
user.setUserId(1);
user.setusername ("Zhang Sanj");
user.setBirthday(new Date());
user.setHeight(1.81F);
user.setPhoneNo(1008611L);
easy JpaDao. Insert (user); method for inserting/executing user
}
The record can then be seen on the database table:
Figure BDA0004097697930000071
2. puncturing operation
@Testpublic void test01(){
EasyJpa easyJpa=new EasyJpa(User.class);
easyjpa. And (condition. Like ("userName", "tense%));
easyjpadao.deletebycondition (easyJpa); deletion of/(and/or) Condition
}
3. Modification of operation
@Testpublic void test01(){
User user=new User();
user.setUserId(1);
user.setusername ("litu");
easy Jpadao.update (user); modification of the user name with primary key=1 to litetra }
4. Check operation
@Testpublic void test01(){
User user=new User();
user.setUserId(1);
List < User > userlist=easyjpadao.query (User); record of// query id=1
}
The entrance threshold of the embodiment is extremely low, an operator can quickly develop by only grasping the common API of easy Jpa Dao, and the bottom details of the easy Jpa are hardly needed to be known; mapping information of entity class can be completely without configuration file, even name only meets Java and SQL specifications, and annotation is not needed; the bottom layer finally generates a preprocessing SQL, and the original jdbc is used as an execution framework, so that the SQL execution efficiency is higher; if a database table is changed, such as modifying some of the database table columns; then only the corresponding entity class needs to be adjusted accordingly, and other DAO operations may not need any modification.
The invention also provides a Java-based full-automatic object relation mapping system, which comprises:
the mapping module is used for receiving the entity class object and mapping the relation between the entity class object and the database table;
the keyword module is used for acquiring mapped logic keywords based on the relation mapping result of the entity class objects and the database table;
the operation module is used for operating the database table;
the splicing module is used for splicing the logic keywords according to the standard of the sql based on different operation types to obtain an sql statement;
and the acquisition module is used for acquiring the corresponding entity class object based on the sql statement and the row data in the database table.
The invention also provides an apparatus comprising at least one processing unit and at least one storage unit, wherein the storage unit stores a computer program which, when executed by the processing unit, causes the processing unit to perform the method described above.
The present invention also provides a storage medium storing a computer program executable by an electronic device, which when run on the electronic device causes the electronic device to perform the above-described method.
The above is only a preferred embodiment of the present invention, and is not intended to limit the present invention, but various modifications and variations can be made to the present invention by those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims (10)

1. A Java-based full-automatic object relation mapping method is characterized by comprising the following steps:
receiving an entity class object, and mapping the relation between the entity class object and a database table;
based on the relation mapping result of the entity class object and the database table, obtaining mapped logic keywords;
operating the database table;
based on different operation types, splicing the logic keywords according to the standard of sql to obtain sql sentences;
and obtaining a corresponding entity class object based on the sql statement and row data in the database table.
2. The method according to claim 1, wherein the relationship between the entity class object and the database table is mapped by a ClassMapper class, and the relationship mapping includes mapping between entity class name and database table name, mapping between field name and column name, and mapping between primary key and mapping between other fields with comments.
3. The fully automatic Java-based object relational mapping method according to claim 2, wherein the logical key comprises a column name, a value and a field type of the mapping of non-null fields, and the logical key is classified, and the classification type comprises a query condition WHERE, a paging LIMIT, a ranking order_by, and an aggregate group_by.
4. The Java-based fully automatic object relationship mapping method of claim 3, wherein the operation types include add, delete, modify and check.
5. A Java-based fully automatic object relationship mapping system, comprising:
the mapping module is used for receiving the entity class object and mapping the relation between the entity class object and the database table;
the keyword module is used for acquiring mapped logic keywords based on the relation mapping result of the entity class object and the database table;
the operation module is used for operating the database table;
the splicing module is used for splicing the logic keywords according to the standard of the sql based on different operation types to obtain an sql statement;
and the acquisition module is used for acquiring the corresponding entity class object based on the sql statement and the row data in the database table.
6. The fully automatic Java-based object relational mapping system according to claim 5, wherein the relational mapping between the entity class object and the database table is performed by a ClassMapper class, and the relational mapping includes mapping between entity class name and database table name, mapping between field name and column name, and mapping between primary key and mapping between notes in other fields.
7. The Java-based fully automatic object relational mapping system according to claim 6, wherein the logical key comprises a column name, a value, and a field type of the mapping of non-null fields, the logical key is classified, and the classification type comprises a query condition WHERE, a paging LIMIT, a ranking order_by, and an aggregate group_by.
8. The Java-based fully automatic object relational mapping system according to claim 7, wherein the operation types include add, delete, modify and check.
9. An apparatus comprising at least one processing unit and at least one storage unit, wherein the storage unit stores a computer program which, when executed by the processing unit, causes the processing unit to perform the method of any of claims 1-4.
10. A storage medium storing a computer program executable by an electronic device, which when run on the electronic device causes the electronic device to perform the method of any one of claims 1-4.
CN202310170138.5A 2023-02-14 2023-02-14 Java-based full-automatic object relation mapping method, system, equipment and storage medium Pending CN116204598A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310170138.5A CN116204598A (en) 2023-02-14 2023-02-14 Java-based full-automatic object relation mapping method, system, equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310170138.5A CN116204598A (en) 2023-02-14 2023-02-14 Java-based full-automatic object relation mapping method, system, equipment and storage medium

Publications (1)

Publication Number Publication Date
CN116204598A true CN116204598A (en) 2023-06-02

Family

ID=86518840

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310170138.5A Pending CN116204598A (en) 2023-02-14 2023-02-14 Java-based full-automatic object relation mapping method, system, equipment and storage medium

Country Status (1)

Country Link
CN (1) CN116204598A (en)

Similar Documents

Publication Publication Date Title
EP3446242B1 (en) Query plan generation and execution in a relational database management system with a temporal-relational database
US6279008B1 (en) Integrated graphical user interface method and apparatus for mapping between objects and databases
US5937409A (en) Integrating relational databases in an object oriented environment
US7979456B2 (en) Method of managing and providing parameterized queries
US9038022B2 (en) Universal and adaptive software development platform for data-driven applications
US7890524B2 (en) Dynamic database access via standard query language and abstraction technology
CN100557609C (en) A kind of persistent layer generation method and device
AU2012271085B2 (en) Recommending data enrichments
US20080098037A1 (en) Markup language based database upgrades
US7831614B2 (en) System and method for generating SQL using templates
CN109710220B (en) Relational database query method, relational database query device, relational database query equipment and storage medium
US7110991B2 (en) IDE integration with JDBC
CN108509199A (en) Automatically generate the method, apparatus, equipment and storage medium of Chinese annotation
US20060218174A1 (en) Method for coordinating schema and data access objects
US8316013B2 (en) Programmatic retrieval of tabular data within a cell of a query result
CN116204598A (en) Java-based full-automatic object relation mapping method, system, equipment and storage medium
CN112181996B (en) Unified data access middleware method and system for relational database
CN113761040A (en) Database and application program bidirectional mapping method, device, medium and program product
US20040249792A1 (en) Automated query file conversions upon switching database-access applications
US7831633B1 (en) Methods and apparatus for implementing a custom driver for accessing a data source
EP1000403A1 (en) An integrated graphical user interface method and apparatus for mapping between objects and databases
JP2001527241A (en) Method and apparatus for loading stored procedures in a database corresponding to object-oriented data dependencies
Pocatilu Building Database-Powered Mobile Applications
Emdi The implementation of a network CODASYL-DML interface for the multi-lingual database system.
CN101266622A (en) Trans-platform mobile storage medium data system

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