技术领域technical field
本发明涉及一种数据库访问方法,尤其是涉及一种基于属性标签的数据库访问方法。The invention relates to a database access method, in particular to a database access method based on attribute labels.
背景技术Background technique
数据库是如今很多管理程序都会用到的数据管理程序,不论是BS还是CS架构应用程序,都可以分为数据处理和数据访问两部分,数据库操作是数据访问的主要部分。而如何高效简便地在应用程序中进行数据库的访问是很多开发人员都在探索的内容,比较常用的两种方式是对象映射的方式和通过反射API获取业务实体类相关信息,然后拼接SQL语句的方式。但两种方式都不够简洁,对象映射方式需要通过xml配置文件来映射代码实体类和数据库表之间的关系,在进行持久化操作时候需要查找对应关系来确定要在数据库中操作的对象。而通过反射API获取业务实体类信息,然后拼接SQL语句的方式需要通过预先配置好的文件来映射对象类和该对象的实际数据访问类,然后才能执行具体的数据库操作。Database is a data management program used by many management programs today. Whether it is a BS or CS architecture application, it can be divided into two parts: data processing and data access. Database operation is the main part of data access. How to efficiently and easily access the database in the application program is what many developers are exploring. The two commonly used methods are the object mapping method and the method of obtaining business entity class related information through the reflection API, and then splicing SQL statements. Way. But the two methods are not concise enough. The object mapping method needs to map the relationship between the code entity class and the database table through the xml configuration file. When performing the persistence operation, it is necessary to find the corresponding relationship to determine the object to be operated in the database. However, the method of obtaining business entity class information through the reflection API, and then splicing SQL statements needs to map the object class and the actual data access class of the object through a pre-configured file, and then perform specific database operations.
发明内容Contents of the invention
本发明的目的就是为了克服上述现有技术存在的缺陷而提供的一种基于属性标签的数据库访问方法,旨在通过业务类的属性标签实现与数据库的绑定,从而实现数据持久化的自动处理。The purpose of the present invention is to provide a database access method based on attribute tags in order to overcome the defects of the above-mentioned prior art, aiming to realize the binding with the database through the attribute tags of the business class, so as to realize the automatic processing of data persistence .
本发明的目的可以通过以下技术方案来实现:The purpose of the present invention can be achieved through the following technical solutions:
一种基于属性标签的数据库访问方法,其特征在于,该方法包括以下步骤:A database access method based on attribute tags, characterized in that the method comprises the following steps:
(1)创建自定义属性标签,包括自定义类的标签和自定义字段的标签,所述的自定义类的标签包括数据库表或视图名称,所述的自定义字段的标签包括数据库中字段名称和字段类型;(1) Create a custom attribute label, including the label of the custom class and the label of the custom field, the label of the custom class includes the name of the database table or view, and the label of the custom field includes the name of the field in the database and field type;
(2)构建所有业务类,并对每个业务类分别添加与数据库内容一一对应的自定义类的标签和自定义字段的标签;(2) Construct all business classes, and add the label of the custom class and the label of the custom field corresponding to the database content one by one to each business class;
(3)构建每个业务类的ADO类,并进行持久化操作;(3) Construct the ADO class of each business class and perform persistence operation;
(4)通过反射机制获取业务类对应的表或视图名称和字段名称;(4) Obtain the table or view name and field name corresponding to the business class through the reflection mechanism;
(5)根据获取到的表或视图名称和字段名称构建CRUD操作的SQL语句;(5) Construct the SQL statement of the CRUD operation according to the obtained table or view name and field name;
(6)根据具体业务类的数据信息构建SQL参数对象,调用ADO执行函数,若有异常则将异常信息返回给系统处理并关闭数据库连接,否则返回结果并关闭数据库连接。(6) Construct the SQL parameter object according to the data information of the specific business class, call the ADO execution function, if there is an exception, return the exception information to the system for processing and close the database connection, otherwise return the result and close the database connection.
所述的标签的字段类型和数据库中具体字段类型一致。The field type of the label is consistent with the specific field type in the database.
所述的步骤(3)具体包括以下子步骤:Described step (3) specifically comprises the following substeps:
(301)构建ADO类的操作模板SmrAdoTmplate<T>;(301) build the operation template SmrAdoTmplate<T> of ADO class;
(302)通过继承SmrAdoTmplate<T>方式来构建每个业务类的ADO类;(302) build the ADO class of each business class by inheriting the SmrAdoTmplate<T> mode;
(302)在系统中调用业务类对应的ADO类执行持久化操作。(302) Invoke the ADO class corresponding to the business class in the system to execute the persistence operation.
所述的步骤(5)中构建CRUD操作的SQL语句具体方式为:调用SQL语句拼接SqlServerConvertor类构建CRUD操作的SQL语句。The concrete way of constructing the SQL statement of CRUD operation in the described step (5) is: call SQL statement splicing SqlServerConvertor class and construct the SQL statement of CRUD operation.
所述的调用SQL语句拼接SqlServerConvertor类构建CRUD操作的SQL语句,若为插入操作,其具体拼接方式为:采用insertinto{0}({1})values({2})字符串进行操作,所述的标签的表或视图名称格式化到{0}位置,所述的标签的字段名称以逗号分隔拼接成字符串后格式化到{1}位置,根据具体的数据库类型在字段名称前添加不同前缀,拼接SQL语句参数内容格式化到{2}位置。The SQL statement that calls the SQL statement splicing SqlServerConvertor class to construct the CRUD operation, if it is an insert operation, the specific splicing method is: use insertinto{0}({1})values({2}) string to operate, the The table or view name of the tag is formatted at {0} position, the field name of the tag is separated by commas and concatenated into a string and then formatted at {1} position, and different prefixes are added before the field name according to the specific database type , splicing the SQL statement parameter content and formatting it to {2} position.
与现有技术相比,本发明只需对业务类添加对应的标签内容,即可实现业务对数据的持久化存储,使得开发工作主要集中在业务逻辑的处理,从而提高开发效率;通过类本身的属性标签来映射业务实体类和数据库实体的内容,不需要额外的映射文件或映射关系表来维护映射关系,从而节省IO和内存资源,提高执行效率。Compared with the prior art, the present invention only needs to add the corresponding label content to the business class to realize the persistent storage of business data, so that the development work is mainly concentrated on the processing of business logic, thereby improving the development efficiency; through the class itself attribute tags to map the content of business entity classes and database entities, and no additional mapping files or mapping relationship tables are required to maintain the mapping relationship, thereby saving IO and memory resources and improving execution efficiency.
附图说明Description of drawings
图1为本发明数据库访问方法总体结构示意图;Fig. 1 is a schematic diagram of the overall structure of the database access method of the present invention;
图2为本发明自定义属性标签与数据库的对应关系结构示意图;Fig. 2 is a schematic diagram of the corresponding relationship structure between the self-defined attribute label and the database of the present invention;
图3为本发明数据库访问方法流程图。Fig. 3 is a flow chart of the database access method of the present invention.
1为业务实体,2为数据库,3为数据持续化层,4为自定义属性标签。1 is the business entity, 2 is the database, 3 is the data persistence layer, and 4 is the custom attribute label.
具体实施方式Detailed ways
下面结合附图和具体实施例对本发明进行详细说明。The present invention will be described in detail below in conjunction with the accompanying drawings and specific embodiments.
实施例Example
如图1,本方法主要通过自定义属性标签来实现业务实体1和数据库2实体之间的映射,通过数据持久化层3来执行数据库2访问操作。As shown in Figure 1, this method mainly realizes the mapping between the business entity 1 and the database 2 entity through the custom attribute label, and executes the database 2 access operation through the data persistence layer 3.
图2中自定义属性标签4的类名映射到数据库2表或视图名称,类属性映射到字段名称,因此自定义属性标签4包括自定义类的标签和自定义字段的标签,其中自定义类的标签仅包含对应的表或视图名称,自定义字段的标签包括数据库中字段名称和字段类型,字段类型应和数据库中具体字段类型保持一致。The class name of the custom attribute label 4 in Figure 2 is mapped to the table or view name of the database 2, and the class attribute is mapped to the field name, so the custom attribute label 4 includes the label of the custom class and the label of the custom field, where the custom class The label of the field contains only the name of the corresponding table or view, and the label of the custom field includes the field name and field type in the database. The field type should be consistent with the specific field type in the database.
图3为本发明数据库访问方法流程图,以下对图中的各步骤进行详细描述:Fig. 3 is a flowchart of the database access method of the present invention, and each step in the figure is described in detail below:
步骤301中,创建自定义属性标签,包括自定义类的标签TableAttribute和自定义字段的标签FieldAttribute。TableAttribute仅定义对应的表或视图名称,FieldAttribute定义字段名称外还要定义是否为主键,执行步骤302;In step 301, a custom attribute tag is created, including a custom class tag TableAttribute and a custom field tag FieldAttribute. TableAttribute only defines the corresponding table or view name, and FieldAttribute defines whether the field name is a primary key or not, and executes step 302;
步骤302中,构建所有业务类,并对每个业务类分别添加与数据库内容一一对应的自定义类的标签和自定义字段的标签,执行步骤303;In step 302, construct all business classes, and add the label of the self-defined class and the label of the self-defined field corresponding to the content of the database respectively for each business class, and execute step 303;
步骤303中,构建ADO类的操作模板SmrAdoTmplate<T>,执行步骤304;In step 303, build the operation template SmrAdoTmplate<T> of ADO class, carry out step 304;
步骤304中,通过继承ADO类的操作模板SmrAdoTmplate<T>方式来构建所有业务类的ADO类,执行步骤305;In step 304, build the ADO class of all business classes by inheriting the operation template SmrAdoTmplate<T> mode of the ADO class, and perform step 305;
步骤305中,在系统中调用业务类对应的ADO类执行持久化操作,执行步骤306;In step 305, the ADO class corresponding to the business class is called in the system to perform the persistence operation, and step 306 is executed;
步骤306中,对于业务类T通过反射机制获取其对应的表名和字段名,执行步骤307;In step 306, for the business class T to obtain its corresponding table name and field name through the reflection mechanism, execute step 307;
步骤307中,调用SQL语句拼接SqlServerConvertor类,根据获取到的表名和字段名信息构建CRUD操作的SQL语句,并执行步骤308,其中SQL具体拼接内容以插入操作为例:采用insertinto{0}({1})values({2})字符串进行插入操作,表名格式化到{0}位置,字段名以逗号分隔拼接成字符串后格式化到{1}位置,根据具体的数据库类型在字段前添加不同前缀,拼接SQL语句参数内容格式化到{2}位置;In step 307, call the SQL statement to splicing the SqlServerConvertor class, construct the SQL statement for the CRUD operation according to the obtained table name and field name information, and execute step 308, where the specific splicing content of the SQL is the insert operation as an example: use insertinto{0}({ 1})values({2}) string to insert, the table name is formatted at {0} position, the field names are separated by commas and concatenated into a string and then formatted at {1} position, according to the specific database type in the field Add different prefixes before, splicing the SQL statement parameter content and formatting it to {2} position;
步骤308中,根据具体T的数据信息构建SQL参数对象,然后调用ADO执行函数,执行步骤309;In step 308, construct the SQL parameter object according to the data information of concrete T, then call the ADO execution function, execute step 309;
步骤309中,判断ADO执行函数是否执行成功,若成功执行步骤311,若不成功则执行步骤310;In step 309, it is judged whether the ADO execution function is executed successfully, if successful execution of step 311, if unsuccessful then execution of step 310;
步骤310中,将异常信息返回给系统处理,执行步骤311;In step 310, the abnormal information is returned to the system for processing, and step 311 is executed;
步骤311,返回结果并关闭数据库连接Step 311, return the result and close the database connection
该数据库访问方法已经被应用于卡斯柯信号有限公司iTC产品ZC/LC工具软件中,该软件的主要功能是对于输入的车辆区域控制和线路控制信息进行处理和转换。This database access method has been applied in the ZC/LC tool software of the iTC product of CASCO Signaling Co., Ltd. The main function of this software is to process and convert the input vehicle area control and line control information.
ZC/LC工具软件使用C#开发,采用SQL数据库完成数据的存储工作。ZC/LC tool software is developed with C#, and uses SQL database to complete data storage.
从系统开发进度看,使用该发明后,编程的复杂度降低了很多,开发效率有比较大的提升;从用户使用情况来看,其交互方式和响应速度也得到了广泛认可。From the perspective of system development progress, after using this invention, the complexity of programming is greatly reduced, and the development efficiency is greatly improved; from the perspective of user usage, its interaction mode and response speed have also been widely recognized.
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN201510451226.8ACN105159920A (en) | 2015-07-28 | 2015-07-28 | Attribute tag based database access method |
| HK16104660.3AHK1216928A1 (en) | 2015-07-28 | 2016-04-22 | A database access method based on attribute labels |
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN201510451226.8ACN105159920A (en) | 2015-07-28 | 2015-07-28 | Attribute tag based database access method |
| Publication Number | Publication Date |
|---|---|
| CN105159920Atrue CN105159920A (en) | 2015-12-16 |
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN201510451226.8APendingCN105159920A (en) | 2015-07-28 | 2015-07-28 | Attribute tag based database access method |
| Country | Link |
|---|---|
| CN (1) | CN105159920A (en) |
| HK (1) | HK1216928A1 (en) |
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN106354605A (en)* | 2016-09-05 | 2017-01-25 | 上海爱数信息技术股份有限公司 | Database access method and system and server provided with system |
| CN107193957A (en)* | 2017-05-23 | 2017-09-22 | 努比亚技术有限公司 | Generation method, terminal and the computer-readable recording medium of SQL |
| CN107992293A (en)* | 2017-12-11 | 2018-05-04 | 国云科技股份有限公司 | An enterprise attribute division system and its implementation method |
| CN108268512A (en)* | 2016-12-30 | 2018-07-10 | 中国移动通信集团上海有限公司 | A kind of tag queries method and device |
| CN108564304A (en)* | 2018-05-04 | 2018-09-21 | 西北工业大学 | A kind of operation characterising parameter generation method towards aircraft assembly |
| CN108628862A (en)* | 2017-03-15 | 2018-10-09 | 北京京东尚科信息技术有限公司 | database addressing method, device and system |
| CN108710504A (en)* | 2017-04-10 | 2018-10-26 | 北大方正集团有限公司 | Database operation method and device |
| CN110096892A (en)* | 2019-04-29 | 2019-08-06 | 武汉中锐源信息技术开发有限公司 | Database Properties access control method and system |
| CN111737225A (en)* | 2020-05-25 | 2020-10-02 | 贵州华泰智远大数据服务有限公司 | Word segmentation technology-based data label database establishment method |
| CN111984666A (en)* | 2019-05-23 | 2020-11-24 | 北京数聚鑫云信息技术有限公司 | Database access method and device, computer readable storage medium and computer equipment |
| CN112905586A (en)* | 2019-12-03 | 2021-06-04 | 阿里巴巴集团控股有限公司 | Data processing and accessing method, device, computer system and readable storage medium |
| CN117611183A (en)* | 2023-09-26 | 2024-02-27 | 重庆赛力斯新能源汽车设计院有限公司 | Commodity pre-sale operation method and device, electronic equipment and readable storage medium |
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN101067814A (en)* | 2007-05-10 | 2007-11-07 | 浪潮集团山东通用软件有限公司 | Mapping conversion method between data access level Xml format data and relational data |
| CN101256650A (en)* | 2008-03-21 | 2008-09-03 | 中国科学院软件研究所 | A business entity-based enterprise data extraction method and system |
| CN101339559A (en)* | 2008-07-18 | 2009-01-07 | 北京航空航天大学 | A method for implementing data persistence |
| CN101706821A (en)* | 2009-12-10 | 2010-05-12 | 中兴通讯股份有限公司 | Tag-based mobile internet page design system and method |
| CN101751260A (en)* | 2009-12-23 | 2010-06-23 | 卡斯柯信号有限公司 | Business object persistence processing method based on dynamic labels |
| CN102054034A (en)* | 2010-12-27 | 2011-05-11 | 华中科技大学 | Implementation method for business basic data persistence of enterprise information system |
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN101067814A (en)* | 2007-05-10 | 2007-11-07 | 浪潮集团山东通用软件有限公司 | Mapping conversion method between data access level Xml format data and relational data |
| CN101256650A (en)* | 2008-03-21 | 2008-09-03 | 中国科学院软件研究所 | A business entity-based enterprise data extraction method and system |
| CN101339559A (en)* | 2008-07-18 | 2009-01-07 | 北京航空航天大学 | A method for implementing data persistence |
| CN101706821A (en)* | 2009-12-10 | 2010-05-12 | 中兴通讯股份有限公司 | Tag-based mobile internet page design system and method |
| CN101751260A (en)* | 2009-12-23 | 2010-06-23 | 卡斯柯信号有限公司 | Business object persistence processing method based on dynamic labels |
| CN102054034A (en)* | 2010-12-27 | 2011-05-11 | 华中科技大学 | Implementation method for business basic data persistence of enterprise information system |
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN106354605B (en)* | 2016-09-05 | 2019-06-21 | 上海爱数信息技术股份有限公司 | A kind of access method of database, system and the server with the system |
| CN106354605A (en)* | 2016-09-05 | 2017-01-25 | 上海爱数信息技术股份有限公司 | Database access method and system and server provided with system |
| CN108268512A (en)* | 2016-12-30 | 2018-07-10 | 中国移动通信集团上海有限公司 | A kind of tag queries method and device |
| CN108628862A (en)* | 2017-03-15 | 2018-10-09 | 北京京东尚科信息技术有限公司 | database addressing method, device and system |
| CN108710504A (en)* | 2017-04-10 | 2018-10-26 | 北大方正集团有限公司 | Database operation method and device |
| CN107193957A (en)* | 2017-05-23 | 2017-09-22 | 努比亚技术有限公司 | Generation method, terminal and the computer-readable recording medium of SQL |
| CN107992293A (en)* | 2017-12-11 | 2018-05-04 | 国云科技股份有限公司 | An enterprise attribute division system and its implementation method |
| CN108564304A (en)* | 2018-05-04 | 2018-09-21 | 西北工业大学 | A kind of operation characterising parameter generation method towards aircraft assembly |
| CN110096892A (en)* | 2019-04-29 | 2019-08-06 | 武汉中锐源信息技术开发有限公司 | Database Properties access control method and system |
| CN111984666A (en)* | 2019-05-23 | 2020-11-24 | 北京数聚鑫云信息技术有限公司 | Database access method and device, computer readable storage medium and computer equipment |
| CN111984666B (en)* | 2019-05-23 | 2024-03-29 | 北京数聚鑫云信息技术有限公司 | Database access method, apparatus, computer readable storage medium and computer device |
| CN112905586A (en)* | 2019-12-03 | 2021-06-04 | 阿里巴巴集团控股有限公司 | Data processing and accessing method, device, computer system and readable storage medium |
| CN111737225A (en)* | 2020-05-25 | 2020-10-02 | 贵州华泰智远大数据服务有限公司 | Word segmentation technology-based data label database establishment method |
| CN117611183A (en)* | 2023-09-26 | 2024-02-27 | 重庆赛力斯新能源汽车设计院有限公司 | Commodity pre-sale operation method and device, electronic equipment and readable storage medium |
| CN117611183B (en)* | 2023-09-26 | 2024-08-30 | 重庆赛力斯新能源汽车设计院有限公司 | Commodity pre-sale operation method and device, electronic equipment and readable storage medium |
| Publication number | Publication date |
|---|---|
| HK1216928A1 (en) | 2016-12-09 |
| Publication | Publication Date | Title |
|---|---|---|
| CN105159920A (en) | Attribute tag based database access method | |
| CN109670053B (en) | Data object mapping method, apparatus, device, and computer-readable storage medium | |
| CN107644286B (en) | Workflow processing method and device | |
| EP3433732B1 (en) | Converting visual diagrams into code | |
| CN106528165A (en) | Code generation method and code generation system | |
| CN110263316B (en) | A file processing method, device and computer equipment | |
| CN113703862A (en) | Configuration-based interface calling method, device, equipment and storage medium | |
| CN104133772A (en) | Automatic test data generation method | |
| CN103777934B (en) | A kind of method and system generating controller CAN alternation of bed based on MATLAB | |
| CN108345458A (en) | A kind of call method and system of static compilation language and script | |
| CN115291773B (en) | Tree structure data display method and device | |
| WO2021022702A1 (en) | Log insertion method and apparatus, computer apparatus and storage medium | |
| CN103177008B (en) | Method and system used for generating and executing structured query language (SQL) statement | |
| WO2025093052A1 (en) | Low-code conversion method and apparatus, readable storage medium, and device | |
| US10338891B2 (en) | Migration between model elements of different types in a modeling environment | |
| CN109582318B (en) | Portobuf localization processing method, device, terminal and storage medium | |
| CN113778388A (en) | Program development method and apparatus | |
| CN108228708B (en) | Big data ETL system and scheduling method thereof | |
| US20100088667A1 (en) | Method and system to modelize resources and external data of a program for procedural language coding | |
| US10635416B2 (en) | Property filtering | |
| CN115729539A (en) | Template file generation method, device, equipment and medium for code development | |
| US20100146417A1 (en) | Adapter for Bridging Different User Interface Command Systems | |
| CN114416090A (en) | A method and device for converting a single-page application framework Vue component | |
| CN114091111A (en) | Method and device for storing intelligent contracts of block chains | |
| CN106156112A (en) | The operational approach of business form and the operation device of business form |
| Date | Code | Title | Description |
|---|---|---|---|
| C06 | Publication | ||
| PB01 | Publication | ||
| C10 | Entry into substantive examination | ||
| SE01 | Entry into force of request for substantive examination | ||
| REG | Reference to a national code | Ref country code:HK Ref legal event code:DE Ref document number:1216928 Country of ref document:HK | |
| RJ01 | Rejection of invention patent application after publication | ||
| RJ01 | Rejection of invention patent application after publication | Application publication date:20151216 | |
| REG | Reference to a national code | Ref country code:HK Ref legal event code:WD Ref document number:1216928 Country of ref document:HK |