mybatis if list
时间: 2023-10-24 21:04:56 浏览: 153
In MyBatis, you can use `<if>` tags to conditionally include or exclude parts of your SQL queries. When dealing with a list, you can use the `<foreach>` tag in combination with the `<if>` tag to iterate over the list and conditionally generate SQL statements.
Here's an example of how you can use `<if>` and `<foreach>` tags with a list in MyBatis:
```xml
<select id="getUserList" resultType="User">
SELECT * FROM users
WHERE 1=1
<if test="userIds != null and !userIds.isEmpty()">
AND user_id IN
<foreach item="userId" collection="userIds" open="(" separator="," close=")">
#{userId}
</foreach>
</if>
</select>
```
In this example, the SQL query selects all users from the `users` table. If the `userIds` list is not null and not empty, it dynamically adds a condition to filter the results by the `user_id` column using the `IN` operator.
You can pass the `userIds` list as a parameter to your MyBatis mapper method:
```java
List<User> getUserList(List<Integer> userIds);
```
By doing so, MyBatis will handle the iteration and conditional inclusion of the SQL statements based on the values in the `userIds` list.
阅读全文