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 ~