You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
text/src/dao/impl/AdminDaoImpl.java

80 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package dao.impl;
import dao.AdminDao; // 引入 AdminDao 接口,定义管理员相关的数据访问方法
import domain.Admin; // 引入 Admin 实体类,表示管理员数据
import org.springframework.dao.DataAccessException; // 引入数据库访问异常类
import org.springframework.jdbc.core.BeanPropertyRowMapper; // 用于将查询结果映射为 Java 对象
import org.springframework.jdbc.core.JdbcTemplate; // JDBC 操作模板,用于简化数据库操作
import utils.JDBCUtils; // 自定义工具类,用于获取数据库数据源
/**
* AdminDaoImpl 实现类,负责对管理员信息进行数据库操作。
*/
public class AdminDaoImpl implements AdminDao {
// 使用 JdbcTemplate 对数据库进行操作JDBCUtils 提供数据源
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
/**
* 根据管理员 ID 和密码查询管理员信息。
*
* @param id 管理员 ID
* @param password 管理员密码
* @return 如果匹配成功,返回管理员对象;否则返回 null
*/
@Override
public Admin findAdminidAndPassword(String id, String password) {
try {
// SQL 查询语句:根据管理员 ID 和密码查询管理员
String sql = "select * from admin where a_id = ? and a_password = ?";
// 执行查询,并将结果映射为 Admin 对象
Admin admin = template.queryForObject(sql, new BeanPropertyRowMapper<>(Admin.class), id, password);
return admin; // 返回找到的管理员
} catch (DataAccessException e) {
// 捕获数据库访问异常并打印堆栈信息
e.printStackTrace();
return null; // 查询失败时返回 null
}
}
/**
* 更新管理员密码。
*
* @param adminid 管理员 ID
* @param newpassword 新密码
*/
@Override
public void updatePassword(String adminid, String newpassword) {
try {
// SQL 更新语句:根据管理员 ID 更新密码
String sql = "update admin set a_password=? where a_id=?";
// 执行更新操作
template.update(sql, newpassword, adminid);
} catch (Exception e) {
// 捕获异常并打印堆栈信息
e.printStackTrace();
}
}
/**
* 根据管理员 ID 查询管理员信息。
*
* @param id 管理员 ID
* @return 如果存在,返回管理员对象;否则返回 null
*/
@Override
public Admin findAdminById(String id) {
try {
// SQL 查询语句:根据管理员 ID 查询管理员
String sql = "select * from admin where a_id = ?";
// 执行查询,并将结果映射为 Admin 对象
Admin admin = template.queryForObject(sql, new BeanPropertyRowMapper<>(Admin.class), id);
return admin; // 返回找到的管理员
} catch (DataAccessException e) {
// 捕获数据库访问异常并打印堆栈信息
e.printStackTrace();
return null; // 查询失败时返回 null
}
}
}