前言
本篇博文是关于Spring Cloud–从零开始搭建微服务基础环境【二】,希望你能够喜欢
个人主页:晨犀主页
个人简介:大家好,我是晨犀,希望我的文章可以帮助到大家,您的满意是我的动力
欢迎大家:这里是CSDN,我总结知识的地方,欢迎来到我的博客,感谢大家的观看
如果文章有什么需要改进的地方还请大佬不吝赐教 先在此感谢啦
1、通过浏览器可以获取会员信息(通过会员中心微服务模块)
1、创建Moduel 并完成配置
2、创建数据库/表
3、创建entity-dao/Mapper.xml-service-controller
4、完成测试
父工程的pom.xml-会做相应变化,管理member-service-provider-10000 微服务子模块
<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">
<parent>
<artifactId>e-commerce-centerartifactId>
<groupId>com.nlc.springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>member-service-provider-10000artifactId>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.13version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
server:
port: 10000
spring:
application:
name: member-service-provider #配置应用的名称
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/e_commerce_center_db?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
#配置mybatis
mybatis:
mapper-locations: classpath:mapper/*.xml #指定mapper.xml文件位置
type-aliases-package: com.my.springcloud.entity # 实例类所在的包,这样通过类名就可以引用
@SpringBootApplication
public class MemberApplication10000 {
public static void main(String[] args) {
SpringApplication.run(MemberApplication10000.class, args);
}
}
CREATE DATABASE e_commerce_center_db
USE e_commerce_center_db
CREATE TABLE member
(
id BIGINT NOT NULL AUTO_INCREMENT COMMENT 'id',
NAME VARCHAR(64) COMMENT '用户名',
pwd CHAR(32) COMMENT '密码',
mobile VARCHAR(20) COMMENT '手机号码',
email VARCHAR(64) COMMENT '邮箱',
gender TINYINT COMMENT '性别',
PRIMARY KEY (id)
);
INSERT INTO member VALUES
(NULL, 'smith', MD5('123'), '123456789000', '[email protected]', 1);
SELECT * FROM member
@AllArgsConstructor
@NoArgsConstructor
@Data
//Serializable 加上,后面可能使用
public class Member implements Serializable {
private Long id;
private String name;
private String pwd;
private String mobile;
private String email;
private Integer gender;
}
创建com/my/springcloud/entity/Result.java
/**
* 1. 用于返回结果, 利于json 格式
* 2. 这个工具类, 在网上也可找到
*/
public class Result<T> {
private String code;
private String msg;
private T data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Result() {
}
public Result(T data) {
this.data = data;
}
public static Result success() {
Result result = new Result<>();
result.setCode("200");
result.setMsg("success");
return result;
}
public static <T> Result<T> success(T data) {
Result<T> result = new Result<>(data);
result.setCode("200");
result.setMsg("success");
return result;
}
public static <T> Result<T> success(String msg, T data) {
Result<T> result = new Result<>(data);
result.setCode("200");
result.setMsg(msg);
return result;
}
public static Result error(String code, String msg) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
public static <T> Result<T> error(String code, String msg, T data) {
Result<T> result = new Result<>(data);
result.setCode(code);
result.setMsg(msg);
return result;
}
}
创建接口:com/my/springcloud/dao/MemberDao.java
@Mapper
public interface MemberDao {
//crud 接口...
Member queryMemberById(Long id);
int save(Member member);
}
创建resources/mapper/MemberMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.my.springcloud.dao.MemberDao">
<resultMap id="BaseResultMap" type="Member">
<id column="id" property="id" jdbcType="BIGINT">id>
<id column="name" property="name" jdbcType="VARCHAR">id>
<id column="pwd" property="pwd" jdbcType="VARCHAR">id>
<id column="mobile" property="mobile" jdbcType="VARCHAR">id>
<id column="email" property="email" jdbcType="VARCHAR">id>
<id column="gender" property="gender" jdbcType="TINYINT">id>
resultMap>
<select id="queryMemberById" parameterType="Long" resultMap="BaseResultMap">
SELECT * FROM `member` WHERE `id`=#{id}
select>
<insert id="save" parameterType="Member" useGeneratedKeys="true" keyProperty="id">
INSERT INTO `member`(`NAME`,`pwd`,`mobile`,`email`,`gender`)
VALUES(#{name}, MD5(#{pwd}), #{mobile}, #{email}, #{gender});
insert>
mapper>
完成测试
创建接口:com/my/springcloud/service/MemberService.java
public interface MemberService {
Member queryMemberById(Long id);
int save(Member member);
}
创建com/my/springcloud/service/impl/MemberServiceImpl.java
@Service
public class MemberServiceImpl implements MemberService {
//装配MemberDao
@Resource
private MemberDao memberDao;
@Override
public Member queryMemberById(Long id) {
return memberDao.queryMemberById(id);
}
@Override
public int save(Member member) {
return memberDao.save(member);
}
}
完成测试
创建:com/my/springcloud/controller/MemberController.java
@RestController
@Slf4j
public class MemberController {
//装配MemberService
@Resource
private MemberService memberService;
//添加方法/接口
//这里请小伙伴回顾, 应该如何提交
//说明
//1. 我们的前端如果是以json格式来发送添加信息Member, 那么我们需要使用@RequestBody
// , 才能将数据封装到对应的bean, 同时保证http的请求头的 content-type是对应
//2. 如果前端是以表单形式提交了,则不需要使用@RequestBody, 才会进行对象参数封装, 同时保证
// http的请求头的 content-type是对应
@PostMapping("/member/save")
public Result save(@RequestBody Member member) {
log.info("service-provider member={}", member);
int affected = memberService.save(member);
if (affected > 0) { //说明添加成功
return Result.success("添加会员成功", affected);
} else {
return Result.error("401", "添加会员失败");
}
}
//查询的方法/接口
//这里使用url占位符+@PathVariable
@GetMapping("/member/get/{id}")
public Result getMemberById(@PathVariable("id") Long id, HttpServletRequest request) {
//String color = request.getParameter("color");
//String address = request.getParameter("address");
//模拟超时, 休眠5s
//try {
// TimeUnit.MILLISECONDS.sleep(5000);
//} catch (InterruptedException e) {
// e.printStackTrace();
//}
Member member = memberService.queryMemberById(id);
//使用Result把查询到的结果返回
if (member != null) {
//return Result.success("查询会员成功 member-service-provider-10000 " + color + "-" + address, member);
return Result.success("查询会员成功 member-service-provider-10000", member);
} else {
return Result.error("402", "ID= " + id + "不存在");
}
}
}
浏览器输入: http://localhost:10000/member/get/1
1、我们的前端如果是以json 格式来发送添加信息furn,那么我们需要使用@RequestBody,才能将数据封装到对应的bean, 同时保证http 的请求头的content-type 是对应。
2、如果前端是以表单形式提交了/或者是以parameters,则不需要使用@RequestBody, 才会进行对象参数封装, 同时保证http 的请求头的content-type 是对应。
3、在进行SpringBoot 应用程序测试时,引入的JUnit 是org.junit.jupiter.api.Test。
4、在运行程序时,一定要确保你的XxxxMapper.xml 文件被自动放到的target 目录的classes 指定目录。
文章到这里就结束了,如果有什么疑问的地方请指出,诸大佬们一起来评论区一起讨论
希望能和诸大佬们一起努力,今后我们一起观看感谢您的阅读
如果帮助到您不妨3连支持一下,创造不易您们的支持是我的动力