Spring MVC 实战:构建一个简单的广告管理系统

一、在本文中,我们将通过一个实际的例子来了解如何使用 Spring MVC 架构来构建一个简单的广告管理系统。这个系统包括以下几个功能模块:

  1. 广告信息的增删改查(CRUD)
  2. 广告分类的管理
  3. 广告搜索功能
  4. 项目结构图

二、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") 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值