Database table access encapsulation of java game server

There are not many database tables involved in the project, but it is very inefficient to manually splice strings for each select, insert, update and delete, especially when the structure needs to be modified from time to time. One goal of development is automation, that is, don't do things manually that can be realized automatically; Another principle is simplification, that is, try to ensure that data or logic has one entry and one exit. This requirement can be solved by using some open source libraries, but because the requirement is simple and the goal is clear, it is not necessary to introduce redundant third-party libraries. So I wrote one to at least meet the current needs.

The encapsulation of database tables has two core classes, table and record. First, you need a table class to save the description of the database table structure, and then automatically generate the corresponding SQL statements. Secondly, a record class is required to automatically set SQL parameters and automatically generate logical objects from the returned result set.

The table structure description of the table class can be obtained from the database automatically or loaded from the configuration table. Here, the loading method from the configuration table is selected, which is simple to implement and more widely used.

The following is a configuration example of an account table (user. XML).

Only one primary key is defined, which can be extended if necessary. Each column name corresponds to the column name of the database table, field corresponds to the member variable name of the logical object, and type corresponds to the type of the field, such as int, string, timestamp, etc. with the name and type, you can automatically get and set data using reflection.

The table class reads the configuration file to obtain the structure description of the data table.

Then generate the preprocessed SQL strings of select, insert, update and delete read and written in preparestatement mode. For example, update:

There are so many functions of the table class. The following is the key record class, which uses reflection to automatically access data.

The template parameter t is the logical object corresponding to a table record. In our example, the account data class:

With SQL statements, you must set parameters before executing them. Primary keys and normal fields are set separately.

According to the table structure description, get the value of the corresponding field through reflection, and then set.

Todbvalue is used to convert the Java logical type to the corresponding database type, such as time. In logic, it is long, and the database type is timestamp.

Take setting the update SQL parameter as an example:

Then execute the SQL statement. If it is a select statement, it will also return a result set. The principle of automatically generating logical objects from the result set is similar. It is an inverse process. See the code at the end of the article for details.

A complete example of using is given below:

The code is easily encapsulated, and there are more requirements that can be improved accordingly.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>