Mybatis-Plus实践学习(十九)

2023-12-13 11:08:12 浏览数 (1)

5.2、基本比较操作

eq

等于 =

ne

不等于 <>

gt

大于 >

ge

大于等于 >=

lt

小于 <

le

小于等于 <=

between

BETWEEN 值1 AND 值2

notBetween

NOT BETWEEN 值1 AND 值2

in

字段 IN (value.get(0), value.get(1), ...)

notIn

字段 NOT IN (v0, v1, ...)

测试用例:

代码语言:javascript复制
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testEq() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE password = ?
AND age >= ? AND name IN (?,?,?)
wrapper.eq("password", "123456")
.ge("age", 20)
.in("name", "李四", "王五", "赵六");
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
}

5.3、模糊查询

like

LIKE '%值%'

例: like("name", "王") ---> name like '%王%'

notLike

NOT LIKE '%值%'

例: notLike("name", "王") ---> name not like '%王%'

likeLeft

LIKE '%值'

例: likeLeft("name", "王") ---> name like '%王'

likeRight

LIKE '值%'

例: likeRight("name", "王") ---> name like '王%'

测试用例:

代码语言:javascript复制
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testWrapper() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
//SELECT id,user_name,password,name,age,email FROM tb_user WHERE name LIKE ?
//Parameters: %曹%(String)
wrapper.like("name", "曹");
List<User> users = this.userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
}

0 人点赞