SpringCloud-基础项目构建

4.构建springcloud基础项目结构

    构建完成后的项目结构如图:

 

4.1父项目建立

开发项目,现在基本都用到maven,以及用父子项目,以及公共模块依赖,来构建方便扩展的项目体系;

首先我们建立父项目 wfd360-station,主要是一个pom,管理module,以及管理依赖,规范所有jar包版本等;

我们Springcloud版本 用 Edgware.SR4

Springboot版本 用 1.5.13.RELEASE

修改pom.xml为:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.wfd360.station</groupId>
 8     <artifactId>wfd360-station</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10     <modules>
11         <module>common</module>
12         <module>ticket-provider-6001</module>
13         <module>ticket-consumer-80</module>
14     </modules>
15     <packaging>pom</packaging>
16     <properties>
17         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18         <maven.compiler.source>1.7</maven.compiler.source>
19         <maven.compiler.target>1.7</maven.compiler.target>
20         <druid.version>1.1.10</druid.version>
21     </properties>
22 
23     <dependencyManagement>
24         <dependencies>
25             <dependency>
26                 <groupId>org.springframework.cloud</groupId>
27                 <artifactId>spring-cloud-dependencies</artifactId>
28                 <version>Edgware.SR4</version>
29                 <type>pom</type>
30                 <scope>import</scope>
31             </dependency>
32             <dependency>
33                 <groupId>org.springframework.boot</groupId>
34                 <artifactId>spring-boot-dependencies</artifactId>
35                 <version>1.5.13.RELEASE</version>
36                 <type>pom</type>
37                 <scope>import</scope>
38             </dependency>
39             <!-- 连接池 -->
40             <dependency>
41                 <groupId>com.alibaba</groupId>
42                 <artifactId>druid</artifactId>
43                 <version>${druid.version}</version>
44             </dependency>
45         </dependencies>
46     </dependencyManagement>
47 
48 </project>
pom.xml 

 这里引入了 springcloud 以及springboot  包括 druid连接池  以及 属性 规范编译 目标版本,包括连接池版本 编码等信息;

 4.2公共模块搭建

 新建module公共模块项目 common,主要是放一些其他项目公用的东西,比如实体类,工具类等等;

 pom.xml 加下 jpa和 mysql驱动包依赖

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>wfd360-station</artifactId>
 7         <groupId>com.wfd360.station</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>common</artifactId>
13     <packaging>jar</packaging>
14 
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.boot</groupId>
18             <artifactId>spring-boot-starter-data-jpa</artifactId>
19         </dependency>
20         <dependency>
21             <groupId>mysql</groupId>
22             <artifactId>mysql-connector-java</artifactId>
23         </dependency>
24     </dependencies>
25 
26 </project>
View Code

我们新建Ticket实体

package com.wfd360.model;

import javax.persistence.*;
import java.io.Serializable;
/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 07/12 9:13
 * @description
 */
@Entity
@Table(name="t_ticket")
public class Ticket implements Serializable {
    /**
     *序列号b vmvh
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private Integer id;
    /**
     * 出发车站
     */
    @Column
    private String startStation;
    /**
     * 到达车站
     */
    @Column
    private String stopStation;
    /**
     * 票价
     */
    @Column
    private Integer price;

    public Integer getId() {
        return id;
    }

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

    public String getStartStation() {
        return startStation;
    }

    public void setStartStation(String startStation) {
        this.startStation = startStation;
    }

    public String getStopStation() {
        return stopStation;
    }

    public void setStopStation(String stopStation) {
        this.stopStation = stopStation;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }
}
View Code

包括映射

当然我们数据库里也提前建一个db_station数据库;

4.3ticket-provider-6001服务提供者项目建立

 我们新建一个服务器提供者module子模块,类似前面建的common公共模块,名称是 ticket-provider-6001

 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>wfd360-station</artifactId>
        <groupId>com.wfd360.station</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ticket-provider-6001</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.wfd360.station</groupId>
            <artifactId>common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>
View Code

 该pom.xml中加common项目依赖,以及 springboot的 web jpa  驱动包 tomcat 连接池 配置 包括自动热部署配置。

application.yml配置:

 1 server:
 2   port: 6001
 3   context-path: /
 4 
 5 # 数据源配置
 6 spring:
 7   datasource:
 8     type: com.alibaba.druid.pool.DruidDataSource
 9     driver-class-name: com.mysql.jdbc.Driver
10     url: jdbc:mysql://localhost:3306/db_station
11     username: root
12     password: admin
13   jpa:
14     hibernate:
15       ddl-auto: update
16     show-sql: true
17   thymeleaf:
18     cache: false

 注意:空格

TicketRepository接口:
package com.wfd360.repository;

import com.wfd360.model.Ticket;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

/**
 * Created by 姿势帝-博客园 on 2020/6/25.
 * 欢迎添加笔者wx(851298348)共同探讨、学习!
 */
@Repository
public interface TicketRepository  extends
        JpaRepository<Ticket, Integer>,JpaSpecificationExecutor<Ticket> {
}
View Code

TicketService接口:

 1 package com.wfd360.service;
 2 
 3 import com.wfd360.model.Ticket;
 4 
 5 import java.util.List;
 6 
 7 /**
 8  * Created by 姿势帝-博客园 on 2020/6/25.
 9  * 欢迎添加笔者wx(851298348)共同探讨、学习!
10  */
11 
12 public interface TicketService {
13     /**
14      * 添加或者修改车票信息
15      *
16      * @param ticket
17      */
18     void save(Ticket ticket);
19 
20     /**
21      * 根据id查找车票信息
22      *
23      * @param id
24      * @return
25      */
26     Ticket findById(Integer id);
27 
28     /**
29      * 查询车票信息
30      *
31      * @return
32      */
33     List<Ticket> list();
34 
35     /**
36      * 根据id删除车票信息
37      *
38      * @param id
39      */
40     void delete(Integer id);
41 }
View Code

TicketServiceImpl实现类:

 1 package com.wfd360.service.impl;
 2 
 3 import com.wfd360.model.Ticket;
 4 import com.wfd360.repository.TicketRepository;
 5 import com.wfd360.service.TicketService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 
 9 import java.util.List;
10 
11 /**
12  * Created by 姿势帝-博客园 on 2020/6/25.
13  * 欢迎添加笔者wx(851298348)共同探讨、学习!
14  */
15 @Service
16 public class TicketServiceImpl implements TicketService {
17     @Autowired
18     private TicketRepository ticketRepository;
19 
20     @Override
21     public void save(Ticket ticket) {
22         ticketRepository.save(ticket);
23     }
24 
25     @Override
26     public Ticket findById(Integer id) {
27         return ticketRepository.findOne(id);
28     }
29 
30     @Override
31     public List<Ticket> list() {
32         return ticketRepository.findAll();
33     }
34 
35     @Override
36     public void delete(Integer id) {
37         ticketRepository.delete(id);
38     }
39 }
View Code

 TicketProviderController类:

package com.wfd360.controller;

import com.wfd360.model.Ticket;
import com.wfd360.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 姿势帝-博客园 on 2020/3/25.
 * 欢迎添加笔者wx(851298348)共同探讨、学习!
 */
@Controller
@RequestMapping("/ticket")
public class TicketController {
    @Autowired
    private TicketService ticketService;
    /**
     * 添加或者修改车票信息
     * @param ticket
     * @return
     */
    @PostMapping(value="/save")
    @ResponseBody
    public boolean save(Ticket ticket){
        try{
            ticketService.save(ticket);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    /**
     * 查询车票信息
     * @return
     */
    @GetMapping(value="/list")
    @ResponseBody
    public List<Ticket> list(){
        List<Ticket> list = ticketService.list();
        Ticket ticket = new Ticket();
        ticket.setStartStation("1001服务提供(代码添加)");
        list.add(ticket);
        return list;
    }

    /**
     * 根据id查询车票信息
     * @return
     */
    @GetMapping(value="/get/{id}")
    @ResponseBody
    public Ticket get(@PathVariable("id") Integer id){
        return ticketService.findById(id);
    }

    /**
     * 根据id删除车票信息
     * @return
     */
    @GetMapping(value="/delete/{id}")
    @ResponseBody
    public boolean delete(@PathVariable("id") Integer id){
        try{
            ticketService.delete(id);
            return true;
        }catch(Exception e){
            return false;
        }
    }
}
View Code

启动类TicketProviderApplication_6001:

 1 package com.wfd360;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 /**
 7  * Created by 姿势帝-博客园 on 2020/6/25.
 8  * 欢迎添加笔者wx(851298348)共同探讨、学习!
 9  */
10 @SpringBootApplication
11 public class TicketProviderApplication_6001 {
12     /**
13      * @param args
14      */
15     public static void main(String[] args) {
16         SpringApplication.run(TicketProviderApplication_6001.class, args);
17     }
18 }
View Code

我们运行启动类,自动生成t_ticket表;

我们加点数据,方便测试:

 

浏览器请求:http://localhost:6001/ticket/list

 

 然后添加修改的话 我们借用下 postman插件(请自行测试);

springCloud课程与代码下载:https://www.cnblogs.com/newAndHui/p/13210228.html

posted @ 2019-03-28 09:28  李东平|一线码农  阅读(954)  评论(0编辑  收藏  举报