1.后端创建
创建最最外层的微服务框架名为 vote_cloud
删去多余的文件,保留.idea和pom.xml即可,修改pom配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <?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> <packaging>pom</packaging> <groupId>org.kangdd</groupId> <artifactId>vote_cloud</artifactId>
<modules> <module>common-module</module> <module>user-service</module> <module>gateway-service</module> <module>exam-service</module> <module>vote-service</module> </modules> <version>1.0-SNAPSHOT</version>
<properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>2.7.18</spring-boot.version> <spring-cloud.version>2021.0.9</spring-cloud.version> <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency>
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
|
刷新maven创建公共模块
2.创建公共模块
2.1 common-module
2.1.1 公共返回类型R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package com.kangdd.commonmodule.common;
import lombok.Data;
import java.io.Serializable;
@Data public class R<T> implements Serializable { private static final long serialVersionUID = 1L;
private Integer code;
private String message;
private T data;
public R() { }
public R(Integer code, String message, T data) { this.code = code; this.message = message; this.data = data; }
public static <T> R<T> success() { return new R<>(200, "操作成功", null); }
public static <T> R<T> success(T data) { return new R<>(200, "操作成功", data); }
public static <T> R<T> success(String message) { return new R<>(200, message, null); }
public static <T> R<T> success(String message, T data) { return new R<>(200, message, data); }
public static <T> R<T> error(String message) { return new R<>(500, message, null); }
public static <T> R<T> error(Integer code, String message) { return new R<>(code, message, null); }
public static <T> R<T> error(Integer code, String message, T data) { return new R<>(code, message, data); }
public boolean isSuccess() { return this.code != null && this.code == 200; } }
|
2.1.2 消息处理器
config.RestTemplateConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package com.kangdd.commonmodule.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.client.RestTemplate;
import java.util.ArrayList; import java.util.List;
@Configuration public class RestTemplateConfig {
@Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
MappingJackson2HttpMessageConverter jacksonConverter = null; for (HttpMessageConverter<?> converter : restTemplate.getMessageConverters()) { if (converter instanceof MappingJackson2HttpMessageConverter) { jacksonConverter = (MappingJackson2HttpMessageConverter) converter; break; } }
if (jacksonConverter != null) { List<MediaType> mediaTypes = new ArrayList<>(jacksonConverter.getSupportedMediaTypes()); mediaTypes.add(MediaType.TEXT_PLAIN); jacksonConverter.setSupportedMediaTypes(mediaTypes); converters.add(jacksonConverter); }
for (HttpMessageConverter<?> converter : restTemplate.getMessageConverters()) { if (!(converter instanceof MappingJackson2HttpMessageConverter)) { converters.add(converter); } }
restTemplate.setMessageConverters(converters); return restTemplate; } }
|
2.1.3 字段自动填充处理器
handler.MyMetaObjectHandler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.kangdd.commonmodule.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component public class MyMetaObjectHandler implements MetaObjectHandler {
@Override public void insertFill(MetaObject metaObject) { this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); }
@Override public void updateFill(MetaObject metaObject) { this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); } }
|
2.1.4 实体对象
pojo.user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.kangdd.commonmodule.pojo;
import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import lombok.Data;
import java.time.LocalDateTime;
@Data public class User { @TableId(value = "id", type = IdType.AUTO) private Integer uid; @TableField("openid") private String openid; @TableField("nickname") private String userName; @TableField("avatar_url") private String userImage; @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE) private LocalDateTime updateTime; }
|
pojo.vote
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| package com.kangdd.commonmodule.pojo;
import com.baomidou.mybatisplus.annotation.*; import lombok.Data;
import java.time.LocalDateTime;
@Data @TableName("vote") public class Vote {
@TableId(value = "id", type = IdType.AUTO) private Long id;
@TableField("title") private String title;
@TableField("type") private Byte type;
@TableField("user_id") private Long userId;
@TableField(value = "create_time", fill = FieldFill.INSERT) private LocalDateTime createTime;
@TableField("dead_time") private LocalDateTime deadTime;
@TableField("status") private Byte status;
@TableField("is_anonymous") private Byte isAnonymous; @TableField("is_public") private Byte isPublic;
@TableField("share") private String share;
@TableField(exist = false) private Integer voteCount; }
|
pojo.vote_potion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.kangdd.commonmodule.pojo;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data;
@Data @TableName("vote_option") public class VoteOption {
@TableId(value = "id", type = IdType.AUTO) private Long id;
@TableField("vote_id") private Long voteId;
@TableField("content") private String content;
@TableField("sort") private Integer sort;
@TableField("vote_count") private Integer voteCount; }
|
pojo.vote_record
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.kangdd.commonmodule.pojo;
import com.baomidou.mybatisplus.annotation.*; import lombok.Data;
import java.time.LocalDateTime;
@Data @TableName("vote_record") public class VoteRecord {
@TableId(value = "id", type = IdType.AUTO) private Long id;
@TableField("vote_id") private Long voteId;
@TableField("user_id") private Long userId;
@TableField("option_id") private Long optionId;
@TableField(value = "create_time", fill = FieldFill.INSERT) private LocalDateTime createTime; }
|
3. gateway网关
稍后更新见(二)