注册接口

1、service模块下创建子模块service_ucenter

2、创建用户数据库

3、用代码生成器生成代码

4、编写配置文件

java 复制代码
# 服务端口
server.port=8006
# 服务名
spring.application.name=service-ucenter
# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8
spring.datasource.username=guli
spring.datasource.password=123123
spring.redis.host=192.168.140.132
spring.redis.port=6379
spring.redis.database= 0
spring.redis.timeout=1800000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
#最小空闲
#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
#配置mapper xml文件的路径
mybatis-plus.mapper-locations=classpath:com/atguigu/ucenterservice/mapper/xml/*.xml
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

5、启动类

java 复制代码
package com.atguigu.ucenterservice;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan({"com.atguigu"})
@SpringBootApplication//取消数据源自动配置
@MapperScan("com.atguigu.ucenterservice.mapper")
public class ServiceUcApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceUcApplication.class, args);
    }
}

6、实现controller

(1)创建RegisterVo类

java 复制代码
@Data
public class RegisterVo {
    @ApiModelProperty(value = "昵称")
    private String nickname;
    @ApiModelProperty(value = "手机号")
    private String mobile;
    @ApiModelProperty(value = "密码")
    private String password;
    @ApiModelProperty(value = "验证码")
    private String code;
}

(2)实现UcenterMemberController方法

java 复制代码
@Api(description="会员管理")
@RestController
@RequestMapping("/ucenterservice/ucenter")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterService;
    @ApiOperation(value = "注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterService.register(registerVo);
        return R.ok();
    }
}

7、实现service

java 复制代码
//注册
@Override
public void register(RegisterVo registerVo) {
    //1获取注册信息
    String nickname = registerVo.getNickname();
    String mobile = registerVo.getMobile();
    String password = registerVo.getPassword();
    String code = registerVo.getCode();

    //2验证参数是否为空
    if(StringUtils.isEmpty(nickname)||StringUtils.isEmpty(mobile)
    ||StringUtils.isEmpty(password)||StringUtils.isEmpty(code)){
        throw new GuliException(20001,"注册信息缺失");
    }
    //3根据手机号查询是否手机号相同
    QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
    wrapper.eq("mobile",mobile);
    Integer count = baseMapper.selectCount(wrapper);
    if(count>0){
        throw new GuliException(20001,"手机号已存在");
    }
    //4判断验证码
    String codeRedis = redisTemplate.opsForValue().get(mobile);
    if(!code.equals(codeRedis)){
        throw new GuliException(20001,"验证码错误");
    }
    //5存入数据库
    //5.1复制数据
    UcenterMember ucenterMember = new UcenterMember();
    BeanUtils.copyProperties(registerVo,ucenterMember);
    //5.2密码加密
    String pwBefore = ucenterMember.getPassword();
    String pwAfter = MD5.encrypt(pwBefore);
    ucenterMember.setPassword(pwAfter);
    //5.3手动设置
    ucenterMember.setIsDisabled(false);
ucenterMember.setAvatar("https://guli-file-190513.oss-cn-beijing.aliyuncs.com/avatar/default.jpg");
    //5.4存入数据
    int insert = baseMapper.insert(ucenterMember);
    if(insert==0){
        throw new GuliException(20001,"注册失败"); 
    }
}
相关推荐
小鹿的周先生6 小时前
Spring-AI-第2篇-ChatClient 实战:使用 DeepSeek 完成第一次 AI 对话
java·人工智能·spring
悲且狂6 小时前
SpringBoot项目改造注意事项(旧项目框架复用)
java·spring boot·后端
花间相见7 小时前
【LangChain组件03】—— LangChain Agents 执行与状态:工作流程与状态管理实战
java·前端·langchain
敲代码的嘎仔7 小时前
28届后端开发-海康威视日常实习一面(已OC)
java·开发语言·后端·面试·海康威视·实习·大厂
IT_陈寒7 小时前
JavaScript数组排序踩的坑,差点让我加班到凌晨
前端·人工智能·后端
接着奏乐接着舞。7 小时前
【2026】73道Redis 常见面试题与参考答案
数据库·redis·后端·缓存
掘金者阿豪8 小时前
Seedance 2.0/2.5 虚拟素材能跨 Key 共用吗?一次讲清 Asset ID、账号隔离与 SaaS 素材架构
前端·后端
程序员黎剑8 小时前
Spring-Bean生命周期-构造器访问Autowired字段为null
java·后端·spring
泡海椒8 小时前
JQuick-Curl 拦截器实战:统一 Token、日志、请求预处理,让第三方接口调用真正工程化
后端
古法安卓9 小时前
Android-重启流程源码解析
android·java·android studio