SpringCloud(第 001 篇)简单用户微服务


声明:本文转载自https://my.oschina.net/u/162754/blog/1539498,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

插播一条广告→2021 ByteDance字节跳动内推←各城市、各方向的岗位都有,大量招人!


SpringCloud(第 001 篇)简单用户微服务

一、大致介绍

通过 RestAPI 接口 /simple/{id} 来简单获取 H2 数据库中的用户信息,并且数据库中的字段与实体 User 类的字段相互映射 。 

二、实现步骤

2.1 添加 maven 引用包

<?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>  	<artifactId>springms-simple-provider-user</artifactId>     <version>1.0-SNAPSHOT</version>     <packaging>jar</packaging> 	     <parent>         <groupId>com.springms.cloud</groupId>         <artifactId>springms-spring-cloud</artifactId>         <version>1.0-SNAPSHOT</version>     </parent> 	 	<dependencies>         <!-- 访问数据库模块 -->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-data-jpa</artifactId>         </dependency>          <!-- web模块 -->         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>          <!-- h2数据库模块 -->         <dependency>             <groupId>com.h2database</groupId>             <artifactId>h2</artifactId>             <scope>runtime</scope>         </dependency>          <!-- 支持Apache Solr搜索平台,包括spring-data-solr --> 		<dependency> 			<groupId>org.springframework.boot</groupId> 			<artifactId>spring-boot-starter-data-solr</artifactId> 		</dependency>     </dependencies>  </project> 

2.2 添加应用配置文件(springms-simple-provider-user\src\main\resources\application.yml)

server:   port: 8000 spring:   application:     name: springms-simple-provider-user  #全部小写   jpa:     generate-ddl: false     show-sql: true     hibernate:       ddl-auto: none   datasource:     platform: h2     schema: classpath:schema.sql     data: classpath:data.sql logging:   level:     root: INFO     org.hibernate: INFO     org.hibernate.type.descriptor.sql.BasicBinder: TRACE     org.hibernate.type.descriptor.sql.BasicExtractor: TRACE     com.springms: DEBUG  

2.3 添加 H2 数据库脚本(springms-simple-provider-user\src\main\resources\schema.sql)

drop table user if exists;  CREATE TABLE USER( 	id BIGINT GENERATED by default as identity, 	username VARCHAR(40), 	name VARCHAR(20), 	age int(3), 	balance DECIMAL(10, 2), 	PRIMARY KEY(id) );  

2.4 插入 H2 数据库一些初始化数据(springms-simple-provider-user\src\main\resources\data.sql)

INSERT into user (id, username, name, age, balance) values (1, 'user1', '张三', 20, 100.00); INSERT into user (id, username, name, age, balance) values (2, 'user2', '李四', 22, 100.00); INSERT into user (id, username, name, age, balance) values (3, 'user3', '王五', 24, 100.00); INSERT into user (id, username, name, age, balance) values (4, 'user4', '赵六', 26, 100.00); INSERT into user (id, username, name, age, balance) values (5, 'user5', '李逵', 27, 100.00); INSERT into user (id, username, name, age, balance) values (6, 'user6', '张远', 10, 100.00); INSERT into user (id, username, name, age, balance) values (7, 'user7', '迪拜', 60, 100.00); INSERT into user (id, username, name, age, balance) values (8, 'user8', '哈士奇', 40, 100.00); INSERT into user (id, username, name, age, balance) values (9, 'user9', '关羽', 30, 100.00);  

2.5 添加访问底层数据模型的DAO接口(springms-simple-provider-user\src\main\java\com\springms\cloud\repository\UserRepository.java)

package com.springms.cloud.repository;  import com.springms.cloud.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;  @Repository public interface UserRepository extends JpaRepository<User, Long> {  }  

2.6 添加实体用户类User(springms-simple-provider-user\src\main\java\com\springms\cloud\entity\User.java)

package com.springms.cloud.entity;  import java.math.BigDecimal;  import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id;  @Entity public class User {   @Id   @GeneratedValue(strategy = GenerationType.AUTO)   private Long id;    @Column   private String username;    @Column   private String name;    @Column   private Short age;    @Column   private BigDecimal balance;    public Long getId() {     return this.id;   }    public void setId(Long id) {     this.id = id;   }    public String getUsername() {     return this.username;   }    public void setUsername(String username) {     this.username = username;   }    public String getName() {     return this.name;   }    public void setName(String name) {     this.name = name;   }    public Short getAge() {     return this.age;   }    public void setAge(Short age) {     this.age = age;   }    public BigDecimal getBalance() {     return this.balance;   }    public void setBalance(BigDecimal balance) {     this.balance = balance;   } }  

2.7 添加用户Web访问层Controller(springms-simple-provider-user\src\main\java\com\springms\cloud\controller\MsSimpleProviderUserController.java)

package com.springms.cloud.controller;  import com.springms.cloud.entity.User; import com.springms.cloud.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;  /**  * 用户微服务Controller。  *  * @author hmilyylimh  *  * @version 0.0.1  *  * @date 2017/9/17  *  */ @RestController public class MsSimpleProviderUserController {      @Autowired     private UserRepository userRepository;      @GetMapping("/simple/{id}")     public User findById(@PathVariable Long id) {         return this.userRepository.findOne(id);     } }   

2.8 添加简单用户微服务启动类(springms-simple-provider-user\src\main\java\com\springms\cloud\MsSimpleProviderUserApplication.java)

package com.springms.cloud;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;  /**  * 简单用户微服务类。  *  * @author hmilyylimh  *  * @version 0.0.1  *  * @date 2017/9/17  *  */ @SpringBootApplication public class MsSimpleProviderUserApplication {  	public static void main(String[] args) { 		SpringApplication.run(MsSimpleProviderUserApplication.class, args); 		System.out.println("【【【【【【 简单用户微服务 】】】】】】已启动."); 	} }  

三、测试

/****************************************************************************************  一、简单用户微服务接口测试:   1、启动 springms-simple-provider-user 模块服务,启动1个端口;  2、在浏览器输入地址 http://localhost:8000/simple/1 可以看到信息成功的被打印出来。;  ****************************************************************************************/ 

四、下载地址

https://git.oschina.net/ylimhhmily/SpringCloudTutorial.git  欢迎关注,您的肯定是对我最大的支持!!! 

本文发表于2017年09月19日 10:37
(c)注:本文转载自https://my.oschina.net/u/162754/blog/1539498,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1350 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1