当前位置:首页 > VUE

vue和springboot实现搜索

2026-01-20 21:53:18VUE

Vue 和 Spring Boot 实现搜索功能

前端实现(Vue)

安装必要的依赖(如 axios 用于 HTTP 请求):

npm install axios

创建搜索组件(Search.vue):

<template>
  <div>
    <input v-model="searchQuery" @input="handleSearch" placeholder="搜索...">
    <ul>
      <li v-for="item in searchResults" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      searchQuery: '',
      searchResults: []
    };
  },
  methods: {
    handleSearch() {
      if (this.searchQuery.trim() === '') {
        this.searchResults = [];
        return;
      }
      axios.get(`http://localhost:8080/api/search?q=${this.searchQuery}`)
        .then(response => {
          this.searchResults = response.data;
        })
        .catch(error => {
          console.error('搜索出错:', error);
        });
    }
  }
};
</script>

后端实现(Spring Boot)

创建实体类(Item.java):

@Entity
public class Item {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    // 其他字段和getter/setter
}

创建仓库接口(ItemRepository.java):

public interface ItemRepository extends JpaRepository<Item, Long> {
    @Query("SELECT i FROM Item i WHERE i.name LIKE %:query%")
    List<Item> searchByName(@Param("query") String query);
}

创建控制器(SearchController.java):

@RestController
@RequestMapping("/api")
public class SearchController {
    @Autowired
    private ItemRepository itemRepository;

    @GetMapping("/search")
    public List<Item> search(@RequestParam String q) {
        return itemRepository.searchByName(q);
    }
}

跨域处理

在 Spring Boot 应用中添加配置类(WebConfig.java):

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/")
                .allowedOrigins("http://localhost:8081") // Vue 应用地址
                .allowedMethods("*");
    }
}

性能优化

对于大量数据搜索,考虑以下优化方案:

  • 添加数据库索引:@Index(name = "idx_name", columnList = "name")
  • 实现分页查询:在 Repository 方法中添加 Pageable 参数
  • 使用缓存:@Cacheable("searchResults")

前端防抖处理

优化 Vue 组件,减少频繁请求:

vue和springboot实现搜索

<script>
import _ from 'lodash';

export default {
  // ...
  created() {
    this.debouncedSearch = _.debounce(this.handleSearch, 300);
  },
  methods: {
    handleSearch() {
      // 原有逻辑
    }
  },
  watch: {
    searchQuery(newVal) {
      this.debouncedSearch();
    }
  }
};
</script>

标签: vuespringboot
分享给朋友:

相关文章

vue 实现pc

vue 实现pc

Vue 实现 PC 端应用的核心要点 Vue.js 是一个渐进式框架,适合构建 PC 端单页应用(SPA)或复杂的前端界面。以下是实现 PC 端应用的关键方法和注意事项: 项目初始化与工程化配置 使…

vue 实现豆瓣

vue 实现豆瓣

以下是基于 Vue 实现豆瓣电影类功能的实现方案,涵盖核心模块和技术要点: 数据获取与 API 调用 使用豆瓣开放 API(需注意调用频率限制)或第三方代理接口 推荐 axios 进行异步请求,配合…

vue for实现

vue for实现

Vue 中的 v-for 指令 v-for 是 Vue.js 中用于渲染列表数据的核心指令,通过遍历数组或对象生成动态内容。 基本语法(数组) <ul> <li v-for="…

vue实现循环

vue实现循环

Vue 实现循环的方法 在 Vue 中,可以通过 v-for 指令实现循环渲染列表或对象。以下是几种常见的用法: 遍历数组 使用 v-for 遍历数组时,可以同时获取当前项和索引: <ul&…

vue实现页面

vue实现页面

Vue 实现页面的基本方法 创建 Vue 项目 使用 Vue CLI 快速初始化项目,运行以下命令安装并创建项目: npm install -g @vue/cli vue create my-pro…

vue实现滚动显示

vue实现滚动显示

Vue 实现滚动显示的几种方法 在 Vue 中实现滚动显示可以通过多种方式实现,以下是几种常见的方法: 使用 CSS 动画 通过 CSS 的 animation 属性结合 @keyframes 实现…