作用

用于提供服务

新建Module

该Module为Maven项目,命名为microservice-student-provider-1001

修改pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?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">
<modelVersion>4.0.0</modelVersion>

<!--继承microservice依赖版本-->
<parent>
<groupId>org.example</groupId>
<artifactId>microservice</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/>
</parent>

<artifactId>microservice-student-provider-1001</artifactId>
<packaging>war</packaging>

<name>microservice-student-provider-1001 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<!--引入microservice-common依赖,引入后可以使用microservice-common的实体类,工具类等-->
<dependency>
<groupId>org.example</groupId>
<artifactId>microservice-common</artifactId>
<version>1.0-SNAPSHOT</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>

application.yml配置

在resources中新建application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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/db_springcloud?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true

学生Repository接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.ledao.repository;

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

/**
* 学生Repository接口
*
* @author LeDao
* @company
* @create 2021-08-11 13:48
*/
public interface StudentRepository extends JpaRepository<Student, Integer>, JpaSpecificationExecutor<Student> {
}

学生信息Service接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.ledao.service;

import com.ledao.entity.Student;

import java.util.List;

/**
* 学生信息Service接口
*
* @author LeDao
* @company
* @create 2021-08-11 13:50
*/
public interface StudentService {

/**
* 添加或者修改学生信息
*
* @param student
*/
void save(Student student);

/**
* 根据id查找学生信息
*
* @param id
* @return
*/
Student findById(Integer id);

/**
* 查询所有学生信息
*
* @return
*/
List<Student> list();

/**
* 根据id删除学生信息
*
* @param id
*/
void delete(Integer id);
}

学生信息Service实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.ledao.service.impl;

import com.ledao.repository.StudentRepository;
import com.ledao.service.StudentService;
import com.ledao.entity.Student;
import org.springframework.stereotype.Service;

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

/**
* 学生信息Service实现类
*
* @author LeDao
* @company
* @create 2021-08-11 13:52
*/
@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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.ledao.controller;

import com.ledao.service.StudentService;
import com.ledao.entity.Student;
import org.springframework.web.bind.annotation.*;

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

/**
* 服务提供者-学生信息控制器
*
* @author LeDao
* @company
* @create 2021-08-11 13:54
*/
@RestController
@RequestMapping("/student")
public class StudentProviderController {

@Resource
private StudentService studentService;

/**
* 添加或者修改学生信息
*
* @param student
* @return
*/
@RequestMapping(value = "/save")
public boolean save(@RequestBody Student student) {
try {
studentService.save(student);
return true;
} catch (Exception e) {
return false;
}
}

/**
* 查询所有学生信息
*
* @return
*/
@RequestMapping("/list")
public List<Student> list() {
return studentService.list();
}

/**
* 根据id查询学生信息
*
* @param id
* @return
*/
@RequestMapping("/get/{id}")
public Student get(@PathVariable("id") Integer id) {
return studentService.findById(id);
}

/**
* 根据id删除学生信息
*
* @param id
* @return
*/
@RequestMapping("/delete/{id}")
public boolean delete(@PathVariable("id") Integer id) {
try {
studentService.delete(id);
return true;
} catch (Exception e) {
return false;
}
}
}

服务提供者启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.ledao;

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

/**
* 服务提供者启动类
*
* @author LeDao
* @company
* @create 2021-08-11 14:01
*/
@SpringBootApplication
@EntityScan("com/ledao/entity")
public class StudentProviderApplication_1001 {

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

结果

运行服务提供者启动类后,在浏览器地址栏输入对应URL可以进行增删查改操作,端口为1001