MySQL Query database table field list information dynamically sorted

MySQL Query database table field list information dynamically sorted


 

Background Description

When the data item list display page, Table components used in the front end, each column comes to this page data is performed in ascending or descending sort.

But customer expectations : randomly click on a column when the query to the all the data to be sorted.

 

for example:

 

There are 100 to query data, page 10 of 10 pages. Table front end assembly 10 may of this page data, sort, does not participate in the other nine sorted.

However, the actual expectation is to query the data of 10 are involved in sorting.

And is not a particular column, but each column can be in ascending or descending order, the results of the query to be sorted again.


 

Treatment

Due to limitations of front-end technology, the need for back-end processing, query interface to modify the list.

1, modify the query entity class

First need to query the requesting entity, two additional fields orderField and OrderType , field names and for storing the incoming sort mode (ascending ASC, descending DESC)

orderField: means for receiving a field to be sorted

orderType: receiving a mode for sorting, ASC, DESC, asc, desc

code show as below:

    // 对字段进行排序
    private String orderField;    // 存放排序字段
    private String orderType;     // ASC DESC

    public String getOrderField() {
        return orderField;
    }

    public void setOrderField(String orderField) {
        this.orderField = orderField;
    }

    public String getOrderType() {
        return orderType;
    }

    public void setOrderType(String orderType) {
        this.orderType = orderType;
    }

 

2, modify the file mapper.xml

MyBatis find the Mapper file, SQL query in the list of methods.

Add the following code:

<if test="orderField != null and orderField != '' and orderType != null and orderType != ''">
  ORDER BY ${orderField} ${orderType} 
</if>

[Note] In particular: two parameters of SQL here, be sure to use $ {}  received, but can not use # {} . Otherwise, it will not reach the desired effect.

$ {} : Not process incoming string . For example: the incoming and create_time desc, $ {} after the treatment effect is ORDER BY create_time desc, may be implemented in reverse order create_time field effect.

# {} : It will process the incoming string . For example: the incoming and create_time desc, # {} after the treatment effect is ORDER BY 'create_time' 'desc'as string constants will not reach the reverse order according create_time field effect.

Of course, $ {} may lead to SQL injection. Generally, # {} are used to. This scenario does not need to be passed in the conversion value, $ {} will be used.

In order to realize the function title, just use the $ {} This feature only.

 

3, increasing the interface calls field

When the reference transmission, increasing  orderField  and  OrderType , the following effects

{
    "pageNum":1,
    "pageSize":5,
    "orderField":"create_time",
    "orderType":"desc"
}

 

 

So far, the function ordered by the incoming field names and sort realized.

 

Guess you like

Origin www.cnblogs.com/miracle-luna/p/11360117.html