Users相关功能重构及增加
This commit is contained in:
parent
6f29019615
commit
97b22ee98a
@ -1,11 +1,33 @@
|
|||||||
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 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);
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ import java.util.List;
|
|||||||
public class Users {
|
public class Users {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Integer id;//主键,用户ID
|
private Long id;//主键,用户ID
|
||||||
private String name;//用户姓名
|
private String name;//用户姓名
|
||||||
private String sex;//用户性别
|
private String sex;//用户性别
|
||||||
@Column(name = "phoneNumber")
|
@Column(name = "phoneNumber")
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.example.springdemo.service;
|
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 java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@ -8,13 +9,21 @@ public interface UsersService {
|
|||||||
|
|
||||||
Users addUsers(Users user);
|
Users addUsers(Users user);
|
||||||
|
|
||||||
void deleteUsersById(Long id);
|
void deleteUsersById(Long userID);
|
||||||
|
|
||||||
|
void deleteUsersByName(String name);
|
||||||
|
|
||||||
Users updateUsers(Users user);
|
Users updateUsers(Users user);
|
||||||
|
|
||||||
Iterable<Users> findAllUsers();
|
Iterable<Users> findAllUsers();
|
||||||
|
|
||||||
Optional<Users> findById(Long id);
|
Optional<Users> findById(Long userID);
|
||||||
|
|
||||||
Users findByName(String name);
|
Users findByName(String name);
|
||||||
|
|
||||||
|
void addPassword(UsersPassword usersPassword);
|
||||||
|
|
||||||
|
void updatePassword(String password, Long userID);
|
||||||
|
|
||||||
|
void deletePasswordById(Long userID);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.example.springdemo.service;
|
|||||||
|
|
||||||
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 jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@ -18,8 +19,14 @@ public class UsersServiceImpl implements UsersService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteUsersById(Long id) {
|
public void deleteUsersById(Long userID) {
|
||||||
usersRepository.deleteById(id);
|
usersRepository.deleteById(userID);
|
||||||
|
this.deletePasswordById(userID);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteUsersByName(String name) {
|
||||||
|
usersRepository.deleteByName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -33,12 +40,27 @@ public class UsersServiceImpl implements UsersService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Users> findById(Long id) {
|
public Optional<Users> findById(Long userID) {
|
||||||
return usersRepository.findById(id);
|
return usersRepository.findById(userID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Users findByName(String name) {
|
public Users findByName(String name) {
|
||||||
return usersRepository.findByName(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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user