java1234开源博客系统
博客信息

microservice-student-provider-1001服务提供者项目建立

0
发布时间:『 2018-07-31 14:11』  博客类别:SpringCloud  阅读(290)

microservice-student-provider-1001服务提供者项目建立


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


详细步骤 请看 一头扎进springcloud 课程


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.java1234.springcloud</groupId>
    <artifactId>microservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservice-student-provider-1001</artifactId>
  
  <dependencies>
  	<dependency>
  		<groupId>com.java1234.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>


加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<Student, Integer>,JpaSpecificationExecutor<Student>{

}


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<Student> 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<Student> 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<Student> 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表;

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

QQ鎴浘20180731140028.jpg


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


显示结果:

QQ鎴浘20180731140139.jpg


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


显示:

QQ鎴浘20180731140218.jpg


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

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


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

QQ鎴浘20180731140617.jpg

添加测试:


QQ鎴浘20180731140846.jpg

修改测试;


关键字:   springcloud     服务提供者项目  
Powered by Java1234 V3.0 Copyright © 2012-2016 Java知识分享网 版权所有