grails criteria :How to select all columns in mysql and at the same time format the date then use like

青春壹個敷衍的年華 提交于 2020-01-07 07:22:12

问题


I'm currently creating a search functionality for my page. The data will be coming from DB.

In the HTML, the date is displayed in this format dd/mm/yyyy. What I want to do is, in the select query, I want to select the all columns and the date should be displayed using that format and I have a a couple of like conditions that will be used to check if there is a match.

How will I select all column, format date and at the same time check for all matches using one single statement in criteria?

Here's what I've done so far, but this wrong.

 searchString = searchString.toLowerCase() 

        def employeeSearchCri = Employee.createCriteria();
        def searchedEmployeeList = employeeSearchCri.list(){
            or {
               ilike("employeeNo", "%" + searchString + "%")
               ilike("firstName", "%" + searchString + "%")
               ilike("lastName", "%" + searchString + "%")
               ilike("middleName", "%" + searchString + "%")
               ilike("jobPosition", "%" + searchString + "%")
               ilike("date_format(hireDate, '%d/%m/%Y' )", "%" + searchString + "%")
               ilike("status", "%" + searchString + "%")
            }
            and{
                eq("companyId",companyId)
            }
        }
        return searchedEmployeeList;     

回答1:


The first thing I recommend is to comment out all of the ilikes except for one. Pick any one. The point is to focus on getting just one of them to work. Then, and only then, add another ilike.

Second, confirm that the pattern your using (ex. %SEARCHSTRING%) is valid for the LIKE operator of the database your using.

Third, once you get it working, you'll learn that unfortunately it won't work as well as you'd like. For example, multi-word searches won't work with your approach. Searching is hard. I got it working quite well, but boy was it a pain. If you want it done right, it's best to let the pros handle it, such as by using Apache Lucene.

Matching the date

That said, the best way to handle the date is to convert it into a Date object and to match on that. You can use something like this:

import java.text.SimpleDateFormat

def searchDate

try { 
    searchDate = new SimpleDateFormat('dd/MM/yyyy').parse(searchString)
} catch (java.text.ParseException e) { }

...

or {
    ...
    if(searchDate) eq("hireDate", searchDate)
}

Tip

You can re-write your ilikes in a more concise form by using GStrings:

ilike("employeeNo", "%$searchString%")

And if you build a list of the columns you want to use ilike on, you can simplify your query. Here's a complete example:

searchString = searchString.toLowerCase()

def columns = [
    "employeeNo",
    "firstName",
    "lastName",
    "middleName",
    "jobPosition",
    "status"
]

def searchDate

try { 
    searchDate = new java.text.SimpleDateFormat('dd/MM/yyyy').parse(searchString)
} catch (java.text.ParseException e) { }

def employeeSearchCri = Employee.createCriteria();
def searchedEmployeeList = employeeSearchCri.list() {
    or {
        columns.each { ilike(it, "%$searchString%") }        
        if(searchDate) eq("hireDate", searchDate)
    }

    and{
        eq("companyId",companyId)
    }
}

return searchedEmployeeList


来源:https://stackoverflow.com/questions/32062449/grails-criteria-how-to-select-all-columns-in-mysql-and-at-the-same-time-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!