Spring boot实现模糊查询

使用注解方式

记得查询时要加通配符来表示个数等信息!
参考博文,SQL模糊查询语句


SearchController.java

package cn.shop.controller;

import cn.shop.entity.Shoe;
import cn.shop.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "search")
public class SearchController {

    private Logger logger = LoggerFactory.getLogger(SearchController.class);

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "")
    @ResponseBody
    public List<Shoe> showSearch(@RequestParam(value = "key") String key ,
                             Model model){
        logger.info("key:" + key);
        List<Shoe> list = productService.searchShoes(key);
        logger.info("查询结果个数:" + list.size());
        return list;
    }

}

ProductService.java

package cn.shop.service;

import cn.shop.entity.Shoe;
import cn.shop.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    public List<Shoe> searchShoes(String key){
        //记得加 %  %
        return productRepository.findAllByNameLike("%" + key + "%");
    }
}

ProductRepository.java

package cn.shop.repository;

import cn.shop.entity.Shoe;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ProductRepository extends JpaRepository<Shoe,Integer>{

    public List<Shoe> findAllByNameLike(String key);
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现模糊查询,需要在后端使用Spring Boot框架进行数据查询并返回符合条件的结果,同时在前端使用Vue框架进行数据展示。 在后端,可以使用Spring Data JPA提供的方法实现模糊查询。假设我们有一个实体类为User,其中有一个属性为name,需要根据name进行模糊查询。可以在Repository中定义一个方法,如下所示: ```java public interface UserRepository extends JpaRepository<User, Long> { List<User> findByNameContaining(String name); } ``` 在Controller中调用该方法,并将查询结果返回: ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/search") public List<User> search(@RequestParam("name") String name) { return userRepository.findByNameContaining(name); } } ``` 在前端,可以使用Vue框架进行数据展示。假设我们有一个userList数组,需要根据输入框中的关键字进行模糊查询,并展示符合条件的结果。可以使用计算属性computed来实现: ```html <template> <div> <input type="text" v-model="keyword" placeholder="请输入关键字"> <ul> <li v-for="user in filteredUsers" :key="user.id">{{ user.name }}</li> </ul> </div> </template> <script> export default { data() { return { userList: [], keyword: '' } }, computed: { filteredUsers() { return this.userList.filter(user => user.name.includes(this.keyword)) } } } </script> ``` 以上代码仅供参考,具体实现方式可以根据需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值