数据库
id
字段设置为 character varying
User 对象
id
设置为 String
类型
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
| package com.example.springsecurity.entity;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection; import java.util.Date; import java.util.List; import java.util.UUID;
public class User implements UserDetails {
private String id;
private String userName;
private String password;
@Override public Collection<? extends GrantedAuthority> getAuthorities() { return List.of(); }
@Override public String getPassword() { return password; }
@Override public String getUsername() { return userName; }
@Override public boolean isAccountNonExpired() { return true; }
@Override public boolean isAccountNonLocked() { return true; }
@Override public boolean isCredentialsNonExpired() { return true; }
@Override public boolean isEnabled() { return true; }
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public void setUserName(String userName) { this.userName = userName; }
public void setPassword(String password) { this.password = password; }
}
|
创建 User 对象
1 2 3 4 5 6 7 8 9 10
| public User signup(RegisterUserDto input) { User user = new User(); user.setId(UUID.randomUUID().toString().replace("-", "")); user.setUserName(input.getUserName()); user.setPassword(passwordEncoder.encode(input.getPassword())); userMapper.insertUser(user); return userMapper.findByUserName(user.getUsername()).get(); }
|
UserMapper 声明插入方法
1 2 3 4 5 6
| @Mapper public interface UserMapper{ Optional<User> findByUserName(String userName);
void insertUser(User user); }
|
user-mapper.xml
1 2 3 4
| <insert id="insertUser" parameterType="com.example.springsecurity.entity.User"> insert into user_acc (user_name, password) values(#{id}, #{userName}, #{password}) </insert>
|