【论文+源码】一个基于SSM(Spring + Spring MVC + MyBatis)的公寓电能计量系统

embedded/2025/1/7 20:59:22/

请添加图片描述

为了实现一个基于SSM(Spring + Spring MVC + MyBatis)的公寓电能计量系统,我们需要创建一个简单的Web应用程序来记录和显示每个公寓的电能使用情况。以下是一个基本的实现示例。

我们将包括以下几个部分:

  1. 数据库表设计
  2. 实体类
  3. DAO层接口及MyBatis映射文件
  4. Service层接口及其实现类
  5. Controller层
  6. 前端页面

1. 数据库表设计

假设我们有一个名为electricity_metering的数据库,其中包含一个meter_reading表:

CREATE TABLE meter_reading (id INT AUTO_INCREMENT PRIMARY KEY,apartment_id INT NOT NULL,reading_date DATE NOT NULL,energy_usage DECIMAL(10, 2) NOT NULL
);

2. 实体类

创建一个Java实体类来表示meter_reading表的数据。

public class MeterReading {private Integer id;private Integer apartmentId;private Date readingDate;private BigDecimal energyUsage;// Getters and Setters
}

3. DAO层接口及MyBatis映射文件

定义DAO层接口以及对应的XML映射文件。

MeterReadingDao.java

import java.util.List;public interface MeterReadingDao {List<MeterReading> getAllReadings();void addReading(MeterReading reading);
}

MeterReadingMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.MeterReadingDao"><select id="getAllReadings" resultType="com.example.model.MeterReading">SELECT * FROM meter_reading</select><insert id="addReading" parameterType="com.example.model.MeterReading">INSERT INTO meter_reading (apartment_id, reading_date, energy_usage)VALUES (#{apartmentId}, #{readingDate}, #{energyUsage})</insert>
</mapper>

4. Service层接口及其实现类

定义Service层接口及其实现类。

MeterReadingService.java

import java.util.List;public interface MeterReadingService {List<MeterReading> getAllReadings();void addReading(MeterReading reading);
}

MeterReadingServiceImpl.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class MeterReadingServiceImpl implements MeterReadingService {@Autowiredprivate MeterReadingDao meterReadingDao;@Overridepublic List<MeterReading> getAllReadings() {return meterReadingDao.getAllReadings();}@Overridepublic void addReading(MeterReading reading) {meterReadingDao.addReading(reading);}
}

5. Controller层

定义Controller层来处理HTTP请求。

MeterReadingController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;import java.util.Date;@Controller
public class MeterReadingController {@Autowiredprivate MeterReadingService meterReadingService;@GetMapping("/readings")public String showReadings(Model model) {model.addAttribute("readings", meterReadingService.getAllReadings());return "readings";}@PostMapping("/addReading")public String addReading(@RequestParam int apartmentId, @RequestParam String readingDateStr, @RequestParam double energyUsage) throws Exception {MeterReading reading = new MeterReading();reading.setApartmentId(apartmentId);reading.setReadingDate(new Date(readingDateStr));reading.setEnergyUsage(BigDecimal.valueOf(energyUsage));meterReadingService.addReading(reading);return "redirect:/readings";}
}

6. 前端页面

最后,创建一些JSP页面来展示数据并允许用户输入新的读数。

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Apartment Electricity Metering System</title>
</head>
<body>
<h1>Welcome to Apartment Electricity Metering System</h1>
<a href="/readings">View Readings</a>
<br/>
<form action="/addReading" method="post">Apartment ID: <input type="number" name="apartmentId"><br/>Reading Date: <input type="date" name="readingDateStr"><br/>Energy Usage: <input type="number" step="0.01" name="energyUsage"><br/><input type="submit" value="Add Reading">
</form>
</body>
</html>

readings.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>Meter Readings</title>
</head>
<body>
<h1>Meter Readings</h1>
<table border="1"><tr><th>ID</th><th>Apartment ID</th><th>Reading Date</th><th>Energy Usage</th></tr><c:forEach var="reading" items="${readings}"><tr><td>${reading.id}</td><td>${reading.apartmentId}</td><td>${reading.readingDate}</td><td>${reading.energyUsage}</td></tr></c:forEach>
</table>
<a href="/">Back to Home</a>
</body>
</html>

以上代码构成了一套完整的基于SSM框架的公寓电能计量系统的后端与前端基础功能。你可以在此基础上进一步扩展和完善系统功能,比如添加用户认证、细化权限管理、增加统计分析等功能。

以下是整合后的完整代码artifact:

package com.example.model;import java.math.BigDecimal;
import java.util.Date;public class MeterReading {private Integer id;private Integer apartmentId;private Date readingDate;private BigDecimal energyUsage;// Getters and Setterspublic Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public Integer getApartmentId() {return apartmentId;}public void setApartmentId(Integer apartmentId) {this.apartmentId = apartmentId;}public Date getReadingDate() {return readingDate;}public void setReadingDate(Date readingDate) {this.readingDate = readingDate;}public BigDecimal getEnergyUsage() {return energyUsage;}public void setEnergyUsage(BigDecimal energyUsage) {this.energyUsage = energyUsage;}
}package com.example.dao;import com.example.model.MeterReading;import java.util.List;public interface MeterReadingDao {List<MeterReading> getAllReadings();void addReading(MeterReading reading);
}<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.MeterReadingDao"><select id="getAllReadings" resultType="com.example.model.MeterReading">SELECT * FROM meter_reading</select><insert id="addReading" parameterType="com.example.model.MeterReading">INSERT INTO meter_reading (apartment_id, reading_date, energy_usage)VALUES (#{apartmentId}, #{readingDate}, #{energyUsage})</insert>
</mapper>package com.example.service;import com.example.model.MeterReading;import java.util.List;public interface MeterReadingService {List<MeterReading> getAllReadings();void addReading(MeterReading reading);
}package com.example.service.impl;import com.example.dao.MeterReadingDao;
import com.example.model.MeterReading;
import com.example.service.MeterReadingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class MeterReadingServiceImpl implements MeterReadingService {@Autowiredprivate MeterReadingDao meterReadingDao;@Overridepublic List<MeterReading> getAllReadings() {return meterReadingDao.getAllReadings();}@Overridepublic void addReading(MeterReading reading) {meterReadingDao.addReading(reading);}
}package com.example.controller;import com.example.model.MeterReading;
import com.example.service.MeterReadingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;@Controller
public class MeterReadingController {@Autowiredprivate MeterReadingService meterReadingService;@GetMapping("/readings")public String showReadings(Model model) {model.addAttribute("readings", meterReadingService.getAllReadings());return "readings";}@PostMapping("/addReading")public String addReading(@RequestParam int apartmentId, @RequestParam String readingDateStr, @RequestParam double energyUsage) throws ParseException {MeterReading reading = new MeterReading();reading.setApartmentId(apartmentId);reading.setReadingDate(new SimpleDateFormat("yyyy-MM-dd").parse(readingDateStr));reading.setEnergyUsage(BigDecimal.valueOf(energyUsage));meterReadingService.addReading(reading);return "redirect:/readings";}
}<!-- index.jsp -->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Apartment Electricity Metering System</title>
</head>
<body>
<h1>Welcome to Apartment Electricity Metering System</h1>
<a href="/readings">View Readings</a>
<br/>
<form action="/addReading" method="post">Apartment ID: <input type="number" name="apartmentId"><br/>Reading Date: <input type="date" name="readingDateStr"><br/>Energy Usage: <input type="number" step="0.01" name="energyUsage"><br/><input type="submit" value="Add Reading">
</form>
</body>
</html><!-- readings.jsp -->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>Meter Readings</title>
</head>
<body>
<h1>Meter Readings</h1>
<table border="1"><tr><th>ID</th><th>Apartment ID</th><th>Reading Date</th><th>Energy Usage</th></tr><c:forEach var="reading" items="${readings}"><tr><td>${reading.id}</td><td>${reading.apartmentId}</td><td>${reading.readingDate}</td><td>${reading.energyUsage}</td></tr></c:forEach>
</table>
<a href="/">Back to Home</a>
</body>
</html>

请根据实际情况调整配置文件(如Spring配置文件、MyBatis配置文件等),并确保所有依赖项已正确导入项目中。


http://www.ppmy.cn/embedded/152107.html

相关文章

基于 Python Django 的西西家居全屋定制系统(源码+部署+文档)

博主介绍&#xff1a;✌程序员徐师兄、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;…

软件工程期末整理(二)

快速原型开发模型是&#xff08;适用于客户需求难以清楚定义、规模较小的系统&#xff09;。(编写系统实施计划)不是系统设计阶段的主要活动 解释&#xff1a;系统实施计划”更侧重于后续的实施与部署阶段&#xff0c;属于项目管理层面的内容 协作性不属于构件的特性在类图中…

Python连接和操作Elasticsearch详细指南

Python连接和操作Elasticsearch详细指南 一、服务器端配置1. 修改 Elasticsearch 配置文件2. 开放防火墙端口 二、本地 Python 连接 Elasticsearch1. 连接 Elasticsearch2. 索引操作3. 文档操作4. 搜索内容5. 聚合查询6. 批量操作 三、注意事项四、故障排除结论 Elasticsearch …

XXL-RPC v1.8.1 | RPC服务框架

Release Notes 1、【安全】序列化安全性增强&#xff0c;默认开启package安全空间机制&#xff1b;2、【扩展】序列化扩展性增强&#xff0c;支持自定义序列化package白名单&#xff1b;3、【优化】序列化类型主动检测&#xff0c;提升问题定位效率&#xff1b;4、【能力】服务…

电压控制环与电流控制环

电压控制环和电流控制环是电力电子系统和电机控制中常见的两种控制策略。 一、电压控制环与电流控制环的比较 电压控制环&#xff1a; 特点&#xff1a;电压控制环通常用于稳压应用&#xff0c;通过调整输出电压以维持其稳定在设定值。由于电压是二阶系统&#xff0c;具有滞后…

mysql删除无用用户

1、删除不用的账户 (1) 查看当前已存在账户 mysql> select user,host,password from mysql.user; 或下面的命令 #mysql> sELECT DISTINCT CONCAT(User: ,user,,host,;) AS query FROM mysql.user; --------------------------------------- | query …

将simpletex 识别的公式 复制到ppt 中

1&#xff09;点击 复制MathML(word) 2&#xff09;右击粘贴到任意word 中 3&#xff09;将word公式粘到 office (2019) 的ppt 中 线上识别链接&#xff1a;SimpleTex - Snip & Get!

5. CSS引入方式

5.1 CSS的三种样式 按照 CSS 样式书写的位置(或者引入的方式)&#xff0c;CSS样式表可以分为三大类&#xff1a; 1.行内样式表&#xff08;行内式&#xff09; 2.内部样式表&#xff08;嵌入式&#xff09; 3. 外部样式表&#xff08;链接式&#xff09; 5.2 内部样式表 …