Spring Boot 笔记 009 创建接口_更新用户基本信息

1.1.1 给User实体类添加校验

java 复制代码
package com.geji.pojo;



import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Data;

import java.time.LocalDateTime;
//lombok  在编译阶段,为实体类自动生成setter  getter toString
// pom文件中引入依赖   在实体类上添加注解
@Data
public class User {
    @NotNull
    private Integer id;//主键ID
    private String username;//用户名
    @JsonIgnore//让springmvc把当前对象转换成json字符串的时候,忽略password,最终的json字符串中就没有password这个属性了
    private String password;//密码


    @NotEmpty
    @Pattern(regexp = "^\\S{1,10}$")
    private String nickname;//昵称

    @NotEmpty
    @Email
    private String email;//邮箱
    private String userPic;//用户头像地址
    private LocalDateTime createTime;//创建时间
    private LocalDateTime updateTime;//更新时间
}

1.1.1 UserController中添加update方法

java 复制代码
    @PutMapping("/update")
    public Result update(@RequestBody @Validated User user) {
        userService.update(user);
        return Result.success();
    }

1.1.2 UserService中添加update接口

java 复制代码
package com.geji.service;

import com.geji.pojo.User;

public interface UserService {
    //根据用户名查找用户
    User findByUserName(String username);

    //根据用户名和密码注册
    void register(String username, String password);

    void update(User user);
}

1.1.3 UserServiceImpl中添加update实现类

java 复制代码
    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

1.1.4 UserMapper中添加update方法

java 复制代码
package com.geji.mapper;

import com.geji.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserMapper {
    //根据用户名查询用户
    @Select("select * from user where username=#{username}")
    User findByUserName(String username);

    //添加
    @Insert("insert into user(username,password,create_time,update_time)" +
            " values(#{username},#{password},now(),now())")
    void add(String username, String password);

    @Update("update user set nickname=#{nickname},email=#{email},update_time=#{updateTime} where id=#{id}")
    void update(User user);

}

1.1.5 使用postman测试

相关推荐
敲个大西瓜1 天前
JAVA 并发编程
java·并发编程
IT古董1 天前
【MES学习笔记系列】MES 术语表
笔记·学习
vHelios1 天前
【电商项目】商品服务模块的问题解决与代码逻辑思考
java·sql·mybatis
rannn_1111 天前
【力扣hot100】238、41、73题解
java·算法·leetcode·开发
今天的砖头有点烫手啊1 天前
Spring Boot
java·人工智能
Anesthesia丶1 天前
PromoxVE安装笔记
笔记·ubuntu·server·promox
疯狂打码的少年1 天前
【数据结构】顺序表 vs 链表的对比与选择
数据结构·笔记·链表
码农颜1 天前
5.4.1 锁分类
java·数据库·mysql
果果燕1 天前
实习笔记(一):NFS、Samba、VNC、Git、CMake、systemd、内核、交叉编译
linux·arm开发·c++·笔记·git
linux-hzh1 天前
百日算法修炼 · Day 03
java·算法