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

简介: 《学生教务系统》之城市管理板块实现🍅 Java学习路线:搬砖工的Java学习路线🍅 作者:程序员小王🍅 程序员小王的博客:https://www.wolai.com/wnaghengjie/ahNwvAUPG2Hb1Sy7Z8waaF🍅 扫描主页左侧二维码,加我微信 一起学习、一起进步🍅 欢迎点赞 👍 收藏 ⭐留言 📝🍅 如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去其糟粕

一、城市管理实现前提

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


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

0.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、城市管理页面实现效果图

1.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>


下一个功能是: 标签功能模块的实现代码


📌 作者:王恒杰

❌ 勘误: 无

📜 声明: 由于作者水平有限,本文有错误和不准确之处在所难免,本人也很想知道这些错误,恳望读者批评指正!

🍅 欢迎点赞 👍 收藏 ⭐留言 📝



相关文章
|
资源调度 Apache 流计算
Yarn命令详细介绍
Yarn命令详细介绍
1163 2
|
缓存 网络协议 网络性能优化
UDP实现可靠传输
UDP实现可靠传输
|
6月前
|
安全 Java 数据建模
【Java基础】JDK17:密封类、模式匹配、Record类(附《思维导图》+《面试高频考点清单》)
Java 17作为LTS版本,重磅引入密封类、模式匹配与Record类三大特性:Record简化不可变数据建模,密封类精准控制继承边界,模式匹配(instanceof+switch)提升类型安全与代码简洁性。三者协同可优雅实现代数数据类型,标志着Java迈向更安全、简洁、表达力更强的现代编程语言。
【Java基础】JDK17:密封类、模式匹配、Record类(附《思维导图》+《面试高频考点清单》)
|
5月前
|
人工智能 缓存 API
别再为 AI 调用超支头疼:Credits 配额,让每一笔消耗都透明可控
Credits 统一度量上线,消费者用量可度量、可约束。
739 30
|
5月前
|
SQL 人工智能 BI
正当红的 Context Layer 到底是什么?
给 AI 更多上下文并不是目的,让 AI 在更小、更可信的范围里把事情做对,才是。
|
5月前
|
JSON 缓存 Rust
这周我蹲了一下 Headroom,它真把我那个烧钱的 Agent 给救回来了
Headroom 是一款开源本地上下文压缩工具,专为 LLM agent 降本增效设计。通过内容智能路由(JSON/日志/代码/文本分治)、多模态专用压缩器及可逆压缩(CCR),实测最高节省 92% token,精度不降反升。支持 Wrap/Proxy/Library/MCP 四种零侵入接入方式,数据不出本地,合规友好。(239字)
476 0
|
存储
阿里云轻量应用服务器收费标准价格表:200Mbps带宽、CPU内存及存储配置详解
阿里云香港轻量应用服务器,200Mbps带宽,免备案,支持多IP及国际线路,月租25元起,年付享8.5折优惠,适用于网站、应用等多种场景。
4207 0
|
安全 搜索推荐 大数据
Dataphin资产上下架审批管理——让企业数据资产“可控、可追溯、高效流转”
Dataphin推出资产上下架审批管理功能,通过灵活分层审批、自定义流程模板及对接企业OA系统,实现数据资产全生命周期的统一管控与高效流转,提升数据安全与运营效率。
502 0
|
前端开发 安全 UED
【项目实战】从终端到浏览器:实现 ANSI 字体在前端页面的彩色展示
在学习和工作中,我们经常需要使用日志来记录程序的运行状态和调试信息。而为了更好地区分不同的日志等级,我们可以使用不同的颜色来呈现,使其更加醒目和易于阅读。 在下图运行结果中,我们使用了 colorlog 库来实现彩色日志输出。通过定义不同日志等级对应的颜色,我们可以在控制台中以彩色的方式显示日志信息。例如,DEBUG 级别的日志使用白色,INFO 级别的日志使用绿色,WARNING 级别的日志使用黄色,ERROR 级别的日志使用红色,CRITICAL 级别的日志使用蓝色。
|
12月前
|
人工智能 算法 机器人
AI场景面试题
本项目聚焦AI客服与智能分析,集成Ollama、MaxKB与百度云千帆大模型,实现RAG问答、健康分析等功能。采用Spring AI框架,通过API调用7B/14B级大模型,结合本地部署与云端资源,优化数据同步、模型微调与推理流程,提升响应准确率。涵盖AIGC应用、图像处理心得及DDPM等算法实践,构建高效、可扩展的智能服务体系。