Compare commits

..

3 Commits

Author SHA1 Message Date
myh
ed65fd0b36 测试逻辑优化 2024-01-04 15:22:56 +08:00
myh
aab93e0141 增加、删除用户时的逻辑优化 2024-01-04 15:22:48 +08:00
myh
dbf5e8760f 删除无用文件 2024-01-04 14:43:07 +08:00
8 changed files with 25 additions and 73 deletions

View File

@ -22,10 +22,9 @@ public class UsersController {
//添加新用户
@PreAuthorize("hasRole('管理员') or hasAuthority('添加用户')")
@PostMapping("/add")
public Users addUsers(@RequestBody @NotNull wrapperUserAndPassword wrapperUserAndPassword) {
return usersService.addUser(
wrapperUserAndPassword.user,
wrapperUserAndPassword.userPassword);
public Users addUsers(@RequestBody @NotNull Users user,
@RequestParam(name = "password") @NotNull String password) {
return usersService.addUser(user, password);
}
@Data

View File

@ -10,15 +10,15 @@ public interface UsersPasswordRepository
extends JpaRepository<UsersPassword, Long> {
//修改密码
@Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.users.id = ?2")
@Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.user.id = ?2")
int updatePassword(String password, Long userID);
//删除账户时删除密码
void deleteByUsersId(Long userID);
void deleteByUserId(Long userID);
//根据用户ID查找密码
UsersPassword findByUsersId(Long userID);
UsersPassword findByUserId(Long userID);
//根据用户名查找密码
UsersPassword findByUsersName(String userName);
UsersPassword findByUserName(String userName);
}

View File

@ -18,7 +18,12 @@ public class UsersPassword {
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "usersId", referencedColumnName = "id")
private Users users;
private Users user;
private String password;
public UsersPassword(Users user, String password) {
this.user = user;
this.password = password;
}
}

View File

@ -9,7 +9,7 @@ import java.util.Optional;
public interface UsersService extends UserDetailsService {
Users addUser(Users user, UsersPassword userPassword);
Users addUser(Users user, String password);
void deleteUserById(Long userId);

View File

@ -15,7 +15,6 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -25,7 +24,6 @@ import java.util.List;
import java.util.Optional;
@Service
@Component
public class UsersServiceImpl implements UsersService {
@Resource
private UsersRepository usersRepository;
@ -39,8 +37,9 @@ public class UsersServiceImpl implements UsersService {
@Override
@Transactional(rollbackFor = Exception.class)
//添加用户时同步添加密码
public Users addUser(Users user, UsersPassword userPassword) {
public Users addUser(Users user, String password) {
Users u = usersRepository.save(user);
UsersPassword userPassword = new UsersPassword(u, password);
usersPasswordRepository.save(userPassword);
return u;
}
@ -49,8 +48,8 @@ public class UsersServiceImpl implements UsersService {
@Transactional(rollbackFor = Exception.class)
//删除用户时同步删除密码
public void deleteUserById(Long userId) {
usersPasswordRepository.deleteByUserId(userId);
usersRepository.deleteById(userId);
usersPasswordRepository.deleteByUsersId(userId);
}
@Override
@ -64,7 +63,7 @@ public class UsersServiceImpl implements UsersService {
//获取用户ID
userId = usersRepository.findByName(name).get().getId();
//删除密码
usersPasswordRepository.deleteByUsersId(userId);
usersPasswordRepository.deleteByUserId(userId);
}
}
@ -109,7 +108,7 @@ public class UsersServiceImpl implements UsersService {
throw new UsernameNotFoundException("用户名不存在");
}
// 取出密码
String password = usersPasswordRepository.findByUsersId(user.getId()).getPassword();
String password = usersPasswordRepository.findByUserId(user.getId()).getPassword();
// 用户角色
Roles role = user.getRoles();

View File

@ -1,51 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- &lt;!&ndash; 连接数据库的基本参数 &ndash;&gt;-->
<!-- <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>-->
<!-- <property name="hibernate.connection.url">jdbc:sqlserver://intpointer.com:1433;\-->
<!-- databaseName=Elm;\-->
<!-- encrypt=true;\-->
<!-- trustServerCertificate=true;\-->
<!-- loginTimeout=30;-->
<!-- </property>-->
<!-- <property name="hibernate.connection.username">myh</property>-->
<!-- <property name="hibernate.connection.password">20231103#MS_Sql</property>-->
<!-- &lt;!&ndash; 配置Hibernate的方言 &ndash;&gt;-->
<!-- <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>-->
<!-- &lt;!&ndash; 可选配置================ &ndash;&gt;-->
<!-- &lt;!&ndash; 打印SQL &ndash;&gt;-->
<!-- <property name="hibernate.show_sql">true</property>-->
<!-- &lt;!&ndash; 格式化SQL &ndash;&gt;-->
<!-- <property name="hibernate.format_sql">true</property>-->
<!-- &lt;!&ndash; 自动创建表 &ndash;&gt;-->
<!-- <property name="hibernate.hbm2ddl.auto">update</property>-->
<!-- &lt;!&ndash; 配置C3P0连接池 &ndash;&gt;-->
<!-- <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>-->
<!-- &lt;!&ndash;在连接池中可用的数据库连接的最少数目 &ndash;&gt;-->
<!-- <property name="c3p0.min_size">5</property>-->
<!-- &lt;!&ndash;在连接池中所有数据库连接的最大数目 &ndash;&gt;-->
<!-- <property name="c3p0.max_size">20</property>-->
<!-- &lt;!&ndash;设定数据库连接的过期时间,以秒为单位,-->
<!-- 如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 &ndash;&gt;-->
<!-- <property name="c3p0.timeout">120</property>-->
<!-- &lt;!&ndash;每3000秒检查所有连接池中的空闲连接 以秒为单位&ndash;&gt;-->
<!-- <property name="c3p0.idle_test_period">3000</property>-->
<!-- &lt;!&ndash; 设置事务隔离级别 &ndash;&gt;-->
<!-- <property name="hibernate.connection.isolation">4</property>-->
<!-- &lt;!&ndash; 配置当前线程绑定的Session &ndash;&gt;-->
<!-- <property name="hibernate.current_session_context_class">thread</property>-->
<!-- 引入映射 -->
<!-- <mapping resource="com/itheima/hibernate/domain/Customer.hbm.xml"/>
<mapping resource="com/itheima/hibernate/domain/LinkMan.hbm.xml"/> -->
<!-- <mapping resource="./"/>-->
<!-- <mapping resource="./"/>-->
</session-factory>
</hibernate-configuration>

View File

@ -103,7 +103,7 @@ class UsersControllerTests {
user.setAddress("test address");
UsersPassword userPassword = new UsersPassword();
userPassword.setPassword("test1234567");
userPassword.setUsers(user);
userPassword.setUser(user);
UsersController.wrapperUserAndPassword usersAndPassword = new UsersController.wrapperUserAndPassword();
usersAndPassword.setUser(user);
@ -139,7 +139,7 @@ class UsersControllerTests {
user.setAddress("test address");
UsersPassword userPassword = new UsersPassword();
userPassword.setPassword("test1234567");
userPassword.setUsers(user);
userPassword.setUser(user);
// Convert to JSON
ObjectMapper mapper = new ObjectMapper();

View File

@ -61,7 +61,7 @@ class UsersServiceTests {
user1.setAddress("test address user1");
user1Password = new UsersPassword();
user1Password.setUsers(user1);
user1Password.setUser(user1);
user1Password.setPassword("TestPassword1");
user2 = new Users();
@ -71,7 +71,7 @@ class UsersServiceTests {
user2.setAddress("test address user2");
user2Password = new UsersPassword();
user2Password.setUsers(user2);
user2Password.setUser(user2);
user2Password.setPassword("TestPassword2");
usersList.add(user1);
@ -99,14 +99,14 @@ class UsersServiceTests {
@Rollback(value = true)
void testSaveUser() {
when(usersRepository.save(any())).thenReturn(user1);
usersService.addUser(user1, user1Password);
usersService.addUser(user1, user1Password.getPassword());
verify(usersRepository, times(1)).save(any());
}
@Test
@Rollback(value = true)
void testGetAllUsers() {
usersService.addUser(user1, user1Password);
usersService.addUser(user1, user1Password.getPassword());
when(usersRepository.findAll()).thenReturn(usersList);
List<Users> usersList1 = usersService.findAllUsers();
Assertions.assertEquals(usersList1, usersList);