`

服务提供者microservice-student-provider-1001

阅读更多
1. 服务提供者项目microservice-student-provider-1001搭建

新建服务提供者项目microservice-student-provider-1001
new -> Maven Module
    -> create a simple project
Module Name:microservice-student-provider-1001
Parent Project:microservice
Working set:SpringCloud
    -> Artifact
Group Id:com.andrew.springcloud
Artifact Id:microservice-student-provider-1001
Version:0.0.1-SNAPSHOT
Packaging:jar


1.1) 服务提供者项目microservice-student-provider-1001增加pom.xml依赖

<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.andrew.springcloud</groupId>
        <artifactId>microservice</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-student-provider-1001</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.andrew.springcloud</groupId>
            <artifactId>microservice-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>


1.2) 服务提供者项目microservice-student-provider-1001增加application.yml配置

server:
  port: 1001
  context-path: /
# 数据源配置
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springcloud
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  thymeleaf:
    cache: false


1.3) 服务提供者项目microservice-student-provider-1001增加com.andrew.StudentProviderApplication_1001.java启动类

package com.andrew;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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


1.4) 服务提供者项目microservice-student-provider-1001增加com.andrew.repository.StudentRepository.java

package com.andrew.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.andrew.entity.Student;

public interface StudentRepository extends JpaRepository<Student, Integer>,JpaSpecificationExecutor<Student> {

}


1.5) 服务提供者项目microservice-student-provider-1001增加com.andrew.service.StudentService.java

package com.andrew.service;

import java.util.List;
import com.andrew.entity.Student;

public interface StudentService {
    public void save(Student student);
    public Student findById(Integer id);
    public List<Student> list();
    public void delete(Integer id);
}


1.6) 服务提供者项目microservice-student-provider-1001增加com.andrew.service.impl.StudentServiceImpl.java

package com.andrew.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.andrew.entity.Student;
import com.andrew.repository.StudentRepository;
import com.andrew.service.StudentService;

@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentRepository studentRepository;
    @Override
    public void save(Student student) {
        studentRepository.save(student);
    }
    @Override
    public Student findById(Integer id) {
        return studentRepository.findOne(id);
    }
    @Override
    public List<Student> list() {
        return studentRepository.findAll();
    }
    @Override
    public void delete(Integer id) {
        studentRepository.delete(id);
    }
}


1.7) 服务提供者项目microservice-student-provider-1001增加com.andrew.controller.StudentProviderController.java

package com.andrew.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.andrew.entity.Student;
import com.andrew.service.StudentService;

@RestController
@RequestMapping("/student")
public class StudentProviderController {
    @Resource
    private StudentService studentService;
    @PostMapping(value = "/save")
    public boolean save(Student student) {
        try {
            studentService.save(student);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    @GetMapping(value = "/list")
    public List<Student> list() {
        return studentService.list();
    }
    @GetMapping(value = "/get/{id}")
    public Student get(@PathVariable("id") Integer id) {
        return studentService.findById(id);
    }
    @GetMapping(value = "/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id) {
        try {
            studentService.delete(id);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}


chrome安装postman插件

postman下载地址:https://pan.baidu.com/share/init?surl=io52U6OD6Rb7CWIrjAn3Pw
postman下载密码:oul9

插入测试数据
insert into t_student values(1, "11", "zhangsan");
insert into t_student values(2, "12", "lisi");
insert into t_student values(3, "13", "wangwu");


启动microservice-student-provider-1001

http://localhost:1001/student/list
[{"id":1,"name":"zhangsan","grade":"11"},{"id":2,"name":"lisi","grade":"12"},{"id":3,"name":"wangwu","grade":"13"}]

http://localhost:1001/student/get/1
{"id":1,"name":"zhangsan","grade":"11"}

post http://localhost:1001/student/save
id    4
grade    14
name    student04
true
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics