博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
docker微服务部署之:二、搭建文章微服务项目
阅读量:4928 次
发布时间:2019-06-11

本文共 6628 字,大约阅读时间需要 22 分钟。

一、新增demo_article模块,并编写代码

右键demo_parent->new->Module->Maven,选择Module SK为jdk8->ArtifactId:demo_article

1.修改pom.xml文件

demo_parent
com.demo
1.0-SNAPSHOT
4.0.0
demo_article
    
org.springframework.boot
spring-boot-starter-data-jpa
    
mysql
mysql-connector-java
    
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client

2.在resources目录下新增application.yml文件

server:  port: 9001 #article启动端口,可自定义spring:  application:    name: demo-article #article项目名称,可自定义  datasource:    url: jdbc:mysql://192.168.31.181:3306/docker?characterEncoding=UTF8 #192.168.31.181为虚拟机宿主机的ip地址,mysql是安装在docker容器里。详见:和    driver-class-name: com.mysql.jdbc.Driver    username: root    password: 123456  jpa:    database: mysql    show-sql: true    generate-ddl: falseeureka:  client:    fetch-registry: true    register-with-eureka: true    service-url:      defaultZone: http://192.168.31.181:7000/eureka #在IDEA中运行时使用127.0.0.1,部署发布时,请修改为虚拟机宿主机的ip地址  instance:    prefer-ip-address: true

3.新建com.demo.article包,并在该包下新建启动类ArticleApplication

package com.demo.article;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /**  * 文章微服务  */ // 标注为一个spring boot项目@SpringBootApplication // 标注为一个eureka客户端@EnableEurekaClientpublic class ArticleApplication {    public static void main(String[] args) {        SpringApplication.run(ArticleApplication.class, args);    }}

4.编写pojo类

package com.demo.article.pojo;import lombok.Data;import javax.persistence.*;import java.io.Serializable;import java.util.Date;@Entity@Table(name = "tb_article")@Data //使用该注解,相当于自动生成了getter和setter方法public class Article implements Serializable {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY) #id自增    private Integer id;    private String title;    private String content;    private String author;    private Date addTime;}

5.编写dao类

package com.demo.article.dao;import com.demo.article.pojo.Article;import org.springframework.data.jpa.repository.JpaRepository;public interface ArticleDao extends JpaRepository
{}

6.编写service类

package com.demo.article.service;import com.demo.article.pojo.Article;import java.util.List;public interface ArticleService {    List
findAll(); Article findById(Integer id); void add(Article article); void updae(Article article); void deleteById(Integer id);}
package com.demo.article.service.impl;import com.demo.article.dao.ArticleDao;import com.demo.article.pojo.Article;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class ArticleServiceImpl implements ArticleService {    @Autowired    ArticleDao articleDao;    @Override    public List
findAll() { return articleDao.findAll(); } @Override public Article findById(Integer id) { return articleDao.findById(id).get(); } @Override public void add(Article article) { articleDao.save(article); } @Override public void updae(Article article) { articleDao.save(article); } @Override public void deleteById(Integer id) { articleDao.deleteById(id); }}

7.编写vo类

package com.demo.article.vo;import lombok.Data;import java.io.Serializable;/** * 统一返回数据实体类 */@Datapublic class Result implements Serializable {    private Boolean flag;//是否成功    private String message;//消息    private Object data;//返回数据    public Result(Boolean flag, String message) {        this.flag = flag;        this.message = message;    }    public Result(Boolean flag, String message, Object data) {        this.flag = flag;        this.message = message;        this.data = data;    }}

8.编写controller类

package com.demo.article.controller;import com.demo.article.pojo.Article;import com.demo.article.service.ArticleService;import com.demo.article.vo.Result;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;@RestController@RequestMapping("/article")public class ArticleController {    @Autowired    ArticleService articleService;    @RequestMapping(method = RequestMethod.GET)    public Result findAll(){        return new Result(true, "查询成功", articleService.findAll());    }    @RequestMapping(value = "/{id}",method = RequestMethod.GET)    public Result findById(@PathVariable Integer id) {        return new Result(true, "查询成功", articleService.findById(id));    }    @RequestMapping(method = RequestMethod.POST)    public Result add(@RequestBody Article article) {        articleService.add(article);        return new Result(true, "添加成功");    }    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)    public Result update(@RequestBody Article article, @PathVariable("id") Integer id) {        article.setId(id);        articleService.updae(article);        return new Result(true,"修改成功");    }    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)    public Result deleteById(@PathVariable("id") Integer id) {        articleService.deleteById(id);        return new Result(true, "删除成功");    }}

9.数据库sql脚本

先创建数据库docker,字符集utf8,排序规则utf8-general_ci

SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for tb_article-- ----------------------------DROP TABLE IF EXISTS `tb_article`;CREATE TABLE `tb_article`  (  `id` int(11) NOT NULL AUTO_INCREMENT,  `title` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,  `content` mediumtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL,  `author` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,  `add_time` datetime(0) NULL DEFAULT NULL,  PRIMARY KEY (`id`) USING BTREE) ENGINE = MyISAM AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

10.运行ArticleApplication启动类

刷新eureka界面,可以看到有一个名为DEMO-ARTICLE的服务已经注册上来了

11.使用Postman进行CRUD测试

11.1 测试add方法

11.2 测试findAll方法

11.3 测试udpate方法

11.4 测试findById方法

11.5 测试delete方法

 

转载于:https://www.cnblogs.com/ztone/p/10569239.html

你可能感兴趣的文章
[LeetCode] 127. Word Ladder _Medium tag: BFS
查看>>
20172302 《程序设计与数据结构》第四周学习总结
查看>>
FZU 2086 餐厅点餐(枚举)
查看>>
HDU 2188 悼念512汶川大地震遇难同胞——选拔志愿者(基础巴什博奕)
查看>>
多态,虚函数
查看>>
Could not obtain information about Windows NT group/user 'xxxx\xxxx', error code 0x5
查看>>
get_locked_objects_rpt.sql
查看>>
基于SignalR的消息推送与二维码描登录实现
查看>>
jquery 绑定事件
查看>>
排序之快速排序
查看>>
单调队列&单调栈归纳
查看>>
新安装的jdk,不知道为啥一直走别的jdk路径
查看>>
leetcode 9. Palindrome Number
查看>>
2018/1/9 redis学习笔记(一)
查看>>
协程 - 单线程并发--day36
查看>>
oracle存储过程遇到的问题
查看>>
如何使用WPS从正文开始页码为1,而不是从目录开始?
查看>>
C# Select
查看>>
【转】关于Scapy
查看>>
关于AES加密,以及各种分组加密
查看>>