• 关于MySQL的like模糊查询


    一、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);
            }
        }
     
    }
  • 相关阅读:
    four day (linux基础学习1)
    three day(网络基础)
    two day(操作系统)
    Oldboy One day(计算机基础)
    mybatis调用存储过程
    调用jiathis分享接口
    汉字转换拼音工具类
    Ajax前后台交互 返回普通格式和JSON格式
    Bootstrap与EasyUI入门
    MD5,sha1加密工具类
  • 原文地址:https://www.cnblogs.com/ZJOE80/p/15741706.html
Copyright © 2020-2023  润新知