zoukankan      html  css  js  c++  java
  • 关于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);
            }
        }
     
    }
  • 相关阅读:
    BZOJ 5314: [Jsoi2018]潜入行动
    BZOJ 3420: Poi2013 Triumphal arch
    BZOJ 1135: [POI2009]Lyz
    BZOJ 4247: 挂饰
    本地
    生成config文件到内存中
    微信获取access_token和curl
    php生成静态页面
    curl
    分页
  • 原文地址:https://www.cnblogs.com/ZJOE80/p/15741706.html
Copyright © 2011-2022 走看看