Users相关功能重构及增加

This commit is contained in:
myh 2023-11-06 16:56:22 +08:00
parent 6f29019615
commit 97b22ee98a
4 changed files with 60 additions and 7 deletions

View File

@ -1,11 +1,33 @@
package com.example.springdemo.dao;
import com.example.springdemo.entities.Users;
import com.example.springdemo.entities.password.UsersPassword;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public interface UsersRepository
extends JpaRepository<Users, Long> {
Users findByName(String name);
@Modifying
@Transactional
void deleteByName(String name);
//添加新用户时创建密码
void addPassword(UsersPassword usersPassword);
@Modifying
@Transactional
@Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.users.id = ?2")
void updatePassword(String password, Long userID);
//删除账户时删除密码
@Modifying
@Transactional
@Query("delete from UsersPassword UsersPwd where UsersPwd.users.id = ?1")
void deletePasswordById(Long userID);
}

View File

@ -18,7 +18,7 @@ import java.util.List;
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;//主键用户ID
private Long id;//主键用户ID
private String name;//用户姓名
private String sex;//用户性别
@Column(name = "phoneNumber")

View File

@ -1,6 +1,7 @@
package com.example.springdemo.service;
import com.example.springdemo.entities.Users;
import com.example.springdemo.entities.password.UsersPassword;
import java.util.Optional;
@ -8,13 +9,21 @@ public interface UsersService {
Users addUsers(Users user);
void deleteUsersById(Long id);
void deleteUsersById(Long userID);
void deleteUsersByName(String name);
Users updateUsers(Users user);
Iterable<Users> findAllUsers();
Optional<Users> findById(Long id);
Optional<Users> findById(Long userID);
Users findByName(String name);
void addPassword(UsersPassword usersPassword);
void updatePassword(String password, Long userID);
void deletePasswordById(Long userID);
}

View File

@ -2,6 +2,7 @@ package com.example.springdemo.service;
import com.example.springdemo.dao.UsersRepository;
import com.example.springdemo.entities.Users;
import com.example.springdemo.entities.password.UsersPassword;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@ -18,8 +19,14 @@ public class UsersServiceImpl implements UsersService {
}
@Override
public void deleteUsersById(Long id) {
usersRepository.deleteById(id);
public void deleteUsersById(Long userID) {
usersRepository.deleteById(userID);
this.deletePasswordById(userID);
}
@Override
public void deleteUsersByName(String name) {
usersRepository.deleteByName(name);
}
@Override
@ -33,12 +40,27 @@ public class UsersServiceImpl implements UsersService {
}
@Override
public Optional<Users> findById(Long id) {
return usersRepository.findById(id);
public Optional<Users> findById(Long userID) {
return usersRepository.findById(userID);
}
@Override
public Users findByName(String name) {
return usersRepository.findByName(name);
}
@Override
public void addPassword(UsersPassword usersPassword) {
usersRepository.addPassword(usersPassword);
}
@Override
public void updatePassword(String password, Long userID) {
usersRepository.updatePassword(password, userID);
}
@Override
public void deletePasswordById(Long userID) {
usersRepository.deletePasswordById(userID);
}
}