为了实现一个基于SSM(Spring + Spring MVC + MyBatis)的公寓电能计量系统,我们需要创建一个简单的Web应用程序来记录和显示每个公寓的电能使用情况。以下是一个基本的实现示例。
我们将包括以下几个部分:
- 数据库表设计
- 实体类
- DAO层接口及MyBatis映射文件
- Service层接口及其实现类
- Controller层
- 前端页面
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 {
@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
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
<%@ 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 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配置文件等),并确保所有依赖项已正确导入项目中。