mybatis-plus使用find_in_set
时间: 2023-08-17 16:16:15 浏览: 199
mybatis_plus
Mybatis-plus使用find_in_set函数可以实现对字段中包含指定值的查询。在使用lambda表达式拼接SQL时,可以通过apply方法来使用find_in_set函数。具体的代码示例如下:
```java
List<String> realIds = crmProgrammeService.getIdsByRealEstateNames(name);
if (!CollectionUtils.isEmpty(realIds)){
if (realIds.size() == 1){
queryWrapper.like("real_estate_ids", realIds.get(0));
} else {
for (int i = 0; i < realIds.size(); i++) {
if (i == 0) {
queryWrapper.apply(null != realIds.get(i), "(find_in_set({0},real_estate_ids)", realIds.get(i));
} else {
if (i == realIds.size() - 1) {
queryWrapper.or().apply(null != realIds.get(i), "find_in_set({0},real_estate_ids))", realIds.get(i));
} else {
queryWrapper.or().apply(null != realIds.get(i), "find_in_set({0},real_estate_ids)", realIds.get(i));
}
}
}
}
} else {
queryWrapper.isNull("real_estate_ids");
}
```
以上代码中,通过apply方法传入find_in_set函数的参数,实现了对real_estate_ids字段进行查询。具体的SQL语句如下:
```sql
SELECT * FROM `crm_programme` where find_in_set(1,real_estate_ids) or find_in_set(3,real_estate_ids);
```
这条SQL语句会查询crm_programme表中real_estate_ids字段包含1或3的记录。\[1\]\[2\]
#### 引用[.reference_title]
- *1* *2* [记录 mybatis plus QuerWapper使用 FIND_IN_SET](https://blog.csdn.net/qq_34410726/article/details/126561485)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [mybatis-plus中使用FIND_IN_SET函数](https://blog.csdn.net/qq_41289165/article/details/116115674)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文