springcloud外賣訂餐系統(tǒng)-day2

springcloud外賣訂餐系統(tǒng)-day2

本次要整合mybatis,需要用到數(shù)據(jù)庫燥翅,數(shù)據(jù)庫的安裝就不再此處講述了粟按,默認已安裝培愁,

直接獻上sql文件:

鏈接: https://pan.baidu.com/s/1pyQ_zIa-_PoVPKqaIy_RDA 提取碼: 53cg 復(fù)制這段內(nèi)容后打開百度網(wǎng)盤手機App盖腿,操作更方便哦

1诫隅、創(chuàng)建模塊(服務(wù)提供者)menu

創(chuàng)建服務(wù)提供者menu:

1钉凌、pom.xml

properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--到配置中心進行配置文件的讀取-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
    <!--注冊到注冊中心成為服務(wù)提供者-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!--mybatis-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>1.1.5</version>
    </dependency>
    <!--<dependency>-->
        <!--<groupId>com.baomidou</groupId>-->
        <!--<artifactId>mybatis-plus-boot-starter</artifactId>-->
        <!--<version>3.1.2</version>-->
    <!--</dependency>-->

    <!--mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
        <!--<version>5.6.0</version>-->
    </dependency>

    <!---->

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2咧最、創(chuàng)建bootstrap.yml

spring:
  application:
    name: menu
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8762
      fail-fast: true

這里application.name指的是服務(wù)名

profiles.active指的是應(yīng)用哪個環(huán)境,一般有生產(chǎn)環(huán)境御雕,開發(fā)環(huán)境dev,測試環(huán)境等等

配置好了就可以訪問我們第一天創(chuàng)建的在classpath: shared文件夾下的menu-dev.yml了

在配置中心的shared文件夾下創(chuàng)建menu-dev.yml

server:
  port: 8020

spring:
  datasource:
    username: root
    password: 123456
#    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:13306/orderingsystem?serverTimezone=UTC

創(chuàng)建實體類Menu.java:

package top.juntech.menu.entity;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Menu {
    private long id;
    private String name;
    private double price;
    private String flavor;

}

創(chuàng)建數(shù)據(jù)訪問層repository:

package top.juntech.menu.repository;

import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import top.juntech.menu.entity.Menu;

@Repository
public interface MenuRepository{

    @Select("select * from t_menu where id = #{id}")
    public Menu findById(@PathVariable("id")long id);

}

創(chuàng)建控制器MenuController.java:

package top.juntech.menu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import top.juntech.menu.entity.Menu;
import top.juntech.menu.repository.MenuRepository;


@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuRepository menuRepository;

    @Value("${server.port}")
    private String port;

    @GetMapping("/index")
    public String index(){
        return "menu服務(wù)提供者的端口為:"+this.port;
    }

    @GetMapping("/findbyid/{id}")
    public Menu findById(@PathVariable("id")long id){
        return menuRepository.findById(id);
    }
}

創(chuàng)建啟動類:

package top.juntech.menu;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@MapperScan("top.juntech.menu.repository")
@EnableDiscoveryClient
public class MenuApplication {

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

}

按照Eurekaserver->eurekaserverconfig->服務(wù)提供者的順序依次執(zhí)行矢沿,打開localhost:8761,就會發(fā)現(xiàn)有2個服務(wù)提供者創(chuàng)建了:

register with eureka

Application AMIs Availability Zones Status
MENU n/a (1) (1) UP (1) - DESKTOP-ODMEB8A:menu:8020
ORDER n/a (1) (1) UP (1) - DESKTOP-ODMEB8A:order:8010

打開http://localhost:8020/menu/findbyid/1

顯示:{"id": 1,"name": "香酥雞","price": 39,"flavor": "五香"}酸纲,則成功了

打開http://localhost:8020/menu/findall/2/3

[{"id": 3,"name": "栗子三杯雞","price": 56,"flavor": "五香"},{"id": 4,"name": "毛血旺","price": 50,"flavor": "麻辣"},{"id": 5,"name": "菠菜拌粉絲","price": 22,"flavor": "五香"}]

打開http://localhost:8020/menu/findall/0/10

[{"id": 1,"name": "香酥雞","price": 39,"flavor": "五香"},{"id": 2,"name": "燒椒扣肉","price": 46,"flavor": "微辣"},{"id": 3,"name": "栗子三杯雞","price": 56,"flavor": "五香"},{"id": 4,"name": "毛血旺","price": 50,"flavor": "麻辣"},{"id": 5,"name": "菠菜拌粉絲","price": 22,"flavor": "五香"},{"id": 6,"name": "涼拌豆腐皮","price": 19,"flavor": "微辣"},{"id": 7,"name": "醬牛肉","price": 36,"flavor": "麻辣"},{"id": 8,"name": "魚頭豆腐湯","price": 32,"flavor": "五香"},{"id": 9,"name": "瘦肉雞蛋白菜湯","price": 30,"flavor": "五香"},{"id": 10,"name": "西葫蘆蝦仁蒸餃","price": 26,"flavor": "五香"}

若發(fā)現(xiàn)在運行過程中有數(shù)據(jù)庫方面的錯誤捣鲸,請參考本站的:mabtis整合踩坑記錄 這篇文章!

2闽坡、服務(wù)消費者整合menu

創(chuàng)建module client

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>top.juntech</groupId>
    <artifactId>client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

bootstrap.yml

spring:
  application:
    name: client
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8762
      fail-fast: true

client-dev.yml

server:
  port: 8030

spring:
  application:
    name: client
  thymeleaf:
    prefix: classpath:/static/
    suffix: .html
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    instance:
      prefer-ip-address: true

把entity里的menu復(fù)制到client里面

創(chuàng)建feign-------》MenuFeign.java

package top.juntech.client.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import top.juntech.client.entity.Menu;

import java.util.List;

@FeignClient(value = "menu")
public interface MenuFeign {

    //獲取menu的接口
    @GetMapping("/menu/findall/{index}/{limit}")
    public List<Menu> findall(@PathVariable("index") int index,@PathVariable("limit") int limit);

}

?

創(chuàng)建ClientController.java

package top.juntech.client.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.juntech.client.entity.Menu;
import top.juntech.client.feign.MenuFeign;

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

@RestController
@RequestMapping("/client")
public class ClientController {

    @Resource
    private MenuFeign menuFeign;
//測試是否可以調(diào)用menu的接口
    @GetMapping("/client/{index}/{limit}")
    public List<Menu> findall(@PathVariable("index") int index, @PathVariable("limit") int limit){
        return menuFeign.findall(index,limit);
    }
}

打開http://localhost:8030/client/client/0/10

[{"id": 1,"name": "香酥雞","price": 39,"flavor": "五香"},{"id": 2,"name": "燒椒扣肉","price": 46,"flavor": "微辣"},{"id": 3,"name": "栗子三杯雞","price": 56,"flavor": "五香"},{"id": 4,"name": "毛血旺","price": 50,"flavor": "麻辣"},{"id": 5,"name": "菠菜拌粉絲","price": 22,"flavor": "五香"},{"id": 6,"name": "涼拌豆腐皮","price": 19,"flavor": "微辣"},{"id": 7,"name": "醬牛肉","price": 36,"flavor": "麻辣"},{"id": 8,"name": "魚頭豆腐湯","price": 32,"flavor": "五香"},{"id": 9,"name": "瘦肉雞蛋白菜湯","price": 30,"flavor": "五香"},{"id": 10,"name": "西葫蘆蝦仁蒸餃","price": 26,"flavor": "五香"}]

則成功栽惶!

3、整合前端框架layui

這里是源代碼:https://github.com/southwind9801/orderingsystem

前往git倉庫下載layui文件疾嗅,放入feign的static文件夾外厂。

由于layui的一些特性,我們需要重新命名一個MenuVO類代承,進行數(shù)據(jù)封裝

MenuVO.java

package top.juntech.menu.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MenuVO implements Serializable {
    private int code;
    private String msg;
    private int count;
    private List<Menu> data;
}

修改MenuController.java

@GetMapping("/findall/{index}/{limit}")
public MenuVO findall(@PathVariable("index") int index,@PathVariable("limit") int limit){
    List<Menu> list = menuRepository.findAll(index,limit);
    return new MenuVO(0,"",menuRepository.count(),list);
}

修改feign:

@GetMapping("/findall")
@ResponseBody
public MenuVO findall(@RequestParam("page") int page, @RequestParam("limit") int limit){
    int index = (page-1)*limit;
    return  menuFeign.findall(index,limit);
}

@GetMapping("/redirect/{location}")
public String redirect(@PathVariable("location") String location ){
    return location;
}

前端代碼不需要做什么修改汁蝶,先打開

http://localhost:8030/client/findall?page=1&limit=10

顯示:

{"code": 0,"msg": "","count": 100,"data": [{"id": 2,"name": "燒椒扣肉","price": 46,"flavor": "微辣"},{"id": 3,"name": "栗子三杯雞","price": 56,"flavor": "五香"},{"id": 4,"name": "毛血旺","price": 50,"flavor": "麻辣"},{"id": 5,"name": "菠菜拌粉絲","price": 22,"flavor": "五香"},{"id": 6,"name": "涼拌豆腐皮","price": 19,"flavor": "微辣"},{"id": 7,"name": "醬牛肉","price": 36,"flavor": "麻辣"},{"id": 8,"name": "魚頭豆腐湯","price": 32,"flavor": "五香"},{"id": 9,"name": "瘦肉雞蛋白菜湯","price": 30,"flavor": "五香"},{"id": 10,"name": "西葫蘆蝦仁蒸餃","price": 26,"flavor": "五香"},{"id": 11,"name": "蛋炒飯","price": 18,"flavor": "五香"}]}

則成功!

新建Type類

package top.juntech.menu.entity;

import lombok.Data;

import java.io.Serializable;

@Data
public class Type implements Serializable {
    private long id;
    private String name;
}

新建TypeRepository

package top.juntech.menu.repository;

import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;
import top.juntech.menu.entity.Type;

@Repository
public interface TypeRepository {

    @Select("select * from t_type where id = #{id}")
    public Type findById(@RequestParam("id")long id);
}

修改menuController

@GetMapping("/findall/{index}/{limit}")
public MenuVO findall(@PathVariable("index") int index,@PathVariable("limit") int limit){
    List<Menu> list = menuRepository.findAll(index,limit);
    Iterator it = list.iterator();
    while (it.hasNext()){
        Menu menu = (Menu)it.next();
        Type type = typeRepository.findById(menu.getId());
        menu.setType(type);
    }
    return new MenuVO(0,"",menuRepository.count(),list);
}

重啟服務(wù)提供者:打開http://localhost:8020/menu/findall/0/10

{"code": 0,"msg": "","count": 12,"data": [{"id": 1,"name": "香酥雞","price": 39,"flavor": "五香","type": {"id": 1,"name": "熱菜"}},{"id": 2,"name": "燒椒扣肉","price": 46,"flavor": "微辣","type": {"id": 2,"name": "涼菜"}},{"id": 3,"name": "栗子三杯雞","price": 56,"flavor": "五香","type": {"id": 3,"name": "湯羹"}},{"id": 4,"name": "毛血旺","price": 50,"flavor": "麻辣","type": {"id": 4,"name": "主食"}},{"id": 5,"name": "菠菜拌粉絲","price": 22,"flavor": "五香","type": {"id": 5,"name": "烘焙"}},{"id": 6,"name": "涼拌豆腐皮","price": 19,"flavor": "微辣","type": null},{"id": 7,"name": "醬牛肉","price": 36,"flavor": "麻辣","type": null},{"id": 8,"name": "魚頭豆腐湯","price": 32,"flavor": "五香","type": null},{"id": 9,"name": "瘦肉雞蛋白菜湯","price": 30,"flavor": "五香","type": null},{"id": 10,"name": "西葫蘆蝦仁蒸餃","price": 26,"flavor": "五香","type": null}]}

把Type,Menu實體類修改后次泽,重啟服務(wù)消費者:打開http://localhost:8030/client/findall?page=1&limit=10

顯示:{"code": 0,"msg": "","count": 12,"data": [{"id": 1,"name": "香酥雞","price": 39,"flavor": "五香","type": {"id": 1,"name": "熱菜"}},{"id": 2,"name": "燒椒扣肉","price": 46,"flavor": "微辣","type": {"id": 2,"name": "涼菜"}},{"id": 3,"name": "栗子三杯雞","price": 56,"flavor": "五香","type": {"id": 3,"name": "湯羹"}},{"id": 4,"name": "毛血旺","price": 50,"flavor": "麻辣","type": {"id": 4,"name": "主食"}},{"id": 5,"name": "菠菜拌粉絲","price": 22,"flavor": "五香","type": {"id": 5,"name": "烘焙"}},{"id": 6,"name": "涼拌豆腐皮","price": 19,"flavor": "微辣","type": null},{"id": 7,"name": "醬牛肉","price": 36,"flavor": "麻辣","type": null},{"id": 8,"name": "魚頭豆腐湯","price": 32,"flavor": "五香","type": null},{"id": 9,"name": "瘦肉雞蛋白菜湯","price": 30,"flavor": "五香","type": null},{"id": 10,"name": "西葫蘆蝦仁蒸餃","price": 26,"flavor": "五香","type": null}]}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末穿仪,一起剝皮案震驚了整個濱河市席爽,隨后出現(xiàn)的幾起案子意荤,更是在濱河造成了極大的恐慌,老刑警劉巖只锻,帶你破解...
    沈念sama閱讀 212,294評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件玖像,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機捐寥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,493評論 3 385
  • 文/潘曉璐 我一進店門笤昨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人握恳,你說我怎么就攤上這事瞒窒。” “怎么了乡洼?”我有些...
    開封第一講書人閱讀 157,790評論 0 348
  • 文/不壞的土叔 我叫張陵崇裁,是天一觀的道長。 經(jīng)常有香客問我束昵,道長拔稳,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,595評論 1 284
  • 正文 為了忘掉前任锹雏,我火速辦了婚禮巴比,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘礁遵。我一直安慰自己轻绞,他們只是感情好,可當我...
    茶點故事閱讀 65,718評論 6 386
  • 文/花漫 我一把揭開白布佣耐。 她就那樣靜靜地躺著铲球,像睡著了一般。 火紅的嫁衣襯著肌膚如雪晰赞。 梳的紋絲不亂的頭發(fā)上稼病,一...
    開封第一講書人閱讀 49,906評論 1 290
  • 那天,我揣著相機與錄音掖鱼,去河邊找鬼然走。 笑死,一個胖子當著我的面吹牛戏挡,可吹牛的內(nèi)容都是我干的芍瑞。 我是一名探鬼主播,決...
    沈念sama閱讀 39,053評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼褐墅,長吁一口氣:“原來是場噩夢啊……” “哼拆檬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起妥凳,我...
    開封第一講書人閱讀 37,797評論 0 268
  • 序言:老撾萬榮一對情侶失蹤竟贯,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后逝钥,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體屑那,經(jīng)...
    沈念sama閱讀 44,250評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,570評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了持际。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片沃琅。...
    茶點故事閱讀 38,711評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蜘欲,靈堂內(nèi)的尸體忽然破棺而出益眉,到底是詐尸還是另有隱情,我是刑警寧澤姥份,帶...
    沈念sama閱讀 34,388評論 4 332
  • 正文 年R本政府宣布呜叫,位于F島的核電站,受9級特大地震影響殿衰,放射性物質(zhì)發(fā)生泄漏朱庆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,018評論 3 316
  • 文/蒙蒙 一闷祥、第九天 我趴在偏房一處隱蔽的房頂上張望娱颊。 院中可真熱鬧,春花似錦凯砍、人聲如沸箱硕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,796評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽剧罩。三九已至,卻和暖如春座泳,著一層夾襖步出監(jiān)牢的瞬間惠昔,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,023評論 1 266
  • 我被黑心中介騙來泰國打工挑势, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留镇防,地道東北人。 一個月前我還...
    沈念sama閱讀 46,461評論 2 360
  • 正文 我出身青樓潮饱,卻偏偏與公主長得像来氧,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子香拉,可洞房花燭夜當晚...
    茶點故事閱讀 43,595評論 2 350