1. 项目概述与核心价值
这是一个基于Java SSM框架的宿舍管理系统,专为高校宿管部门设计。我在实际部署中发现,这套系统完美解决了传统纸质登记效率低下、数据难以统计的痛点。系统采用经典的Spring+SpringMVC+MyBatis技术栈,前端使用JSP+Bootstrap实现响应式布局,在IDEA开发环境下可一键运行。
提示:系统默认使用MySQL 5.7数据库,建议提前安装好对应版本避免兼容性问题
系统最实用的三个功能模块:
- 学生住宿信息管理(含批量导入导出)
- 宿舍资产报修全流程跟踪
- 晚归登记与违纪记录统计
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 开发环境搭建指南
2.1 基础软件准备
需要预先安装:
- JDK 1.8(注意环境变量配置)
- Apache Tomcat 8.5+
- MySQL 5.7(必须此版本)
- IntelliJ IDEA Ultimate(社区版需额外配置)
2.2 数据库初始化
源码包中的db_dorm.sql需要特别注意字符集设置:
sql复制/* 执行前先创建数据库 */
CREATE DATABASE db_dorm
DEFAULT CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci;
2.3 IDEA项目导入关键步骤
- 选择"Open"而非"Import Project"
- 在pom.xml右键选择"Add as Maven Project"
- 设置Facets(关键!):
- Project Structure → Modules → 添加Web
- 指定webapp目录路径
3. 核心功能实现解析
3.1 多条件复合查询设计
系统采用MyBatis动态SQL实现灵活的宿舍查询:
xml复制<select id="selectByCondition" parameterType="map" resultMap="dormMap">
SELECT * FROM tb_dorm
<where>
<if test="building != null">
AND building_no = #{building}
</if>
<if test="type != null">
AND dorm_type = #{type}
</if>
<if test="status != null">
AND dorm_status = #{status}
</if>
</where>
LIMIT #{start},#{pageSize}
</select>
3.2 报修流程状态机
系统通过枚举类实现报修状态流转:
java复制public enum RepairStatus {
PENDING(0, "待处理"),
ASSIGNED(1, "已派工"),
PROCESSING(2, "维修中"),
COMPLETED(3, "已完成"),
REJECTED(4, "已驳回");
// 省略getter/setter
}
4. 典型问题排查实录
4.1 中文乱码解决方案
遇到表单提交乱码时,按以下步骤检查:
- 确认数据库连接串添加了
?useUnicode=true&characterEncoding=UTF-8 - web.xml中CharacterEncodingFilter配置是否正确
- IDEA的File Encodings设置(重点检查)
- Global Encoding: UTF-8
- Project Encoding: UTF-8
- Default encoding for properties files: UTF-8
4.2 分页插件配置异常
当分页失效时,检查PageHelper的两种配置方式:
properties复制# 方式一:在mybatis-config.xml
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
# 方式二:在applicationContext.xml
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
5. 前端优化实践
5.1 响应式表格增强
原生的Bootstrap表格增加以下特性:
jsp复制<table class="table table-hover table-striped table-bordered"
id="dormTable"
data-toggle="table"
data-pagination="true"
data-search="true"
data-show-columns="true">
<!-- 表头内容 -->
</table>
5.2 使用ECharts实现数据可视化
在统计页面引入宿舍入住率饼图:
javascript复制// 初始化图表
var chart = echarts.init(document.getElementById('chart'));
// 请求数据
$.get('/dorm/occupancyRate', function(data) {
chart.setOption({
series: [{
type: 'pie',
data: [
{value: data.used, name: '已入住'},
{value: data.empty, name: '空床位'}
]
}]
});
});
6. 安全加固建议
6.1 密码加密存储
修改UserServiceImpl中的密码处理逻辑:
java复制// 原MD5加密升级为BCrypt
public void saveUser(User user) {
String encodedPwd = new BCryptPasswordEncoder().encode(user.getPassword());
user.setPassword(encodedPwd);
userMapper.insert(user);
}
6.2 XSS防护方案
在SpringMVC配置中添加:
java复制@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public FilterRegistrationBean<XssFilter> xssFilter() {
FilterRegistrationBean<XssFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new XssFilter());
registration.addUrlPatterns("/*");
return registration;
}
}
7. 二次开发扩展点
7.1 添加微信通知功能
集成微信公众号模板消息:
- 在pom.xml添加依赖:
xml复制<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.1.0</version>
</dependency>
- 创建WxMpService实例:
java复制@Bean
public WxMpService wxMpService() {
WxMpService service = new WxMpServiceImpl();
WxMpConfigStorage config = new WxMpInMemoryConfigStorage();
config.setAppId("your_appid");
config.setSecret("your_secret");
service.setWxMpConfigStorage(config);
return service;
}
7.2 导出Excel优化
使用EasyExcel替代原生POI:
java复制// 控制器方法改造
@GetMapping("/export")
public void exportExcel(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=dorm.xlsx");
List<Dorm> list = dormService.getAll();
EasyExcel.write(response.getOutputStream(), Dorm.class)
.sheet("宿舍数据")
.doWrite(list);
}
8. 性能调优实战
8.1 MyBatis二级缓存配置
在mapper.xml中添加:
xml复制<cache eviction="LRU"
flushInterval="60000"
size="512"
readOnly="true"/>
8.2 连接池参数优化
修改druid配置:
properties复制# 初始连接数
spring.datasource.initialSize=5
# 最小空闲连接
spring.datasource.minIdle=5
# 最大活跃连接
spring.datasource.maxActive=20
# 获取连接超时时间(毫秒)
spring.datasource.maxWait=60000
# 配置间隔多久检测空闲连接(毫秒)
spring.datasource.timeBetweenEvictionRunsMillis=60000
9. 部署上线要点
9.1 外置Tomcat部署
需要特别注意:
- 修改pom.xml打包方式:
xml复制<packaging>war</packaging>
- 排除内嵌Tomcat:
xml复制<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
9.2 日志文件分割
配置logback-spring.xml:
xml复制<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/app.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
10. 项目源码结构解析
核心目录说明:
code复制src/main/java
├── com.dorm.controller # 控制器层
├── com.dorm.entity # 实体类
├── com.dorm.mapper # MyBatis映射接口
├── com.dorm.service # 业务逻辑层
└── com.dorm.config # 配置类
src/main/resources
├── mapper # MyBatis映射文件
├── static # 静态资源
└── application.yml # 主配置文件
特别提醒:系统默认管理员账号admin/123456,首次登录后请立即修改密码。我在实际部署时发现,很多学校会忽略这个基础安全设置,导致系统容易被未授权访问。
