樂優商城2 —— 商品分類

你能夠訪問 碼雲 - 樂優商城 來獲取關於樂優商城的工程代碼。

你能夠訪問 百度雲 - 樂優優商城 密碼:ppzy 來獲取關於樂優商城的資料。

1、後臺前端工程

 因爲後臺功能複雜,直接使用資料中的leyou-manage-web工程。咱們使用npm管理包,先要安裝nodejs。

npm
start web
start success

2、統一環境

 爲了保證在不一樣環境下都能正常訪問,咱們使用域名訪問項目。

 咱們經過修改host文件來實現對域名的解析,咱們使用資料中的SwitchHosts修改host文件(以管理員身份運行)。

host

 咱們還要解決端口問題,咱們採用資料中反向代理工具Nginx,使得不一樣域名訪問不一樣端口。

 咱們經過conf目錄下的nginx.conf來完成端口代理。

request map

 咱們的網關映射

request map1

 咱們啓動Nginx,而且訪問manage.leyou.com

request map success

3、商品分類查詢

 咱們首先要將資料中leyou.sql導入mysql數據庫中。

mysql-category
request-category

 咱們開始實現後端邏輯,首先咱們須要Category的實體類,咱們將實體類定義在leyou-interface中,記得生產get set方法。

category-emity

 因爲註解使用的是jpa註解,咱們在leyou-interface pom文件中寫入座標。

interface-pom

 因爲咱們知道那四個請求值,咱們先寫controller 。

category-controller

 咱們開始寫service ,通用mapper封裝的api能夠簡化數據庫操做 。

category-service

 咱們開始寫通用mapper,首先在啓動類上加 @MapperScan("com.leyou.item.mapper") 這樣它就會掃描mapper包。

 咱們新建 mapper包 和 CategoryMapper

categoryMapper

 到目前爲止咱們已經完成整個商品分類的查詢,但存在一個跨域問題。

 凡是請求的域名、協議、端口發生改變咱們稱爲跨域。好比咱們如今manage.leyou.com 訪問 api.leyou.com

 咱們採用CORS這種方式解決跨域問題,經過設置響應頭容許跨域,咱們在leyou-gateway中添加配置類。

cors

pom文件展開查看
package com.leyou.cateway.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;

/**
 * TODO
 *
 * @author Gary
 * @date 2020/1/18 10:07
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        //1.添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //1) 容許的域,不要寫*,不然cookie就沒法使用了
        config.addAllowedOrigin("http://manage.leyou.com");
        //2) 是否發送Cookie信息
        config.setAllowCredentials(true);
        //3) 容許的請求方式
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        // 4)容許的頭信息
        config.addAllowedHeader("*");

        //2.添加映射路徑,咱們攔截一切請求
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);

        //3.返回新的CorsFilter.
        return new CorsFilter(configSource);
    }
}

 重啓服務,咱們訪問一下商品分類,增刪改功能咱們再也不闡述,具體實現能夠訪問git倉庫。

category-success

相關文章
相關標籤/搜索