SpringCloud microservice-student-provider-1001服务提供者项目建立(三)

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

pom.xml改成:


  4.0.0
 
    com.java1234.springcloud
    microservice
    0.0.1-SNAPSHOT
 

  microservice-student-provider-1001
   
 
     
          com.java1234.springcloud
          microservice-common
         ${project.version}
     

     
        org.springframework.boot
        spring-boot-starter-web
   

   
        org.springframework.boot
        spring-boot-starter-data-jpa
   

   
        mysql
        mysql-connector-java
   

   
        org.springframework.boot
        spring-boot-starter-tomcat
   

   
        com.alibaba
        druid
   

   
   
        org.springframework
        springloaded
   

   
        org.springframework.boot
        spring-boot-devtools
   

 


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

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/db_springcloud
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  thymeleaf:
    cache: false


StudentRepository接口:

package com.java1234.repository;
 
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 
import com.java1234.entity.Student;
 
/**
 * 学生Repository接口
 * @author Administrator
 *
 */
public interface StudentRepository extends JpaRepository,JpaSpecificationExecutor{
 
}

StudentService接口:

package com.java1234.service;
 
import java.util.List;
 
import com.java1234.entity.Student;
 
/**
 * 学生信息Service接口
 * @author Administrator
 *
 */
public interface StudentService {
 
    /**
     * 添加或者修改学生信息
     * @param student
     */
    public void save(Student student);
     
    /**
     * 根据id查找学生信息
     * @param id
     * @return
     */
    public Student findById(Integer id);
     
    /**
     * 查询学生信息
     * @return
     */
    public List list();
     
    /**
     * 根据id删除学生信息
     * @param id
     */
    public void delete(Integer id);
     
     
}


StudentServiceImpl实现类:

package com.java1234.service.impl;
 
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.java1234.entity.Student;
import com.java1234.repository.StudentRepository;
import com.java1234.service.StudentService;
 
/**
 * 学生信息Service实现类
 * @author Administrator
 *
 */
@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 list() {
        return studentRepository.findAll();
    }
 
    @Override
    public void delete(Integer id) {
        studentRepository.delete(id);
    }
 
}


StudentProviderController类:

package com.java1234.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.java1234.entity.Student;
import com.java1234.service.StudentService;
 
/**
 * 服务提供者-学生信息控制器
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/student")
public class StudentProviderController {
 
    @Resource
    private StudentService studentService;
     
    /**
     * 添加或者修改学生信息
     * @param student
     * @return
     */
    @PostMapping(value="/save")
    public boolean save(Student student){
        try{
            studentService.save(student);  
            return true;
        }catch(Exception e){
            return false;
        }
    }
     
    /**
     * 查询学生信息
     * @return
     */
    @GetMapping(value="/list")
    public List list(){
        return studentService.list();
    }
     
    /**
     * 根据id查询学生信息
     * @return
     */
    @GetMapping(value="/get/{id}")
    public Student get(@PathVariable("id") Integer id){
        return studentService.findById(id);
    }
     
    /**
     * 根据id删除学生信息
     * @return
     */
    @GetMapping(value="/delete/{id}")
    public boolean delete(@PathVariable("id") Integer id){
        try{
            studentService.delete(id);
            return true;
        }catch(Exception e){
            return false;
        }
    }
}


启动类StudentProviderApplication_1001:

package com.java1234;
 
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);
    }
}


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

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

SpringCloud microservice-student-provider-1001服务提供者项目建立(三)_第1张图片

 

浏览器请求:http://localhost:1001/student/list

显示结果:

SpringCloud microservice-student-provider-1001服务提供者项目建立(三)_第2张图片

 

请求:http://localhost:1001/student/get/1

显示:

 

然后添加修改的话 我们借用下 postman插件;

具体插件包和文件,这里给下地址:http://www.java1234.com/a/javaziliao/kfgj/2018/0730/11625.html

具体postman测试演示,大伙可以结合我的视频,有详细的操作;

SpringCloud microservice-student-provider-1001服务提供者项目建立(三)_第3张图片

添加测试:

SpringCloud microservice-student-provider-1001服务提供者项目建立(三)_第4张图片

修改测试;

你可能感兴趣的:(SpringCloud,Java,java,数学建模,开发语言)