Learn Mybatis step by step-how to implement dynamic SQL query (6)

Source: Internet
Author: User

In the previous chapter, we have finished the paging usage of Mybatis. In fact

      

Because our parameters pageIndex and pageSize are both int values, we can directly determine this. If it is an object instance, we can use null judgment to control some dynamic logic, the actual development depends on the business needs. Here I think it should be noted that it is not very easy to write and &, which will not be recognized in the configuration ~.

(2) usage of choose (when, otherwise)

  The choose when is mainly used in scenarios where multiple conditions are met and only one of them is used. For example, a query condition is constructed to pass the id, name, and createTime respectively. Assume that when we query a VisitorId table, if VisitorId has a value, we use Id to query. If VisitorName has a value, we use VisitName to query the table. mybatis. demo. add the List <Visitor> getListChooseWhenDemo (BasicQueryArgs args) method to the IVisitorOperation interface class. Add the corresponding select node configuration in VisitorMapper:

Delete (Visitor query (List <Visitor> List <VisitorWithRn> Visitor basicQuery (List <Visitor> getListForeachDemo (List <Integer>Modified IvisitorOperationChoose-when-otherwise dynamic condition configuration

(3) usage of where if (trim)

The advantage of the where keyword is that if a filter condition exists, it knows to insert the where keyword when appropriate. It also knows when to remove the corresponding and or connector, mainly to deal with the following scenarios

    

Not because all conditions are not met.


Or because the first condition is not met, simply the following conditions are met


Therefore, we can add the corresponding public List <Visitor> getListWhereDemo (BasicQueryArgs args) to the IVisitorOperation Interface Class in the choose when condition example ), add the following configurations in the VisitorMapper configuration file:

Where if dynamic condition configuration

(4) foreach usage

Foreach dynamic condition configuration

Finally, you only need to create the corresponding test method in DemoRun, and the dynamic SQL in Mybatis is completed. The DemoRun method used in the following test is used.

GetListForeachDemo (List <Integer> = session. getMapper (IVisitorOperation. <Visitor> ls = getListWhereCondition (= name = ""? === Session. getMapper (IVisitorOperation. <Visitor> ls = (ls. size () = 0 "no matching! "GetListChooseWhenDemo (= name = ""? === Session. getMapper (IVisitorOperation. <Visitor> ls = (ls. size () = 0 "no matching! "DemoRun Method

The last chapter describes how to generate these configurations and files using the MybatisGenerator generation tool ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.