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/CDCDaoImpl.java

93 lines
3.7 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.CDCDao; // 数据访问对象DAO接口
import domain.CDC; // 表示 CDC 表数据结构的实体类
import org.springframework.dao.DataAccessException; // 数据访问异常类
import org.springframework.jdbc.core.BeanPropertyRowMapper; // 用于将结果集映射为实体类
import org.springframework.jdbc.core.JdbcTemplate; // Spring 的 JDBC 模板类
import utils.JDBCUtils; // 自定义的工具类,用于获取数据源
import java.util.List; // Java 的 List 接口
/**
* CDCDaoImpl 实现了 CDCDao 接口,负责与数据库交互以获取 CDC 数据表的信息。
*/
public class CDCDaoImpl implements CDCDao {
// 使用 JdbcTemplate 进行数据库操作,通过 JDBCUtils 获取数据源
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
/**
* 查询所有学院名称(去重)
* @return 包含所有学院名称的 CDC 对象列表;查询失败返回 null
*/
@Override
public List<CDC> findAllCollege() {
try {
// SQL 语句:获取去重的学院名称
String sql = "select distinct college from college_department_class";
// 使用 JdbcTemplate 执行查询,并将结果映射为 CDC 对象
List<CDC> cdcs = template.query(sql, new BeanPropertyRowMapper<CDC>(CDC.class));
return cdcs; // 返回查询结果
} catch (DataAccessException e) {
e.printStackTrace(); // 打印异常堆栈信息
return null; // 查询失败时返回 null
}
}
/**
* 查询所有系名称(去重)
* @return 包含所有系名称的 CDC 对象列表;查询失败返回 null
*/
@Override
public List<CDC> findAllDepartment() {
try {
// SQL 语句:获取去重的系名称
String sql = "select distinct department from college_department_class";
// 使用 JdbcTemplate 执行查询,并将结果映射为 CDC 对象
List<CDC> cdcs = template.query(sql, new BeanPropertyRowMapper<CDC>(CDC.class));
return cdcs; // 返回查询结果
} catch (DataAccessException e) {
e.printStackTrace(); // 打印异常堆栈信息
return null; // 查询失败时返回 null
}
}
/**
* 查询所有班级名称(去重)
* @return 包含所有班级名称的 CDC 对象列表;查询失败返回 null
*/
@Override
public List<CDC> findAllClass() {
try {
// SQL 语句:获取去重的班级名称
String sql = "select distinct cclass from college_department_class";
// 使用 JdbcTemplate 执行查询,并将结果映射为 CDC 对象
List<CDC> cdcs = template.query(sql, new BeanPropertyRowMapper<CDC>(CDC.class));
return cdcs; // 返回查询结果
} catch (DataAccessException e) {
e.printStackTrace(); // 打印异常堆栈信息
return null; // 查询失败时返回 null
}
}
/**
* 查询表中的所有数据
* @return 包含所有数据的 CDC 对象列表;查询失败返回 null
*/
@Override
public List<CDC> findAll() {
try {
// SQL 语句:获取表中所有数据
String sql = "select * from college_department_class";
// 使用 JdbcTemplate 执行查询,并将结果映射为 CDC 对象
List<CDC> cdcs = template.query(sql, new BeanPropertyRowMapper<CDC>(CDC.class));
return cdcs; // 返回查询结果
} catch (DataAccessException e) {
e.printStackTrace(); // 打印异常堆栈信息
return null; // 查询失败时返回 null
}
}
}