010-从零搭建微服务-系统服务(三)

写在最前

如果这个项目让你有所收获,记得 Star 关注哦,这对我是非常不错的鼓励与支持。

源码地址(后端):https://gitee.com/csps/mingyue

源码地址(前端):https://gitee.com/csps/mingyue-ui

文档地址:https://gitee.com/csps/mingyue/wikis

mingyue-common-mybatis

模块化 mybatis,便于管理 mybatis,并使其与项目解耦。

创建 mybatis 模块

<dependencies>
    <dependency>
      <groupId>com.csp.mingyuegroupId>
      <artifactId>mingyue-common-coreartifactId>
    dependency>

    
    <dependency>
      <groupId>com.mysqlgroupId>
      <artifactId>mysql-connector-jartifactId>
    dependency>
    
    <dependency>
      <groupId>com.baomidougroupId>
      <artifactId>mybatis-plus-boot-starterartifactId>
    dependency>
    
    <dependency>
      <groupId>com.baomidougroupId>
      <artifactId>mybatis-plus-generatorartifactId>
    dependency>
    
    <dependency>
      <groupId>org.apache.velocitygroupId>
      <artifactId>velocity-engine-coreartifactId>
    dependency>
dependencies>

迁移 config

MybatisPlusConfiguration 文件迁移入 mingyue-common-mybatis 模块

整理依赖

mingyue-common-bom


<dependency>
    <groupId>com.csp.mingyuegroupId>
    <artifactId>mingyue-common-mybatisartifactId>
    <version>${project.version}version>
dependency>

mingyue-system-api

<dependency>
    <groupId>com.csp.mingyuegroupId>
    <artifactId>mingyue-common-coreartifactId>
dependency>

mingyue-system-biz

<dependency>
    <groupId>com.csp.mingyuegroupId>
    <artifactId>mingyue-common-mybatisartifactId>
dependency>

通过用户名获取用户信息

Service

public interface SysUserService extends IService<SysUser> {

    SysUser getSysUserInfoByUsername(String username);
}

Controller

@GetMapping("getSysUserInfoByUsername")
public R<SysUser> getSysUserInfoByUsername(String username) {
  return R.ok(sysUserService.getSysUserInfoByUsername(username));
}

调用接口

发送请求:

curl --location --request GET 'http://127.0.0.1:8000/sysUser/getSysUserInfoByUsername?username=mingyue' \
--header 'User-Agent: Apifox/1.0.0 (https://www.apifox.cn)' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8000' \
--header 'Connection: keep-alive'

返回结果:

{
    "code": 200,
    "msg": "操作成功",
    "data": {
        "userId": 1,
        "username": "mingyue",
        "nickname": "明月",
        "sex": "0",
        "password": "123456",
        "phone": "13260718262",
        "email": null,
        "avatar": null,
        "lockFlag": "0",
        "delFlag": "0",
        "createTime": null,
        "updateTime": null,
        "createBy": null,
        "updateBy": null
    }
}

网关调用用户信息接口

后续添加的接口或者服务都走网关调用了,不再直接访问服务!

添加 mingyue-gateway 网关路由

# 系统服务
- id: mingyue-system
  uri: lb://mingyue-system-biz
  predicates:
  	- Path=/sys/**
  filters:
  	- StripPrefix=1

网关调用

发送请求:

curl --location --request GET 'http://127.0.0.1:9100/sys/sysUser/getSysUserInfoByUsername?username=mingyue' \
--header 'User-Agent: Apifox/1.0.0 (https://www.apifox.cn)' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:9100' \
--header 'Connection: keep-alive'

返回结果:

{
    "code": 200,
    "msg": "操作成功",
    "data": {
        "userId": 1,
        "username": "mingyue",
        "nickname": "明月",
        "sex": "0",
        "password": "123456",
        "phone": "13260718262",
        "email": null,
        "avatar": null,
        "lockFlag": "0",
        "delFlag": "0",
        "createTime": null,
        "updateTime": null,
        "createBy": null,
        "updateBy": null
    }
}

小结

整理一下,模块和代码结构舒服多了~~~,用户名查询的接口已经开发完成啦。

是时候引入接口文档并聚合各模块,方便提供给前端童鞋联调喽~~~

你可能感兴趣的:(MingYue微服务,微服务,mybatis,java)