战狂粗人张

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

一、like(完全模糊,即“like '%val%'”)

实例:查询用户名称中包含“王”值的用户信息,如下:

QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
wrapper.like("name", "王"); // 等价 SQL 语句:name like '%王%'

 

二、notLike(完全模糊取非,即“not like '%val%'”)

实例:查询用户包含“王”值的用户信息,如下:

QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
wrapper.notLike("name", "王"); // 等价 SQL 语句:name not like '%王%'

 

三、likeLeft(仅左边模糊,即“like '%val'”)

实例:查询用户名以“王”值结束的用户信息列表,如下:

QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
wrapper.likeLeft("name", "王"); // 等价 SQL 语句:name like '%王'

 

四、likeRight(仅右边模糊,即“like 'val%'”)

实例:查询用户名以“王”值开始的用户信息列表,如下:

QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
likeRight("name", "王"); // 等价 SQL 语句:name like '王%'

 

五、示例代码

package com.hxstrive.mybatis_plus.simple_mapper.condition;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxstrive.mybatis_plus.mapper.SimpleMapper;
import com.hxstrive.mybatis_plus.model.UserBean;
import org.junit.jupiter.api.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
class Condition7Test {
 
    @Autowired
    private SimpleMapper simpleMapper;
 
    @Test
    void contextLoads() {
        System.out.println("================== 分割线 (like '%佳%') ===================");
        QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
        wrapper.like("name", "佳");
        List<UserBean> userBeanList = simpleMapper.selectList(wrapper);
        for(UserBean userBean : userBeanList) {
            System.out.println(userBean);
        }
 
        System.out.println("================== 分割线 (not like '%佳%') ===================");
        wrapper = new QueryWrapper<>();
        wrapper.notLike("name", "佳");
        userBeanList = simpleMapper.selectList(wrapper);
        System.out.println("result size=" + userBeanList.size());
        for(int i = 0; i < (userBeanList.size() > 10 ? 10 : userBeanList.size()); i++) {
            System.out.println(userBeanList.get(i));
        }
 
        System.out.println("================== 分割线 (like '黄%') ===================");
        wrapper = new QueryWrapper<>();
        wrapper.likeRight("name", "黄");
        userBeanList = simpleMapper.selectList(wrapper);
        for(UserBean userBean : userBeanList) {
            System.out.println(userBean);
        }
 
        System.out.println("================== 分割线 (like '%佳') ===================");
        wrapper = new QueryWrapper<>();
        wrapper.likeLeft("name", "佳");
        userBeanList = simpleMapper.selectList(wrapper);
        for(UserBean userBean : userBeanList) {
            System.out.println(userBean);
        }
    }
 
}

 

posted on 2021-12-28 18:01  战狂粗人张  阅读(608)  评论(0编辑  收藏  举报