Spring data jpa @ query using native SQl, need to pay attention to the pit

Spring data jpa @Query using pit of native Sql

The code illustrated:

@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:

  1. From does not support renaming.
  2. Returns a page <Object []>, only stored data array, no corresponding key, according to the sequence returns only data, are sequentially injected into the DTO.
  3. For paging, you require: "ORDER BY # {# pageable}?", Can be directly into a pageable object will automatically parse.
  4. Note that formatting issues, many times, when the new line, with no spaces.
  5. 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.
  6. This is solved using micro service, a large amount of data needs to remote calls, will reduce the performance of the program.
  7. 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 *.
  8. 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.
/**
* pageInfos: 转换之后的数据。
* pageable:传入的pageable.
* totalPage: 第一条SQL算好的返回值。
* 这样就可以统一的返回各种pageDTO。
*/
private Page<T> convertForPage(List<T> pageInfos, Pageable pageable, Integer totalPage) {
        return new PageImpl<>(pageInfos, pageable, totalPage);
    }

Guess you like

Origin blog.csdn.net/weixin_38608626/article/details/83183556
Recommended