写在最前
如果这个项目让你有所收获,记得 Star 关注哦,这对我是非常不错的鼓励与支持。
源码地址(后端):https://gitee.com/csps/mingyue-springcloud-learning
源码地址(前端):https://gitee.com/csps/mingyue-springcloud-ui
文档地址:https://gitee.com/csps/mingyue-springcloud-learning/wikis
mingyue-common-mybatis
模块化 mybatis,便于管理 mybatis,并使其与项目解耦。
创建 mybatis 模块
<dependencies>
<dependency>
<groupId>com.csp.mingyue</groupId>
<artifactId>mingyue-common-core</artifactId>
</dependency>
<!-- Mysql 数据库驱动 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!-- mybatis-plus 代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</dependency>
<!-- 代码生成使用模板 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
</dependency>
</dependencies>
迁移 config
MybatisPlusConfiguration 文件迁移入 mingyue-common-mybatis
模块
整理依赖
mingyue-common-bom
<!-- Mybatis -->
<dependency>
<groupId>com.csp.mingyue</groupId>
<artifactId>mingyue-common-mybatis</artifactId>
<version>${project.version}</version>
</dependency>
mingyue-system-api
<dependency>
<groupId>com.csp.mingyue</groupId>
<artifactId>mingyue-common-core</artifactId>
</dependency>
mingyue-system-biz
<dependency>
<groupId>com.csp.mingyue</groupId>
<artifactId>mingyue-common-mybatis</artifactId>
</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=/system/**
filters:
- StripPrefix=1
网关调用
发送请求:
curl --location --request GET 'http://127.0.0.1:9100/system/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
}
}
小结
整理一下,模块和代码结构舒服多了~~~,用户名查询的接口已经开发完成啦。
是时候引入接口文档并聚合各模块,方便提供给前端童鞋联调喽~~~