SpringBoot+vue实现按照类型查询

(一):搭建前后端交互(解决跨域)

后端:

第一步:导入跨域的依赖


    com.alibaba
    fastjson
    1.2.47

第二步:创建跨域类:

package com.kingback.wangxuedi.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {
    //当前跨越请求最大有效时长,默认为一天
    private static final long MAX_AGE = 24*60*60;
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
        corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
        corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
        corsConfiguration.setMaxAge(MAX_AGE);
        source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
        return new CorsFilter(source);
    }
}

第三步:创建实体类

package com.kingback.wangxuedi.entity;

import lombok.Data;

@Data
public class Scenic {
    private int id;
    private String content;
    private String name;
    private String images;
    private String leixing;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImages() {
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }

    public String getLeixing() {
        return leixing;
    }

    public void setLeixing(String leixing) {
        this.leixing = leixing;
    }

    @Override
    public String toString() {
        return "Scenic{" +
                "id=" + id +
                ", content='" + content + '\'' +
                ", name='" + name + '\'' +
                ", images='" + images + '\'' +
                ", leixing='" + leixing + '\'' +
                '}';
    }
}

第四步:创建mapper接口:

package com.kingback.wangxuedi.mapper;

import com.kingback.wangxuedi.entity.Scenic;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ScenicMapper {
    //查询所有的景点
    Scenic findByLeixing(String leixing);//通过类型对页面数据进行展示
}

第五步:实现接口方法




    

第六步:编写Controller层

package com.kingback.wangxuedi.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingback.wangxuedi.common.Result;
import com.kingback.wangxuedi.entity.Scenic;
import com.kingback.wangxuedi.mapper.ScenicMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController

public class ScenicController {
    @Resource
    private ScenicMapper scenicMapper;
    @GetMapping("/scenic")
    public Result findByName(@RequestParam String leixing){
        return Result.success(scenicMapper.findByLeixing(leixing));
    }
}

前端:Vue层

第一步:按照axios

npm i axios -S

第二步:引入axios并且编写跨域请求

import axios from 'axios'
import ElementUI from 'element-ui';
const request = axios.create({
	baseURL: 'http://localhost:9000',  // 注意!! 这里是全局统一加上了 '/api' 前缀,也就是说所有接口都会加上'/api'前缀在,页面里面写接口的时候就不要加 '/api'了,否则会出现2个'/api',类似 '/api/api/user'这样的报错,切记!!!
    timeout: 5000
})

// request 拦截器
// 可以自请求发送前对请求做一些处理
// 比如统一加token,对请求参数统一加密
request.interceptors.request.use(config => {
    config.headers['Content-Type'] = 'application/json;charset=utf-8';

    let user = localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")):null
    
    // console.log("token为",user.token)
    if(user){
        config.headers['token'] = user.token;  // 设置请求头
    }
    return config
}, error => {
    return Promise.reject(error)
});

// response 拦截器
// 可以在接口响应后统一处理结果
request.interceptors.response.use(
    response => {
        let res = response.data;
        // 如果是返回的文件
        if (response.config.responseType === 'blob') {
            return res
        }
        // 兼容服务端返回的字符串数据
        if (typeof res === 'string') {
            res = res ? JSON.parse(res) : res
        }
        //当权限验证不通过时,给出提示
        if(res.code == '401'){
            ElementUI.Message({
                message:res.msg,
                type:'error'
            })
        }
        return res;
    },
    error => {
        console.log('err' + error) // for debug
        return Promise.reject(error)
    }
)


export default request

第三步:在main.js中引入封装好的axios

import request from '@/utils/request';
Vue.prototype.request = request;
Vue.config.productionTip = false

第四步:实际使用:进行数据请求

 methods: {
            load(){
              this.request.get("/scenic",{
                params:{
                  leixing:this.leixing
                }
              }).then(res =>{
                console.log(res);
              })
            }
        }

你可能感兴趣的:(spring,boot,vue.js,java)