当前位置:   article > 正文

会员中心微服务

会员中心微服务

1.环境配置

1.创建会员中心模块

image-20240322113235884

2.检查父子模块的pom.xml
1.父模块
注意:如果父模块中的依赖显示not found,原因是子模块并没有引用,不用在意

image-20240322113712318

2.子模块

image-20240322113535868

3.pom.xml 引入相关依赖(别忘记刷新maven)
    <dependencies>
        <!-- springboot web starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 如果在子工程/模块指定了 version,则以指定为准 -->
        </dependency>
        <!-- 
        1. starter-actuator 是 springboot 程序的监控系统,可以实现健康检查,info 信息
        等
        2. 访问 http://localhost:10000/actuator 可以看到相关链接, 还可以做相关设置. -->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <!-- 这里我们重新指定一下 version 因为父项目中没有对这个依赖进行版本仲裁-->
            <version>1.1.13</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- jdbc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
  • 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
4.application.yml 进行基本配置
server:
  port: 10000 # 配置服务端口
spring:
  application:
    name: member-service-provider-10000 # 配置服务的名称,名字任意这里与项目名保持一致
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    # 别忘记创建数据库之后修改数据库名称
    url: jdbc:mysql://localhost:3306/xxx?useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
mybatis:
  mapper-locations: classpath:mapper/*.xml # 扫描所有Mapper.xml
  type-aliases-package: com.sun.springcloud.entity # 扫描所有实体类,可以直接使用类名来标识

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
5.com/sun/springcloud/MemberApplication.java 编写主程序测试
package com.sun.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Description:
 *
 * @Author sun
 * @Create 2024/3/22 12:49
 * @Version 1.0
 */
@SpringBootApplication
public class MemberApplication {
    public static void main(String[] args) {
        SpringApplication.run(MemberApplication.class, args);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
6.出现两个问题
1.端口冲突,所以这里换一个端口为10001,将子模块名也一并修改!

image-20240323101726428

2.数据库没创建

image-20240323101910525

7.使用云数据库
1.数据库表设计
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', 'smith@sohu.com', 1);
SELECT * from member;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
2.修改application.yml 的port,name以及数据库配置
server:
  port: 10001 # 配置服务端口
spring:
  application:
    name: member-service-provider-10001 # 配置服务的名称,名字任意这里与项目名保持一致
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    # 别忘记创建数据库之后修改数据库名称
    url: 你连接数据库的url
    username: sdfsdfsd
    password: 88888
mybatis:
  mapper-locations: classpath:mapper/*.xml # 扫描所有Mapper.xml
  type-aliases-package: com.sun.springcloud.entity # 扫描所有实体类,可以直接使用类名来标识

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
3.执行主程序测试

image-20240323110603378

2.代码编写

1.com/sun/springcloud/entity/Member.java 创建实体类
package com.sun.springcloud.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * Description:
 *
 * @Author sun
 * @Create 2024/3/23 11:10
 * @Version 1.0
 */

@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;
}

  • 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
2.com/sun/springcloud/util/Result.java 封装响应结果的工具类
package com.sun.springcloud.util;

/**
 * Description: 用于返回结果, 利于 json 格式
 *
 * @Author sun
 * @Create 2024/3/23 11:12
 * @Version 1.0
 */

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;
    }
}
  • 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
3.编写dao层
1.com/sun/springcloud/dao/MemberDao.java 编写Mapper接口并注入容器
package com.sun.springcloud.dao;

import com.sun.springcloud.entity.Member;
import org.apache.ibatis.annotations.Mapper;


/**
 * Description: 对member表进行操作
 *
 * @Author sun
 * @Create 2024/3/23 21:06
 * @Version 1.0
 */
@Mapper // 注入容器
public interface MemberDao {

    /**
     * 根据id进行查询
     * @param id
     * @return 查询成功返回Member对象,查询失败返回null
     */
    public Member queryMemberById(Long id);

    /**
     * 保存一条记录
     * @param member
     * @return 返回受影响的行数
     */
    public int save(Member member);
}

  • 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
2.mapper/MemberMapper.xml 编写Mapper实现类
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.sun.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>

    <!-- useGeneratedKeys="true" keyProperty="id" 表示插入之后将自增的主键id的值返回到Member对象中 -->
    <insert id="save" parameterType="Member" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO `member` VALUES
        (NULL, #{name}, MD5(#{pwd}), #{mobile}, #{email}, #{gender});
    </insert>
</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
3.com/sun/springcloud/MemberApplicationTest.java 测试dao层
package com.sun.springcloud;

import com.sun.springcloud.dao.MemberDao;
import com.sun.springcloud.entity.Member;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * Description:
 *
 * @Author sun
 * @Create 2024/3/23 21:46
 * @Version 1.0
 */
@SpringBootTest
@Slf4j
public class MemberApplicationTest {
    @Resource
    private MemberDao memberDao;

    @Test
    public void queryMemberById() {
        Member member = memberDao.queryMemberById(1L);
        log.info("member={}", member);
    }

    @Test
    public void save() {
        Member member = new Member();
        member.setName("sun");
        member.setPwd("12121");
        member.setMobile("2342343");
        member.setEmail("123@qq.com");
        member.setGender(0);

        int save = memberDao.save(member);
        Assert.assertEquals(1, save);
    }
}

  • 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

image-20240323215932670

4.编写service层
1.com/sun/springcloud/service/MemberService.java 编写service接口
package com.sun.springcloud.service;

import com.sun.springcloud.entity.Member;

/**
 * Description: 对member表进行操作的service
 *
 * @Author sun
 * @Create 2024/3/23 22:02
 * @Version 1.0
 */
public interface MemberService {
    /**
     * 根据id获取一条记录
     *
     * @param id
     * @return 成功返回member对象,失败返回null
     */
    public Member queryMemberById(Long id);


    /**
     * 保存一条记录
     *
     * @param member
     * @return 成功返回1,失败返回0
     */
    public int save(Member member);

}

  • 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
2.com/sun/springcloud/service/Impl/MemberServiceImpl.java 编写service实现类并注入容器
package com.sun.springcloud.service.Impl;

import com.sun.springcloud.dao.MemberDao;
import com.sun.springcloud.entity.Member;
import com.sun.springcloud.service.MemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Description: member表的service实现类
 *
 * @Author sun
 * @Create 2024/3/23 22:05
 * @Version 1.0
 */
@Service // 注入容器
public class MemberServiceImpl implements MemberService {
    @Resource // 依赖注入memberdao
    private MemberDao memberDao;
    @Override
    public Member queryMemberById(Long id) {
        return memberDao.queryMemberById(id);
    }

    @Override
    public int save(Member member) {
        return memberDao.save(member);
    }
}

  • 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
3.测试service层
    @Test
    public void queryMemberById2() {
        Member member = memberService.queryMemberById(1L);
        log.info("member={}", member);
    }

    @Test
    public void save2() {
        Member member = new Member();
        member.setName("sun");
        member.setPwd("12121");
        member.setMobile("2342343");
        member.setEmail("123@qq.com");
        member.setGender(0);
        int save = memberService.save(member);
        Assert.assertEquals(1, save);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

image-20240323221243863

5.编写controller层
1.com/sun/springcloud/controller/MemberController.java 编写controller
package com.sun.springcloud.controller;

import com.sun.springcloud.entity.Member;
import com.sun.springcloud.service.MemberService;
import com.sun.springcloud.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Description: member表的控制器
 *
 * @Author sun
 * @Create 2024/3/23 22:24
 * @Version 1.0
 */
@RestController
@Slf4j
public class MemberController {
    @Resource // 注入service
    private MemberService memberService;

    /**
     * 根据前端传进来的json数据插入一条记录
     *
     * @param member json类型的member对象数据
     * @return 返回json类型的响应
     */
    @PostMapping("/member/save")
    public Result save(@RequestBody Member member) {
        int save = memberService.save(member);
        if (save > 0) {
            return Result.success("添加会员成功!", save);
        } else {
            return Result.error("401", "添加会员失败");
        }
    }

    /**
     * 根据id来获取某个会员的信息
     *
     * @param id 使用路径参数的形式传入参数
     * @return 返回json格式的数据
     */
    @GetMapping("/member/get/{id}") // 这里使用的路径参数
    public Result getMemberById(@PathVariable("id") Long id) {
        Member member = memberService.queryMemberById(id);

        if (member != null) {
            return Result.success("查询成功!", member);
        } else {
            return Result.error("402", "ID= " + id + "不存在");
        }
    }
}

  • 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
2.postman测试

image-20240323223620845

image-20240323225016196

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/324847
推荐阅读
相关标签
  

闽ICP备14008679号