mybatisplus查询带上默认字段,比如sql语句最后加上is_del = 0
时间: 2023-08-23 21:12:22 浏览: 252
在 MybatisPlus 中,可以通过在实体类中添加 @TableLogic 注解来实现逻辑删除功能。@TableLogic 注解标记的字段默认值为 1,表示未删除状态,当该字段值为 0 时表示已删除状态。如果需要在查询时自动加上逻辑删除的条件,可以通过全局配置或者在查询方法中手动添加。
全局配置方式:
在 MybatisPlus 的配置文件中添加以下配置:
```xml
<global-config>
<db-config>
<!-- 开启逻辑删除 -->
<logic-delete-value>0</logic-delete-value>
<logic-not-delete-value>1</logic-not-delete-value>
</db-config>
</global-config>
```
这样在查询时,MybatisPlus 会自动在 SQL 语句中添加条件:is_del = 1。
手动添加方式:
在查询方法中使用 QueryWrapper 对象来构建查询条件,可以通过 wrapper.eq() 方法手动添加逻辑删除条件,例如:
```java
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("is_del", 0);
List<User> userList = userMapper.selectList(wrapper);
```
这样查询出来的结果就会自动带上逻辑删除的条件。
相关问题
select s.sex as sex, if(s.sex = 0, '女', '男') as sexText, s.political as political, dd.dict_value as politicalText, s.certificate as certificate, dd1.dict_value as certificateText, s.household as household, dd2.dict_value as householdText, s.pay_type as payType, dd3.dict_value as payTypeText, s.enroll_mode as enrollMode, dd4.dict_value as enrollModeText, s.admission_batch as admissionBatch, dd5.dict_value as admissionBatchTypeText, s.cultivation_level as cultivationLevel, dd6.dict_value as cultivationLevelText, s.cultivation_mode as cultivationMode, dd7.dict_value as cultivationModeText, s.learning_type as learningType, dd8.dict_value as learningTypeText, s.subject as subject, dd9.dict_value as subjectText, dd.is_del as is_del, dd.status as status from student as s left join data_dictionary as dd on s.political = dd.id left join data_dictionary as dd1 on s.certificate = dd1.id left join data_dictionary as dd2 on s.household = dd2.id left join data_dictionary as dd3 on s.pay_type = dd3.id left join data_dictionary as dd4 on s.enroll_mode = dd4.id left join data_dictionary as dd5 on s.admission_batch = dd5.id left join data_dictionary as dd6 on s.cultivation_level = dd6.id left join data_dictionary as dd7 on s.cultivation_mode = dd7.id left join data_dictionary as dd8 on s.learning_type = dd8.id left join data_dictionary as dd9 on s.subject = dd9.id where 1 = 1 and dd9.is_del = 1 and dd9.status = 1 and dd8.is_del = 1 and dd8.status = 1 and dd7.is_del = 1 and dd7.status = 1 and dd6.is_del = 1 and dd6.status = 1 and dd5.is_del = 1 and dd5.status = 1 and dd4.is_del = 1 and dd4.status = 1 and dd3.is_del = 1 and dd3.status = 1 and dd2.is_del = 1 and dd2.status = 1 and dd1.is_del = 1 and dd1.status = 1 and dd.is_del = 1 and dd.status = 1
这是一个 SQL 查询语句,用于查询学生信息表(student)中的各个字段,并通过左连接查询各个字段对应的字典表(data_dictionary)中的对应值。其中,查询的字段包括性别、政治面貌、证件类型、户籍类型、缴费类型、入学方式、录取批次、培养层次、培养方式、学习类型、专业等,并将对应的字典值查询出来。同时,WHERE 子句中包括了一系列过滤条件,用于限定查询结果的范围。
优化 SELECT * FROM tbl_park_recode t WHERE car_number = '贵EUY662' and is_color = 1 AND is_out_site=0 AND is_fee!='-2' AND del_flag!=1 order by t.in_site_time desc limit 1
这个SQL查询语句的优化可以考虑以下几个方面:
1. 添加索引
在 `tbl_park_recode` 表中,可以考虑为 `car_number`、`is_color`、`is_out_site`、`is_fee`、`del_flag`、`in_site_time` 字段添加索引,以加快查询速度。
2. 使用字段列表代替 *
在查询语句中,使用字段列表代替 * 可以减少不必要的数据传输,提高查询效率。
3. 避免使用不等于操作符
在查询语句中,使用不等于操作符 `!=` 可能会导致索引失效,影响查询效率。可以使用其他方式代替,比如使用 `>` 或 `<`。
4. 避免使用函数
在查询语句中,使用函数可能会导致索引失效,影响查询效率。可以使用其他方式代替,比如使用范围查询。
综上所述,一个优化后的查询语句可能如下所示:
```
SELECT car_number, is_color, is_out_site, is_fee, del_flag, in_site_time
FROM tbl_park_recode
WHERE car_number = '贵EUY662' AND is_color = 1 AND is_out_site = 0 AND is_fee > -2 AND del_flag <> 1
ORDER BY in_site_time DESC
LIMIT 1;
```
同时,为了进一步提高查询效率,可以考虑对表结构进行优化,比如拆分大表、合并小表、增加冗余字段等。
阅读全文