作用
用于提供服务
新建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>
<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> <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> <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;
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;
public interface StudentService {
void save(Student student);
Student findById(Integer id);
List<Student> list();
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("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;
@RestController @RequestMapping("/student") public class StudentProviderController {
@Resource private StudentService studentService;
@RequestMapping(value = "/save") public boolean save(@RequestBody Student student) { try { studentService.save(student); return true; } catch (Exception e) { return false; } }
@RequestMapping("/list") public List<Student> list() { return studentService.list(); }
@RequestMapping("/get/{id}") public Student get(@PathVariable("id") Integer id) { return studentService.findById(id); }
@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;
@SpringBootApplication @EntityScan("com/ledao/entity") public class StudentProviderApplication_1001 {
public static void main(String[] args) { SpringApplication.run(StudentProviderApplication_1001.class, args); } }
|
结果
运行服务提供者启动类后,在浏览器地址栏输入对应URL可以进行增删查改操作,端口为1001