一、新增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 { ListfindAll(); 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 ListfindAll() { 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方法