SpringBoot-mybatisplus-模糊查询

模糊查询如何实现如下案例中两种实现方法

第一种:利用QueryWrapper.like自己实现。
第二种:使用@TableField(condition = SqlCondition.LIKE)实现。
特别注意:这里要特别强调一下第二种方法,对于mysql什么都不用改就可以成功,但是oralce需要自己重写like的拼接方法,因为mysql和oracle的CONCAT函数不同,oralce不接收三个以上的参数,不重写就会报“参数个数无效”的错误,这是个大坑千万注意。
如果使用的是mysql则下面的SqlCondition.java 不用写也可以正常使用。

1、数据准备(oracle版)

 CREATE TABLE TEST_BLOCK_T
(
  BLOCK_ID    VARCHAR2(10 BYTE),
  BLOCK_NAME  VARCHAR2(200 BYTE),
  TEST_NAME   VARCHAR2(200 BYTE)
);
Insert into TEST_BLOCK_T  (BLOCK_ID, BLOCK_NAME, TEST_NAME) Values  ('99999', 'PHP哈JAVA', '你好啊');
COMMIT;

2、pom.xml文件

 <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.3.7.RELEASE</version>
    </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.3.7.RELEASE</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


    <!-- 集成mybatis-plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.1</version>
    </dependency>


    <!-- oracle驱动 -->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
    </dependency>



</dependencies>

3、配置文件

 

 

4、工程结构

 

 

5、源码

package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.domain.Block;
import com.example.demo.mapper.BlockMapper;
import com.example.demo.service.IBlockTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private IBlockTService iBlockTService;

    @Autowired
    private BlockMapper blockMapper;


    @GetMapping("/list")
    @ResponseBody
    public List index(Block block) {
        Map likeMap = new HashMap();
        likeMap.put("BLOCK_NAME","哈");
        Map  map = iBlockTService.selectList(block,likeMap);
        List<Block> list =  (List)map.get("dataList");
        return list;
    }

    @GetMapping("/listlike")
    @ResponseBody
    public List listlike(Block block) {
        List<Block> list = blockMapper.selectList(new QueryWrapper<>(block));
        return list;
    }
}
package com.example.demo.domain;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;


@TableName(value = "TEST_BLOCK_T")
public class Block {
    private static final long serialVersionUID = 1L;

    @TableId
    private String blockId;

    private String blockName;

    @TableField(condition = SqlCondition.LIKE)
    private String testName;

    public String getBlockId() {
        return blockId;
    }

    public void setBlockId(String blockId) {
        this.blockId = blockId;
    }

    public String getBlockName() {
        return blockName;
    }

    public void setBlockName(String blockName) {
        this.blockName = blockName;
    }

    public String getTestName() {
        return testName;
    }

    public void setTestName(String testName) {
        this.testName = testName;
    }

    @Override
    public String toString() {
        return "Block{" +
                "blockId='" + blockId + '\'' +
                ", blockName='" + blockName + '\'' +
                ", testName='" + testName + '\'' +
                '}';
    }
}
package com.example.demo.domain;

/**
 * 由于oracle的CONCAT函数不支持拼接2个以上的参数,因此产生了错误。因此自己定义一个注解类
 */
public class SqlCondition {
    /**
     * mybatis原生sqlCondition是面向mysql的,在使用字段注解like时
     * 用一个CONCAT函数拼接三个参数,会报错,因此复写。
     */
    public  static  final  String LIKE = "%s LIKE CONCAT(CONCAT('%%',#{%s}),'%%')";
}
package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.domain.Block;



public interface BlockMapper extends BaseMapper<Block> {

}
package com.example.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.domain.Block;
import com.example.demo.mapper.BlockMapper;
import com.example.demo.service.IBlockTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类。
 * </p>
 *
 * @author yc
 * @since 2021-09-10
 */
@Service
public class BlockTServiceImpl implements IBlockTService {



    @Autowired
    BlockMapper blockMapper;


    @Override
    public Map selectList(Block block,Map likeMap) {
        Map<String, List> reusltMap = new HashMap<>();
        QueryWrapper<Block> queryWrapper = new QueryWrapper<>(block);

        //这里要把需要模糊查询的属性设置为null
        block.setBlockName(null);

        Iterator entries = likeMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            String key = (String)entry.getKey();
            String value = (String)entry.getValue();
            System.out.println("Key = " + key + ", Value = " + value);
            queryWrapper.like(key,value);
        }

        reusltMap.put("dataList", blockMapper.selectList(queryWrapper));
        return reusltMap;
    }


}
package com.example.demo.service;


import com.example.demo.domain.Block;


import java.util.Map;

/**
 * <p>
 * 服务类。
 * </p>
 *
 * @author yc
 * @since 2021-09-10
 */
public interface IBlockTService {

    Map selectList(Block block,Map likeMap);


}
package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

6、测试文件下载

(1)利用QueryWrapper.like自己实现

注:需要模糊查询的字段直接写在controller里了,自己看源码

 

 

 

后台生成的语句如下:

 

 

 

(2)使用@TableField(condition = SqlCondition.LIKE)实现

 

 

 

后台生成的语句如下:

 

 源码获取方式(免费):
(1)登录-注册:http://resources.kittytiger.cn/
(2)签到获取积分
(3)搜索:it白话-模糊查询mybatisPlusLike

 

资源丰富的的网盘资源:网盘资源大全! 推荐一个适合零基础学习SQL的网站:不用安装数据库,在线轻松学习SQL!
posted @ 2022-07-26 16:18  万笑佛  阅读(4497)  评论(0编辑  收藏  举报