加入用户密码CRUD操作
This commit is contained in:
parent
b9c8cd892c
commit
0a31998bd3
@ -0,0 +1,17 @@
|
||||
package com.example.springdemo.dao;
|
||||
|
||||
import com.example.springdemo.entities.password.UsersPassword;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UsersPasswordRepository
|
||||
extends JpaRepository<UsersPassword, Long> {
|
||||
@Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.users.id = ?2")
|
||||
void updatePassword(String password, Long userID);
|
||||
|
||||
//删除账户时删除密码
|
||||
@Query("delete from UsersPassword UsersPwd where UsersPwd.users.id = ?1")
|
||||
void deletePasswordById(Long userID);
|
||||
}
|
@ -1,33 +1,13 @@
|
||||
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);
|
||||
}
|
||||
|
@ -3,11 +3,9 @@ package com.example.springdemo.service;
|
||||
import com.example.springdemo.entities.Users;
|
||||
import com.example.springdemo.entities.password.UsersPassword;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UsersService {
|
||||
|
||||
Users addUsers(Users user);
|
||||
Users addUsers(Users user, UsersPassword usersPassword);
|
||||
|
||||
void deleteUsersById(Long userID);
|
||||
|
||||
@ -21,9 +19,5 @@ public interface UsersService {
|
||||
|
||||
Users findByName(String name);
|
||||
|
||||
void addPassword(UsersPassword usersPassword);
|
||||
|
||||
void updatePassword(String password, Long userID);
|
||||
|
||||
void deletePasswordById(Long userID);
|
||||
}
|
||||
|
@ -1,35 +1,55 @@
|
||||
package com.example.springdemo.service;
|
||||
|
||||
import com.example.springdemo.dao.UsersPasswordRepository;
|
||||
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.data.jpa.repository.Modifying;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class UsersServiceImpl implements UsersService {
|
||||
@Resource
|
||||
private UsersRepository usersRepository;
|
||||
|
||||
@Resource
|
||||
private UsersPasswordRepository usersPasswordRepository;
|
||||
|
||||
@Override
|
||||
public Users addUsers(Users user) {
|
||||
return usersRepository.save(user);
|
||||
@Modifying
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
//添加用户时同步添加密码
|
||||
public Users addUsers(Users user, UsersPassword usersPassword) {
|
||||
Users u = usersRepository.save(user);
|
||||
usersPasswordRepository.save(usersPassword);
|
||||
return u;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Modifying
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
//删除用户时同步删除密码
|
||||
public void deleteUsersById(Long userID) {
|
||||
usersRepository.deleteById(userID);
|
||||
this.deletePasswordById(userID);
|
||||
usersPasswordRepository.deletePasswordById(userID);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Modifying
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
//删除用户时同步删除密码
|
||||
public void deleteUsersByName(String name) {
|
||||
usersRepository.deleteByName(name);
|
||||
//获取用户ID
|
||||
Long userID = usersRepository.findByName(name).getId();
|
||||
usersPasswordRepository.deletePasswordById(userID);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Modifying
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Users updateUsers(Users user) {
|
||||
return usersRepository.save(user);
|
||||
}
|
||||
@ -50,17 +70,10 @@ public class UsersServiceImpl implements UsersService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPassword(UsersPassword usersPassword) {
|
||||
usersRepository.addPassword(usersPassword);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Modifying
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
//更新用户密码
|
||||
public void updatePassword(String password, Long userID) {
|
||||
usersRepository.updatePassword(password, userID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deletePasswordById(Long userID) {
|
||||
usersRepository.deletePasswordById(userID);
|
||||
usersPasswordRepository.updatePassword(password, userID);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user