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