一、在本文中,我们将通过一个实际的例子来了解如何使用 Spring MVC 架构来构建一个简单的广告管理系统。这个系统包括以下几个功能模块:
- 广告信息的增删改查(CRUD)
- 广告分类的管理
- 广告搜索功能
- 项目结构图
二、DAO层解析
在DAO层,我们定义了一个接口AdvDaoProxy
,用注解方式用于操作数据库中的广告信息。以下是关键方法及其功能:
package com.fs.dao;
import com.fs.pojo.AdvCategory;
import com.fs.pojo.AdvInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.time.chrono.AbstractChronology;
import java.util.List;
/**
* @Version:1.0
* @Description: TODO(一句话描述该类的功能)
* @Date: 2024/8/31 10:37
* @Author: tao
*/
@Repository
public interface AdvDaoProxy {
@Select("select * from advinfo")
@Results(value = {
@Result(id = true, column = "advId", property = "advId"),
@Result(id = true, column = "advTitle", property = "advTitle"),
@Result(id = true, column = "advContent", property = "advContent"),
@Result(id = true, column = "clickCount", property = "clickCount"),
@Result(id = true, column = "expiredTime", property = "expiredTime"),
@Result(id = true, column = "categoryId", property = "categoryId"),
@Result(id = true, column = "remark", property = "remark"),
@Result(id = true, column = "categoryId", property = "advCategory",
one = @One(select = "com.fs.dao.AdvDaoProxy.getAdvCategoryById"))
}, id = "adInfoMap")
List<AdvInfo> queryALL();
// 查询单个页面
@Select("select * from advinfo where advId=#{advId}")
@ResultMap("adInfoMap")
AdvInfo queryAdvOne(@Param("advId") int advId);
// 将AdvCategory传AdvInfo.advCategory
@Select("select * from advcategory where categoryId=#{categoryId}")
AdvCategory getAdvCategoryById(int categoryId);
//新增信息
@Insert("insert into advinfo(categoryId,advTitle,expiredTime,advContent,remark) values(#{categoryId},#{advTitle},#{expiredTime},#{advContent},#{remark})")
int addAdvInfo(AdvInfo advInfo);
//修改广告
@Update("update advinfo set advTitle=#{advTitle},expiredTime=#{expiredTime},categoryId=#{categoryId},remark=#{remark} " +
"where advId=#{advId}")
int updateAdvInfo(AdvInfo advInfo);
// 通过name查categoryId
@Select("select categoryId from advcategory where categoryName=#{name}")
Integer queryIdByName(@Param("name") String name);
// 删除该广告
@Delete("delete from advinfo where advId=#{advId}")
int delAdvMation(int advId);
// 查询所有商品分类
@Select("select categoryName from advcategory")
List<String> queryAllName();
// 通过id查名字
@Select("select categoryName from advcategory where categoryId=#{categoryId}")
String queryNameByid(@Param("categoryId") int categoryId);
// 实现搜索功能:通过categoryName,title查询categoryId
@Select("select categoryId from advcategory where categoryName=#{name} ")
Integer queryIdByNaTle(@Param("name") String name);
// categoryId,来搜索具体广告信息
@Select("select * from advinfo where categoryId=#{categoryId} and advTitle like #{title}")
@ResultMap("adInfoMap")
List<AdvInfo> queryAdvByCaId(@Param("categoryId") int categoryId,@Param("title") String title);
}
三、Controller 层
Controller 层是 Spring MVC 的核心部分,它负责处理 HTTP 请求并将它们转发到相应的业务逻辑层进行处理。在我们的项目中,我们只有一个控制器类 AdvHandler
,它包含了所有与广告管理相关的路由和处理方法。
@Controller
@RequestMapping("/adv")
public class AdvHandler {
// 注入服务层的依赖
@Autowired
private AdvService advService;
// 处理广告列表页面的请求
@RequestMapping("/list")
public String list(Model model) {
List<AdvInfo> advInfos = advService.listAll();
model.addAttribute("advInfos", advInfos);
return "adv/list";
}
// 处理添加新广告的请求
@RequestMapping("/add")
public String add(@ModelAttribute("adv") AdvInfo advInfo) {
advService.save(advInfo);
return "redirect:/adv/list";
}
// 处理编辑广告的请求
@RequestMapping("/edit/{id}")
public String edit(@PathVariable("id")