Use spri to automatically generate rest API from the database

If you have an existing database and you want to write a front end to use it, you will often find yourself spending hours establishing a connection between the database and the front end. If you can simply press a button and directly generate the entire rest application programming interface, it will use your time more efficiently.

Speedment is a tool that uses code generation to generate customized domain models based on existing database structures. Queries can be sent directly to the database or provided from memory for better performance. In this article, we will use the free accelerated official plug-in "Spring Generator" to generate a complete spring application to provide a simple rest application programming interface. We will add support for paging, remote filtering and sorting without writing a single line of code.

In the following example, we will use the Sakila database for MySQL. Sakila is a sample database that simulates a movie rental shop. It has tables called movies, actors, categories, etc.

The first thing we need to do is to configure our files to use the latest acceleration dependencies and Maven plugins. The fastest way is to generate a file using the accelerated initializer you can find here. If you select "Spring" in the plug-in list, and then press "Download", you will get a complete project folder and an automatically generated file.


Next, open the command line of the downloaded file and enter the following:


This will launch the acceleration tool and prompt you to enter the license key. Just select "Start for Free" and you will get a free license! After registration, you can connect to the database and start using it.

By default, in addition to the regular accelerated database real-time synchronization class, the only thing the plug-in adds is a bean. To actually generate a controller for your table, you can select the table in the tree on the left, and then select "Generate @RestController" on the right.

This will enable many of the options below it, but we will discuss these options in a minute. Now you only need to do this to enable the controller logic. Click "Generate" to generate the code.

In the integrated development environment, you can now see many classes. If you downloaded the -file from the initializer, then you should already have the -file. Make sure it is in the correct folder, then build and run the application.


You can call:


Keep it simple.

A cool feature of the spring-generator plugin is that it supports remote filtering. This means that the front end can send the predicate encoded as a JSON object to the server, and the server will respond with the filtered JSON response. Acceleration will automatically parse the JSON filter into a SQL statement. If you want to be able to handle thousands of requests per second, you can also enable Speedment Datastore to process queries directly from memory. In this case, the JSON filter will be parsed to find the appropriate memory index.

The syntax of the JSON filter is very simple. To get movies less than 60 minutes in length, you only need to dial:


(The G parameter ensures that the command is sent as a get request and not a publish request)

You can add multiple filters by wrapping objects in the list.


This will return all movies between 30 and 60 minutes in length. By default, all operators in the list are considered to be separated by the "and" operator. All conditions must apply to the rows that pass the filter. To change this situation, you can use an explicit OR statement.


This will return all videos shorter than 30 minutes or longer than 1 hour.

By default, acceleration returns rows in the same order as the database returns. However, if you add a filter to the expression, the order of return is undefined. To ensure that the order is well defined on the front end, you can therefore send a defined order to the generated back end and tell it how to sort the elements. If you use accelerated data storage to serve the elements in memory, then it will use the in-memory index to resolve the correct order.

Here is an example, movies are retrieved by length.


To get the videos in reverse order, you can specify a descending order as follows:


You can also send multiple sequencers to the backend to define main orders, auxiliary orders, and so on.


The last feature of the plug-in is the ability to page results to avoid sending unnecessary large objects to the browser. This is enabled by default, which is why when you query the backend, you won't see more than 25 results. To skip many results (not pages), you can specify parameters.


This will skip the first 25 elements, starting from the 26th. You can change the default page size by adding parameters.


This also starts at the 26th element, but only returns 5 elements instead of 25.

In this article, you have learned how easy it is to use Speedment, Spring, and plugins to get up and running a complete REST application programming interface.

Guess you like

Origin blog.csdn.net/weixin_49470452/article/details/107506911