一、《学生教务系统》之城市管理板块实现

简介: 《学生教务系统》之城市管理板块实现

《学生教务系统》之城市管理板块实现

一、城市管理实现前提

注意:在实现城市功能之前我们可以参考一下:《学生教务系统》立项需求说明书


(1)城市管理实现思路图


image.png

2、城市管理数据库设计(t_city)


image.png

注意:其中Studnet表中有t_city,所以我们建表时最好把城市表放在第一个建的表


create table t_city(
  id int primary key auto_increment ,
  name varchar(20) not null  unique
)

3、城市管理页面实现效果图


image.png

二、查询,删除,添加功能实现

(1)实体类(bean类)

public class City implements Serializable {
    private Integer cid;
    private String cname;

(2)dao接口

public interface CityDao{
    /**
     * 添加城市
     * @param city
     */
    public void insertCity(City city);
    /**
     * 删除城市
     * @param id
     */
    public void deleteCity(Integer id);
    /**
     * 查询所有
     * @return
     */
    public List<City> selectCity();
}

(3)Mybatis实现Dao接口

<mapper namespace="com.tjcu.dao.CityDao">
    <!--动态SQL语句-->
    <resultMap id="SelectCity" type="city">
        <id property="cid" column="c_id"></id>
        <result property="cname" column="c_name"></result>
    </resultMap>
    <!--添加城市-->
    <insert id="insertCity">
        insert into t_city
        values (#{cid}, #{cname})
    </insert>
    <!--删除城市-->
    <delete id="deleteCity">
        delete
        from t_city
        where c_id = #{id}
    </delete>
    <!--查询所有-->
    <select id="selectCity" resultMap="SelectCity">
        select c_id, c_name
        from t_city
    </select>
</mapper>

(4)Service接口和Service的实现类

Service接口

public interface CityService {
    /**
     * 添加城市
     * @param city
     */
    public void insertCity(City city);
    /**
     * 删除城市
     * @param id
     */
    public void deleteCity(Integer id);
    /**
     * 查询所有
     * @return
     */
    public List<City> selectCity();
}

Service实现

public class CityServiceImpl implements CityService {
    @Override
    public void insertCity(City city) {
        CityDao mapper = (CityDao) MybatisUtil.getMapper(CityDao.class);
        mapper.insertCity(city);
        MybatisUtil.commit();
    }
    @Override
    public void deleteCity(Integer id) {
        CityDao mapper = (CityDao) MybatisUtil.getMapper(CityDao.class);
        mapper.deleteCity(id);
        MybatisUtil.commit();
    }
    @Override
    public List<City> selectCity() {
        CityDao mapper = (CityDao) MybatisUtil.getMapper(CityDao.class);
        List<City> cities = mapper.selectCity();
        MybatisUtil.close();
        return cities;
    }

(5)Struts2实现Controller层

public class CityAction extends ActionSupport {
    private City city;
    private  List<City> cities;
    //添加城市
    public String insertCity() {
        CityService cityService = new CityServiceImpl();
        city.setCid(null);
        cityService.insertCity(city);
       return  SUCCESS;
    }
    //删除城市
    public String deleteCity() {
        CityService cityService = new CityServiceImpl();
        cityService.deleteCity(city.getCid());
        return  SUCCESS;
    }
   //展示成
    public String selectCity() {
        CityService cityService = new CityServiceImpl();
         cities = cityService.selectCity();
        return SUCCESS;
    }
    public City getCity() {
        return city;
    }
    public void setCity(City city) {
        this.city = city;
    }
    public List<City> getCities() {
        return cities;
    }
    public void setCities(List<City> cities) {
        this.cities = cities;
    }

(6)测试类

public class CityTest {
    @Test
    public void insert(){
        CityService cityService = new CityServiceImpl();
        City city = new City(null, "天津");
        cityService.insertCity(city);
    }
    @Test
    public void delete(){
        CityService cityService = new CityServiceImpl();
        cityService.deleteCity(2);
    }
    @Test
    public void select(){
        CityService cityService = new CityServiceImpl();
        List<City> cities = cityService.selectCity();
        for (City city : cities) {
            System.out.println(city);
        }
    }

(7)Web.xml中struts2过滤器配置(过滤器只需要配置,后续模块功能不再配置)

  <filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

(8)Struts核心配置

<!--城市-->
    <package name="city" namespace="/city" extends="struts-default">
        <!--添加城市-->
        <action name="addCity" method="insertCity" class="com.tjcu.action.CityAction">
            <result name="success" type="redirectAction">showCity</result>
        </action>
        <!--删除城市-->
        <action name="dropCity" method="deleteCity" class="com.tjcu.action.CityAction">
            <result name="success" type="redirectAction">showCity</result>
        </action>
        <!--展示城市-->
        <action name="showCity" method="selectCity" class="com.tjcu.action.CityAction">
            <result name="success" type="dispatcher">/city/showCity.jsp</result>
        </action>
    </package>

(9)JSP页面

展示(删除)城市页面

<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html lang="en">
<head>
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/statics/css/bootstrap.min.css">
    <title>城市列表</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12">
            <input type="button" class="btn btn-success btn-sm" value="添加城市" onclick="location.href='addCity.jsp'"/>
        </div>
    </div>
    <div class="row" style="margin-top: 15px;">
        <div class="col-sm-12">
            <table class="table table-striped table-bordered table-hover">
                <tr>
                    <th>编号</th>
                    <th>名称</th>
                    <th>操作</th>
                </tr>
                <c:forEach var="city" items="${requestScope.cities}" varStatus="c">
                    <tr>
                        <%--使用jstl+EL进行排序--%>
                        <td>${c.count}</td>
                        <td>${city.cname}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/city/dropCity?city.cid=${city.cid}" class="btn btn-danger btn-sm">删除</a>
                        </td>
                    </tr>
                </c:forEach>
            </table>
        </div>
    </div>
</div>
</body>
</html>

添加城市页面

<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" isELIgnored="false" %>
<html lang="en">
<head>
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="../statics/css/bootstrap.min.css">
    <title>添加城市</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12">
            <form action="${pageContext.request.contextPath}/city/addCity" method="post">
                <div class="form-group">
                    <label>
                        城市名称:
                    </label>
                    <input type="text" class="form-control" name="city.cname"/>
                </div>
                <input type="submit" class="btn btn-success btn-block btn-sm" value="提交"/>
                <input type="button" class="btn btn-info btn-block btn-sm" value="返回上级" onclick="history.go(-1);"/>
            </form>
        </div>
    </div>
</div>
</body>
</html>
相关文章
|
9天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1197 4
|
8天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1114 87
|
6天前
|
机器学习/深度学习 物联网
Wan2.2再次开源数字人:Animate-14B!一键实现电影角色替换和动作驱动
今天,通义万相的视频生成模型又又又开源了!Wan2.2系列模型家族新增数字人成员Wan2.2-Animate-14B。
569 11
|
18天前
|
人工智能 运维 安全
|
8天前
|
云栖大会
阿里云云栖大会2025年9月24日开启,免费申请大会门票,速度领取~
2025云栖大会将于9月24-26日举行,官网免费预约畅享票,审核后短信通知,持证件入场
1680 12
|
1天前
|
资源调度
除了nrm-pm,还有哪些工具可以管理多个包管理器的源?
除了nrm-pm,还有哪些工具可以管理多个包管理器的源?
212 127
|
9天前
|
弹性计算 Kubernetes jenkins
如何在 ECS/EKS 集群中有效使用 Jenkins
本文探讨了如何将 Jenkins 与 AWS ECS 和 EKS 集群集成,以构建高效、灵活且具备自动扩缩容能力的 CI/CD 流水线,提升软件交付效率并优化资源成本。
344 0
|
9天前
|
消息中间件 Java Apache
SpringBoot集成RocketMq
RocketMQ 是一款开源的分布式消息中间件,采用纯 Java 编写,支持事务消息、顺序消息、批量消息、定时消息及消息回溯等功能。其优势包括去除对 ZooKeeper 的依赖、支持异步和同步刷盘、高吞吐量及消息过滤等特性。RocketMQ 具备高可用性和高可靠性,适用于大规模分布式系统,能有效保障消息传输的一致性和顺序性。
493 2

热门文章

最新文章