mybatisplus查询时不启用逻辑删除
时间: 2023-09-06 13:14:22 浏览: 55
在 Mybatis Plus 中,可以通过设置 GlobalConfig 中的 sqlParserConfig 的 isLogicDelete 参数为 false 来禁用逻辑删除。
示例代码:
```
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setSqlParserConfig(new SqlParserConfig().setLogicDelete(false));
```
另外,在实际的查询中,可以使用 Wrapper 来进行条件构造,例如:
```
Wrapper<User> wrapper = new QueryWrapper<User>().eq("name", "张三").eq("age", 18);
List<User> userList = userMapper.selectList(wrapper);
```
这里的 QueryWrapper 是 Mybatis Plus 提供的一个条件构造器,可以用来构造查询条件。在构造查询条件时,可以不加入逻辑删除条件,从而查询到被逻辑删除的数据。
相关问题
mybatisplus实现启用禁用
MyBatis-Plus提供了一种方便的方式来实现启用禁用功能。下面是一个简单的示例:
1. 在你的实体类中添加一个用于表示启用禁用状态的字段,例如:
```java
@TableLogic
private Integer status;
```
`@TableLogic` 注解表示该字段为逻辑删除字段,0表示禁用,1表示启用。
2. 在你的Mapper接口中继承 Mybatis-Plus 提供的 BaseMapper 接口,例如:
```java
public interface UserMapper extends BaseMapper<User> {
}
```
3. 现在你可以使用 MyBatis-Plus 提供的方法进行查询和更新操作,例如:
```java
// 查询所有启用的用户
List<User> userList = userMapper.selectList(Wrappers.<User>lambdaQuery().eq(User::getStatus, 1));
// 禁用用户
int rows = userMapper.update(null, Wrappers.<User>lambdaUpdate().set(User::getStatus, 0).eq(User::getId, userId));
```
通过以上步骤,你可以使用 MyBatis-Plus 实现启用禁用功能。当你查询数据时,MyBatis-Plus 会自动过滤掉禁用的数据。当你更新数据时,只需要设置相应的状态值即可。希望能对你有所帮助!如有更多问题,请继续提问。
springboot+mybatisplus查询指定表的数据
可以使用Mybatis-plus提供的Wrapper来指定查询的条件,具体的方法如下:
1. 在pom.xml文件中添加mybatis-plus的依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.x.x</version>
</dependency>
```
2. 在启动类上添加@EnableTransactionManagement和@MapperScan注解,分别启用事务管理和Mapper扫描:
```
@EnableTransactionManagement
@MapperScan("com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
3. 在mapper接口中定义查询方法,如下面的例子:
```
public interface UserMapper extends BaseMapper<User> {
@Select("select * from user where name = #{name}")
User selectByName(String name);
@Select("select * from user ${ew.customSqlSegment}")
List<User> selectWithWrapper(@Param(Constants.WRAPPER) Wrapper<User> wrapper);
}
```
4. 在service中调用mapper接口的查询方法,如下面的例子:
```
public List<User> selectWithWrapper(String name) {
QueryWrapper<User> wrapper = new QueryWrapper<User>().eq("name", name);
return userMapper.selectWithWrapper(wrapper);
}
```
通过上述步骤,即可使用Mybatis-plus和Spring Boot来查询指定表的数据。