@Query(value = "select bill.id_ as id, bill.created_date as date, bill.no, lawyer_case .case_no as caseNo, " +
"lawyer_case .case_name as caseName, customer.no as customerNo, customer.cn_name as customerName, " +
"bill.total_expense_after_tax, bill.collected_money, bill.book_ticket_amount, bill.version " +
"e1.name as creator, bill.status" +
"from bill " +
"left join lawyer_case on lawyer_case .case_no=bill.case_no " +
"left join customer on customer.no=bill.customer_no " +
"left join employee e1 on e1.id_=bill.creator " +
"where IF (?1!='', customer_no=?1, 1=1) " +
"and IF (?2!='', case_no=?2, 1=1) " +
"and IF (?3!='', status=?3, 1=1) " +
"and IF (?4!='', creator'%',?4,'%')), 1=1) " +
"and create_by=?5 " +
"ORDER BY ?#{#pageable} ",
countQuery = "select count(*) " +
"from bill " +
"left join lawyer_case on lawyer_case .case_no=bill.case_no " +
"left join customer on customer.no=bill.customer_no " +
"left join employee e1 on e1.id_=bill.creator " +
"where IF (?1!='', customer_no=?1, 1=1) " +
"and IF (?2!='', case_no=?2, 1=1) " +
"and IF (?3!='', status=?3, 1=1) " +
"and IF (?4!='', creator'%',?4,'%')), 1=1) " +
"and create_by=?5 "+
"ORDER BY ?#{#pageable} ",
nativeQuery = true)
Page<Object[]> findAllBill(String customerNo, String caseNo, Integer status, String creator,
String createBy, Pageable pageable);
Methods to note the following points:
From does not support renaming.
Returns a page <Object []>, only stored data array, no corresponding key, according to the sequence returns only data, are sequentially injected into the DTO.
For paging, you require: "ORDER BY # {# pageable}?", Can be directly into a pageable object will automatically parse.
Note that formatting issues, many times, when the new line, with no spaces.
Carefully correspondence tables in a database field, often can not find a field report is wrong because the field names, and the corresponding database is not on.
This is solved using micro service, a large amount of data needs to remote calls, will reduce the performance of the program.
Pageabel use as a parameter when to be paged. At first, think it is a viable option, but we had to pay attention to the time, when you need to sort the time, it was unable to join the sort field. It would have been an error left *.
Solutions for 7, the native SQL data queries and countQuery into two query methods. COUNT obtained, and then determining, if equal to 0, the process directly returns an empty set; otherwise, the data acquisition taken. We need to calculate their own pages, passing right pageNumber and pageSize. Most systems are in descending order according to modification time. So, order by can write dead. Then pageNumber and dynamic pageSize passed. pageNumber algorithm = (pageNumber - 1) * pageSize, is provided from the beginning PageNumber 1, if 0, the pageNumber = pageNumber * PageSize; so as to ensure correct data.