mybatisplus notlike使用
时间: 2023-07-06 12:43:20 浏览: 247
MybatisPlus 中的 notLike 方法可以用于模糊查询不包含指定字符串的记录。
以下是 notLike 方法的使用示例:
```java
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.notLike("name", "Tom");
List<User> userList = userMapper.selectList(wrapper);
```
上述代码将查询出所有 name 字段不包含 "Tom" 的 User 记录。
如果您希望查询出 name 字段不以 "Tom" 开头的记录,可以使用 notLikeRight 方法:
```java
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.notLikeRight("name", "Tom");
List<User> userList = userMapper.selectList(wrapper);
```
上述代码将查询出所有 name 字段不以 "Tom" 开头的 User 记录。
相关问题
mybatisplus中not使用
在MyBatis-Plus中,可以使用`not`关键字进行条件的取反操作。通过使用`not`关键字,可以将原本为真的条件取反为假,或者将原本为假的条件取反为真。
在MyBatis-Plus中,可以使用`not`关键字来构建查询条件,例如:
```java
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.notLike("name", "Tom");
```
上述代码表示查询名字不包含"Tom"的用户。
除了`notLike`方法,MyBatis-Plus还提供了其他一些常用的`not`方法,如`notIn`、`notBetween`等,用于构建不包含某些值或不在某个范围内的查询条件。
mybatisplus使用if else
MyBatis Plus provides a convenient way to use if-else conditions in SQL queries. You can use the `@if` annotation to conditionally include a part of the SQL statement based on a specified condition. Here's an example of how to use if-else conditions with MyBatis Plus:
1. First, you need to import the necessary annotations in your mapper interface:
```java
import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
```
2. Then, define your SQL query in the mapper interface using the `@Select` annotation:
```java
public interface UserMapper extends BaseMapper<User> {
@Select("SELECT * FROM user " +
"WHERE 1=1" +
"<if test=\"name != null and name != ''\">" +
"AND name = #{name}" +
"</if>" +
"<if test=\"age != null\">" +
"AND age = #{age}" +
"</if>")
List<User> getUsers(@Param("name") String name, @Param("age") Integer age);
}
```
In this example, the SQL query selects all users from the `user` table. However, the `name` and `age` conditions are included only if the corresponding parameters are not null. The `@Param` annotation is used to map the method parameters to the SQL query parameters.
3. Finally, you can use this method in your service or controller:
```java
@Autowired
private UserMapper userMapper;
public List<User> searchUsers(String name, Integer age) {
return userMapper.getUsers(name, age);
}
```
When calling the `searchUsers` method, you can pass the `name` and `age` parameters to filter the results accordingly.
This is just a basic example of using if-else conditions in MyBatis Plus. You can also use other conditional annotations like `@when`, `@otherwise`, and `@choose` for more complex scenarios.
阅读全文