@Override public SystemAdminServiceDto login(String username, String password) throws SQLException { //用username查询数据库、判断密码是否正确 SystemAdmin systemAdmin = this.systemAdminDao.findByUsername(username,password); SystemAdminServiceDto systemAdminServiceDto = new SystemAdminServiceDto(); Connection con=JDBCUtil.getConnection(); PreparedStatement preparedStatement=con.prepareStatement("select * from users where username = ? and password=?"); preparedStatement.setString(1,username); preparedStatement.setString(2,password); preparedStatement.execute(); if (systemAdmin == null){ systemAdminServiceDto.setCode((-1)); }else { if (systemAdmin.equals(password)){ systemAdminServiceDto.setCode(0); }else { systemAdminServiceDto.setCode(1); systemAdminServiceDto.setSystemAdmin(systemAdmin); } } // System.out.println(username+password); return systemAdminServiceDto; }优化代码
时间: 2024-03-11 20:47:37 浏览: 19
可以使用以下方式优化代码:
1. 删除不必要的数据库查询代码,因为已经通过 `this.systemAdminDao.findByUsername(username, password)` 方法获取了 `SystemAdmin` 对象。
2. 修改密码比较的方式,使用 `SystemAdmin` 对象的 `getPassword()` 方法获取密码,然后与传入的密码参数进行比较即可。
3. 在 `SystemAdminServiceDto` 类中添加常量 `CODE_SUCCESS`、`CODE_FAIL`,分别表示登录成功和登录失败的状态码,避免使用魔法数字。
4. 修改代码中的注释,保持与实际代码相符。
下面是优化后的代码:
```java
@Override
public SystemAdminServiceDto login(String username, String password) throws SQLException {
// 通过用户名和密码查询数据库,获取 SystemAdmin 对象
SystemAdmin systemAdmin = this.systemAdminDao.findByUsername(username, password);
SystemAdminServiceDto systemAdminServiceDto = new SystemAdminServiceDto();
if (systemAdmin == null) {
// 登录失败,用户名不存在
systemAdminServiceDto.setCode(SystemAdminServiceDto.CODE_FAIL);
} else {
String dbPassword = systemAdmin.getPassword();
if (dbPassword.equals(password)) {
// 登录成功,密码正确
systemAdminServiceDto.setCode(SystemAdminServiceDto.CODE_SUCCESS);
} else {
// 登录失败,密码错误
systemAdminServiceDto.setCode(SystemAdminServiceDto.CODE_FAIL);
systemAdminServiceDto.setSystemAdmin(systemAdmin);
}
}
return systemAdminServiceDto;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)