ssm写一个通知公告的代码
时间: 2023-07-25 07:26:47 浏览: 83
首先,需要创建一个通知公告的实体类,例如:
```java
public class Notice {
private Integer id;
private String title;
private String content;
private Date createTime;
// getter和setter方法省略
}
```
接着,需要创建一个数据访问层接口和实现类:
```java
public interface NoticeMapper {
int insert(Notice notice);
int update(Notice notice);
int delete(Integer id);
Notice selectById(Integer id);
List<Notice> selectAll();
}
@Repository
public class NoticeMapperImpl implements NoticeMapper {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int insert(Notice notice) {
String sql = "INSERT INTO notice(title, content, create_time) VALUES (?, ?, ?)";
return jdbcTemplate.update(sql, notice.getTitle(), notice.getContent(), notice.getCreateTime());
}
@Override
public int update(Notice notice) {
String sql = "UPDATE notice SET title=?, content=?, create_time=? WHERE id=?";
return jdbcTemplate.update(sql, notice.getTitle(), notice.getContent(), notice.getCreateTime(), notice.getId());
}
@Override
public int delete(Integer id) {
String sql = "DELETE FROM notice WHERE id=?";
return jdbcTemplate.update(sql, id);
}
@Override
public Notice selectById(Integer id) {
String sql = "SELECT * FROM notice WHERE id=?";
return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Notice.class), id);
}
@Override
public List<Notice> selectAll() {
String sql = "SELECT * FROM notice";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Notice.class));
}
}
```
然后是服务层接口和实现类:
```java
public interface NoticeService {
void addNotice(Notice notice);
void updateNotice(Notice notice);
void deleteNotice(Integer id);
Notice getNoticeById(Integer id);
List<Notice> getAllNotices();
}
@Service
public class NoticeServiceImpl implements NoticeService {
@Autowired
private NoticeMapper noticeMapper;
@Override
public void addNotice(Notice notice) {
noticeMapper.insert(notice);
}
@Override
public void updateNotice(Notice notice) {
noticeMapper.update(notice);
}
@Override
public void deleteNotice(Integer id) {
noticeMapper.delete(id);
}
@Override
public Notice getNoticeById(Integer id) {
return noticeMapper.selectById(id);
}
@Override
public List<Notice> getAllNotices() {
return noticeMapper.selectAll();
}
}
```
最后是控制层的代码,可以使用SpringMVC框架来实现:
```java
@Controller
@RequestMapping("/notice")
public class NoticeController {
@Autowired
private NoticeService noticeService;
@GetMapping("/list")
public String getNoticeList(Model model) {
List<Notice> noticeList = noticeService.getAllNotices();
model.addAttribute("noticeList", noticeList);
return "noticeList";
}
@GetMapping("/add")
public String addNoticePage() {
return "addNotice";
}
@PostMapping("/add")
public String addNotice(Notice notice) {
noticeService.addNotice(notice);
return "redirect:/notice/list";
}
@GetMapping("/edit/{id}")
public String editNoticePage(@PathVariable("id") Integer id, Model model) {
Notice notice = noticeService.getNoticeById(id);
model.addAttribute("notice", notice);
return "editNotice";
}
@PostMapping("/edit")
public String editNotice(Notice notice) {
noticeService.updateNotice(notice);
return "redirect:/notice/list";
}
@GetMapping("/delete/{id}")
public String deleteNotice(@PathVariable("id") Integer id) {
noticeService.deleteNotice(id);
return "redirect:/notice/list";
}
}
```
以上代码实现了通知公告的添加、编辑、删除和查询功能。其中,addNoticePage、addNotice、editNoticePage、editNotice和deleteNotice方法对应了添加、编辑、删除和查询的页面和请求。需要在SpringMVC的配置文件中配置视图解析器和静态资源映射。
阅读全文