Java project combat day 11: the realization of the search function

Java project combat day 11: the realization of the search function

Today is the 110th day that Liu Xiaoai has taught herself Java.

Thank you for watching, thank you.

Not much to say, start today's study:

Note in advance: about the realization of today's search function.

It does not use mainstream search technology such as inverted index, just a normal fuzzy query.

Before that, learn a small knowledge point, sql statement dynamic splicing.

1. sql statement dynamic splicing

When we write SQL statements, we usually use pre-compiled queries, using the wildcard character "?"

However, sometimes there may be cases where a certain parameter does not exist. For example, this time the query front end passes 3 parameters, but the next time only 2 parameters are passed.

Faced with this problem, what should we do?

You need to use the dynamic splicing of sql statements, the code is written as follows:

The cid parameter in the sql statement is uncertain, sometimes there is, sometimes it may not.

The use of StringBuilder

First store the determined part of the sql statement in StringBuilder, and then create a collection to store the parameters.

Make a judgment on cid

If cid is not empty and exists, splice "and cid = ?" in the sql statement, and store cid in the parameter set at the same time.

Splicing other parameters behind cid

Splice "limit ?,?" in the sql statement, and then store the other two fixed parameters into the collection.

Note : Don't forget the spaces during the sql splicing process. In short, make sure that the spliced SQL is the same as the one in , which can be viewed through debug.

Query data

Convert the StringBuilder that dynamically stores the sql statement into a string, convert the set of stored parameters into an array, and then use jdbcTemp to query.

2. front and back code writing

1 business analysis

We need to find the static page where the search box is located, as shown below:

To solve two problems:

We want to bind a click event to the search, and after clicking it, it will jump to the route_list page to display the corresponding data queried.

The bound function is queryByRname()

We want to get the data entered in the search box.

Here you can select the label corresponding to the search box through the hierarchical selector $(".search input").

2 front-end code implementation

Define the function queryByRname()

Get the content in the search box

Using the hierarchical selector, call jQuery's val() method to get the data in the corresponding label, which is the search box here.

Jump to route_list.html page

location.href can jump to the corresponding page. At the same time, rname is passed to the background as a parameter.

route_list.html page code

The jump in carries a parameter. Naturally, it is necessary to obtain the parameter and then add it to the server request parameter.

The parameters need to be decoded, because the browser will automatically encode the data entered in the search box.

Use the decodeURL() function to complete the encoding.

3The background accepts the parameter

Add parameters on the basis of existing business logic

Only need to add the code to get the data of rname in the web layer.

At the same time, it is passed to the service layer as a parameter, and the service layer also adds the parameter to the dao layer to query.

The other codes do not need to be changed, but an rname parameter is added.

In the same way, SQL query statements are dynamically spliced in the dao layer.

In short, you can write a complete SQL statement first, and then dynamically splice the rname parameter.

The check method can be used to judge whether the sql statement is spliced correctly through debug.

3. search box data backfill

After we enter the information in the search box and click search, the contents of the search box will disappear.

In fact, they should still exist, so what should we do?

You need to consider data backfilling:

A rough idea is:

After obtaining the data in the search box, reset it to the corresponding label.

The notable ones are:

The parameters need to be decoded and the page load event needs to be bound.

At last

thanks for watching.

If you can, please give me a like, thank you.