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

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

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

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

1. 数据库表设计

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

sql 复制代码
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表的数据。

java 复制代码
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

java 复制代码
import java.util.List;

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

MeterReadingMapper.xml

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

java 复制代码
import java.util.List;

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

MeterReadingServiceImpl.java

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MeterReadingServiceImpl implements MeterReadingService {

    @Autowired
    private MeterReadingDao meterReadingDao;

    @Override
    public List<MeterReading> getAllReadings() {
        return meterReadingDao.getAllReadings();
    }

    @Override
    public void addReading(MeterReading reading) {
        meterReadingDao.addReading(reading);
    }
}

5. Controller层

定义Controller层来处理HTTP请求。

MeterReadingController.java

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 {

    @Autowired
    private 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

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

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:

java 复制代码
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 Setters
    public 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 {

    @Autowired
    private MeterReadingDao meterReadingDao;

    @Override
    public List<MeterReading> getAllReadings() {
        return meterReadingDao.getAllReadings();
    }

    @Override
    public 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 {

    @Autowired
    private 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配置文件等),并确保所有依赖项已正确导入项目中。

相关推荐
大G哥22 分钟前
Spring源码分析 - BeanFactoryPostProcessor 的处理
java·后端·网络协议·spring·rpc
大包菜 cc33 分钟前
SpringMVC的Jackson全局空值处理
spring·mvc
杂货铺的小掌柜1 小时前
spring mvc源码学习笔记之四
学习·spring·mvc
Andya3 小时前
SpringBoot | 使用Apache POI库读取Excel文件介绍
spring boot·后端·spring
IManiy6 小时前
自动生成RESTful API——Spring Data Rest
后端·spring·restful
dzend10 小时前
spring cloud-skywalking入门指南
spring·spring cloud·skywalking
Java知识日历14 小时前
【内含例子代码】Spring框架的设计模式应用(第二集)
java·开发语言·后端·spring·设计模式
丁总学Java17 小时前
定义一个名为 MyCache 的 Spring 配置类
java·spring
HUNAG-DA-PAO20 小时前
Spring&SpringBoot常用注解总结
java·spring boot·spring
武昌库里写JAVA20 小时前
迟来的前端面试经验
spring boot·spring·毕业设计·layui·课程设计