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.
155 lines
5.4 KiB
155 lines
5.4 KiB
package com.student.dao.impl;
|
|
|
|
import com.student.bean.Announcement;
|
|
import com.student.dao.AnnouncementDao;
|
|
import com.student.util.DBUtil;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.Date;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Timestamp;
|
|
import java.sql.Types;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class AnnouncementDaoImpl implements AnnouncementDao {
|
|
|
|
private static final String BASE_SQL =
|
|
"SELECT a.id, a.title, a.content, a.publisher_id, a.is_top, a.expire_date, a.create_time, "
|
|
+ "u.real_name AS publisher_name FROM announcement a "
|
|
+ "LEFT JOIN users u ON a.publisher_id = u.id ";
|
|
|
|
@Override
|
|
public Announcement findById(int id) {
|
|
List<Announcement> list = queryList(BASE_SQL + " WHERE a.id = ?", id);
|
|
return list.isEmpty() ? null : list.get(0);
|
|
}
|
|
|
|
@Override
|
|
public boolean insert(Announcement announcement) {
|
|
String sql = "INSERT INTO announcement (title, content, publisher_id, is_top, expire_date) VALUES (?, ?, ?, ?, ?)";
|
|
return executeUpdate(sql, announcement);
|
|
}
|
|
|
|
@Override
|
|
public boolean update(Announcement announcement) {
|
|
String sql = "UPDATE announcement SET title=?, content=?, publisher_id=?, is_top=?, expire_date=? WHERE id=?";
|
|
Connection conn = null;
|
|
PreparedStatement ps = null;
|
|
try {
|
|
conn = DBUtil.getConnection();
|
|
ps = conn.prepareStatement(sql);
|
|
fillParams(ps, announcement);
|
|
ps.setInt(6, announcement.getId());
|
|
return ps.executeUpdate() > 0;
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException("更新公告失败", e);
|
|
} finally {
|
|
DBUtil.close(conn, ps);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean delete(int id) {
|
|
String sql = "DELETE FROM announcement WHERE id = ?";
|
|
Connection conn = null;
|
|
PreparedStatement ps = null;
|
|
try {
|
|
conn = DBUtil.getConnection();
|
|
ps = conn.prepareStatement(sql);
|
|
ps.setInt(1, id);
|
|
return ps.executeUpdate() > 0;
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException("删除公告失败", e);
|
|
} finally {
|
|
DBUtil.close(conn, ps);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<Announcement> findAll() {
|
|
return queryList(BASE_SQL + " ORDER BY a.is_top DESC, a.create_time DESC");
|
|
}
|
|
|
|
@Override
|
|
public List<Announcement> findActive() {
|
|
return queryList(BASE_SQL + " WHERE (a.expire_date IS NULL OR a.expire_date >= CURDATE()) "
|
|
+ "ORDER BY a.is_top DESC, a.create_time DESC");
|
|
}
|
|
|
|
private boolean executeUpdate(String sql, Announcement announcement) {
|
|
Connection conn = null;
|
|
PreparedStatement ps = null;
|
|
try {
|
|
conn = DBUtil.getConnection();
|
|
ps = conn.prepareStatement(sql);
|
|
fillParams(ps, announcement);
|
|
return ps.executeUpdate() > 0;
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException("保存公告失败", e);
|
|
} finally {
|
|
DBUtil.close(conn, ps);
|
|
}
|
|
}
|
|
|
|
private void fillParams(PreparedStatement ps, Announcement a) throws SQLException {
|
|
ps.setString(1, a.getTitle());
|
|
ps.setString(2, a.getContent());
|
|
if (a.getPublisherId() != null) {
|
|
ps.setInt(3, a.getPublisherId());
|
|
} else {
|
|
ps.setNull(3, Types.INTEGER);
|
|
}
|
|
ps.setInt(4, a.getIsTop() != null ? a.getIsTop() : 0);
|
|
if (a.getExpireDate() != null) {
|
|
ps.setDate(5, new Date(a.getExpireDate().getTime()));
|
|
} else {
|
|
ps.setNull(5, Types.DATE);
|
|
}
|
|
}
|
|
|
|
private List<Announcement> queryList(String sql, Object... params) {
|
|
List<Announcement> list = new ArrayList<>();
|
|
Connection conn = null;
|
|
PreparedStatement ps = null;
|
|
ResultSet rs = null;
|
|
try {
|
|
conn = DBUtil.getConnection();
|
|
ps = conn.prepareStatement(sql);
|
|
for (int i = 0; i < params.length; i++) {
|
|
ps.setObject(i + 1, params[i]);
|
|
}
|
|
rs = ps.executeQuery();
|
|
while (rs.next()) {
|
|
list.add(mapRow(rs));
|
|
}
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException("查询公告失败", e);
|
|
} finally {
|
|
DBUtil.close(conn, ps, rs);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
private Announcement mapRow(ResultSet rs) throws SQLException {
|
|
Announcement a = new Announcement();
|
|
a.setId(rs.getInt("id"));
|
|
a.setTitle(rs.getString("title"));
|
|
a.setContent(rs.getString("content"));
|
|
int publisherId = rs.getInt("publisher_id");
|
|
a.setPublisherId(rs.wasNull() ? null : publisherId);
|
|
a.setIsTop(rs.getInt("is_top"));
|
|
Date expireDate = rs.getDate("expire_date");
|
|
if (expireDate != null) {
|
|
a.setExpireDate(new java.util.Date(expireDate.getTime()));
|
|
}
|
|
Timestamp createTime = rs.getTimestamp("create_time");
|
|
if (createTime != null) {
|
|
a.setCreateTime(new java.util.Date(createTime.getTime()));
|
|
}
|
|
a.setPublisherName(rs.getString("publisher_name"));
|
|
return a;
|
|
}
|
|
}
|
|
|