在当今信息爆炸的时代,美食爱好者面临着信息分散、质量参差不齐的困扰。传统的美食信息获取方式往往需要通过多个平台切换,信息更新不及时且缺乏统一标准。针对这一痛点,我们设计并实现了一个基于SSH框架的企业级美食信息管理平台,通过技术手段整合多源数据,为用户提供精准、高效的美食发现体验。
系统架构与技术栈
该平台采用经典的SSH(Struts2 + Spring + Hibernate)三层架构,实现了清晰的责任分离和高效的开发协作。表示层使用Struts2框架处理用户请求和页面渲染,业务逻辑层由Spring框架统一管理,数据持久层则基于Hibernate实现对象关系映射。
技术栈配置示例:
<!-- Spring配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/food_db"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- Hibernate SessionFactory配置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/food/entity/Dish.hbm.xml</value>
<value>com/food/entity/Restaurant.hbm.xml</value>
</list>
</property>
</bean>
数据库设计亮点
商品表设计优化
t_goods表作为核心业务表,其设计体现了多重优化考虑:
CREATE TABLE `t_goods` (
`goods_id` int(11) NOT NULL COMMENT '商品ID',
`goods_name` varchar(50) DEFAULT NULL COMMENT '商品名称',
`goods_miaoshu` varchar(4000) DEFAULT NULL COMMENT '商品描述',
`goods_pic` varchar(50) DEFAULT NULL COMMENT '商品图片',
`goods_jiage` int(11) DEFAULT NULL COMMENT '商品价格',
`goods_catelog_id` int(11) DEFAULT NULL COMMENT '商品分类ID',
`goods_canguan_id` int(11) DEFAULT NULL COMMENT '商品餐馆ID',
`goods_del` varchar(50) DEFAULT NULL COMMENT '删除标记',
PRIMARY KEY (`goods_id`),
KEY `idx_catelog` (`goods_catelog_id`),
KEY `idx_restaurant` (`goods_canguan_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci COMMENT='商品表'
设计亮点分析:
- 字段类型精准匹配:
goods_miaoshu字段使用varchar(4000)充分容纳详细描述,而goods_pic采用varchar(50)存储图片路径,符合实际使用场景 - 索引优化:为
goods_catelog_id和goods_canguan_id建立索引,显著提升分类查询和餐厅关联查询性能 - 软删除设计:
goods_del字段实现逻辑删除,避免物理删除导致的数据丢失风险
用户表设计规范
t_user表的设计体现了企业级系统的严谨性:
CREATE TABLE `t_user` (
`user_id` int(11) NOT NULL COMMENT '用户ID',
`user_name` varchar(50) DEFAULT NULL COMMENT '用户名',
`user_pw` varchar(55) DEFAULT NULL COMMENT '用户密码',
`user_realname` varchar(50) DEFAULT NULL COMMENT '用户真实姓名',
`user_sex` varchar(50) DEFAULT NULL COMMENT '用户性别',
`user_address` varchar(50) DEFAULT NULL COMMENT '用户地址',
`user_tel` varchar(50) DEFAULT NULL COMMENT '用户电话',
`user_del` varchar(50) DEFAULT NULL COMMENT '删除标记',
PRIMARY KEY (`user_id`),
UNIQUE KEY `uk_username` (`user_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci COMMENT='用户表'
安全性与完整性保障:
- 用户名唯一约束防止重复注册
- 密码字段长度55位为加密存储预留空间
- 完整的用户信息字段支持个性化服务

核心功能实现
1. 智能美食检索系统
平台实现了多条件组合检索功能,用户可根据菜系、价格区间、评分等多维度进行精准筛选。
核心检索Action实现:
public class DishSearchAction extends ActionSupport {
private String keyword;
private Integer minPrice;
private Integer maxPrice;
private String category;
private List<Dish> dishList;
private DishService dishService;
public String execute() {
try {
// 构建复杂查询条件
DishQuery query = new DishQuery();
if (StringUtils.isNotBlank(keyword)) {
query.setKeyword(keyword.trim());
}
if (minPrice != null && maxPrice != null) {
query.setPriceRange(new PriceRange(minPrice, maxPrice));
}
if (StringUtils.isNotBlank(category)) {
query.setCategory(category);
}
dishList = dishService.searchDishes(query);
return SUCCESS;
} catch (Exception e) {
addActionError("检索过程中出现错误");
return ERROR;
}
}
// Getter和Setter方法
public String getKeyword() { return keyword; }
public void setKeyword(String keyword) { this.keyword = keyword; }
// 其他getter/setter...
}
HQL查询实现:
@Repository
public class DishDaoImpl extends HibernateDaoSupport implements DishDao {
public List<Dish> searchByQuery(DishQuery query) {
StringBuffer hql = new StringBuffer("from Dish d where 1=1");
Map<String, Object> params = new HashMap<>();
if (StringUtils.isNotBlank(query.getKeyword())) {
hql.append(" and (d.name like :keyword or d.description like :keyword)");
params.put("keyword", "%" + query.getKeyword() + "%");
}
if (query.getPriceRange() != null) {
hql.append(" and d.price between :minPrice and :maxPrice");
params.put("minPrice", query.getPriceRange().getMin());
params.put("maxPrice", query.getPriceRange().getMax());
}
if (StringUtils.isNotBlank(query.getCategory())) {
hql.append(" and d.category.id = :categoryId");
params.put("categoryId", query.getCategory());
}
hql.append(" and d.deleted = false order by d.createTime desc");
Query hibernateQuery = getSession().createQuery(hql.toString());
for (Map.Entry<String, Object> entry : params.entrySet()) {
hibernateQuery.setParameter(entry.getKey(), entry.getValue());
}
return hibernateQuery.list();
}
}

2. 餐厅关联管理系统
系统实现了餐厅与菜品的多对多关系管理,支持复杂的业务逻辑处理。
餐厅管理Service层实现:
@Service
@Transactional
public class RestaurantServiceImpl implements RestaurantService {
@Autowired
private RestaurantDao restaurantDao;
@Autowired
private DishDao dishDao;
public void addRestaurantWithDishes(Restaurant restaurant, List<Dish> dishes) {
try {
// 保存餐厅基本信息
restaurantDao.save(restaurant);
// 批量处理关联菜品
for (Dish dish : dishes) {
dish.setRestaurant(restaurant);
dishDao.save(dish);
}
// 记录操作日志
logService.logOperation("新增餐厅:" + restaurant.getName());
} catch (Exception e) {
// 事务回滚
throw new BusinessException("餐厅添加失败", e);
}
}
@Transactional(readOnly = true)
public Pagination<Restaurant> getRestaurantsByPage(int pageNo, int pageSize) {
long totalCount = restaurantDao.getTotalCount();
List<Restaurant> restaurants = restaurantDao.findByPage(pageNo, pageSize);
return new Pagination<>(pageNo, pageSize, totalCount, restaurants);
}
}

3. 公告信息发布系统
平台提供了完整的公告管理功能,支持富文本编辑和定时发布。
公告实体模型设计:
@Entity
@Table(name = "t_gonggao")
public class Gonggao implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "gonggao_id")
private Integer id;
@Column(name = "gonggao_title", length = 50)
private String title;
@Column(name = "gonggao_content", length = 2555)
private String content;
@Column(name = "gonggao_data", length = 50)
private String publishDate;
// 业务逻辑方法
public boolean isPublished() {
return publishDate != null && !publishDate.isEmpty();
}
public String getShortContent() {
if (content == null || content.length() <= 100) {
return content;
}
return content.substring(0, 100) + "...";
}
// Getter和Setter方法
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
// 其他getter/setter...
}
公告管理Action实现:
public class GonggaoAction extends ActionSupport {
private Gonggao gonggao;
private List<Gonggao> gonggaoList;
private File attachment;
private String attachmentFileName;
@Autowired
private GonggaoService gonggaoService;
public String list() {
gonggaoList = gonggaoService.getAllPublishedGonggaos();
return SUCCESS;
}
public String save() {
try {
if (gonggao.getId() == null) {
gonggao.setPublishDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
}
gonggaoService.saveGonggao(gonggao);
addActionMessage("公告保存成功");
return SUCCESS;
} catch (Exception e) {
addActionError("公告保存失败:" + e.getMessage());
return ERROR;
}
}
}

4. 用户权限管理系统
系统实现了基于角色的访问控制,确保不同用户具有相应的操作权限。
用户登录验证逻辑:
@Service
public class UserServiceImpl implements UserService {
public User login(String username, String password) {
if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
throw new ValidationException("用户名和密码不能为空");
}
User user = userDao.findByUsername(username);
if (user == null) {
throw new AuthenticationException("用户不存在");
}
if (!passwordEncoder.matches(password, user.getPassword())) {
throw new AuthenticationException("密码错误");
}
if ("true".equals(user.getDeleted())) {
throw new AuthenticationException("用户已被禁用");
}
// 记录登录日志
loginLogService.recordLogin(user.getId());
return user;
}
}

实体模型设计
系统采用面向对象的设计思想,通过Hibernate映射实现对象关系管理。
菜品实体映射配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.food.entity.Dish" table="t_goods">
<id name="id" column="goods_id">
<generator class="identity"/>
</id>
<property name="name" column="goods_name" length="50"/>
<property name="description" column="goods_miaoshu" length="4000"/>
<property name="imagePath" column="goods_pic" length="50"/>
<property name="price" column="goods_jiage"/>
<property name="deleted" column="goods_del" length="50"/>
<many-to-one name="category" column="goods_catelog_id"
class="com.food.entity.Category" lazy="false"/>
<many-to-one name="restaurant" column="goods_canguan_id"
class="com.food.entity.Restaurant" lazy="false"/>
</class>
</hibernate-mapping>
功能展望与优化
基于当前系统架构,未来可从以下几个方向进行深度优化:
1. 引入Redis缓存层
现状分析:当前系统频繁查询菜品分类、餐厅信息等静态数据,数据库压力较大。
优化方案:
@Service
public class CacheableDishService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Cacheable(value = "dishes", key = "#categoryId + '_' + #pageNo")
public List<Dish> getDishesByCategory(Integer categoryId, int pageNo) {
// 数据库查询逻辑
return dishDao.findByCategory(categoryId, pageNo);
}
@CacheEvict(value = "dishes", allEntries = true)
public void updateDish(Dish dish) {
dishDao.update(dish);
}
}
2. 微服务架构改造
架构重构:将单体应用拆分为用户服务、菜品服务、搜索服务等微服务模块。
服务拆分示例:
# Spring Cloud配置
services:
user-service:
port: 8081
dependencies: [mysql, redis]
dish-service:
port: 8082
dependencies: [mysql, elasticsearch]
search-service:
port: 8083
dependencies: [elasticsearch, redis]
3. 智能推荐引擎集成
功能扩展:基于用户行为数据实现个性化推荐。
推荐算法集成:
@Service
public class RecommendationService {
public List<Dish> getPersonalizedRecommendations(Integer userId) {
// 协同过滤算法实现
List<SimilarUser> similarUsers = findSimilarUsers(userId);
return generateRecommendations(similarUsers);
}
private List<SimilarUser> findSimilarUsers(Integer userId) {
// 基于用户评分行为计算相似度
return collaborativeFiltering.calculateSimilarities(userId);
}
}
4. 移动端适配与PWA支持
技术升级:开发响应式界面,支持PWA离线访问。
Service Worker配置:
// sw.js - Service Worker实现
self.addEventListener('fetch', (event) => {
if (event.request.url.includes('/api/dishes')) {
event.respondWith(
caches.match(event.request)
.then(response => response || fetch(event.request))
);
}
});
5. 实时消息通知系统
功能增强:实现WebSocket实时通信,支持新菜品通知、订单状态更新等。
WebSocket配置:
@ServerEndpoint("/notifications")
@Component
public class NotificationEndpoint {
@OnOpen
public void onOpen(Session session) {
// 用户连接处理
}
@OnMessage
public void onMessage(String message, Session session) {
// 消息处理逻辑
}
}
总结
该美食信息管理平台通过SSH框架的有机结合,构建了一个稳定可靠的企业级应用系统。数据库设计体现了良好的规范性和扩展性,核心功能模块实现了完整的业务闭环。系统架构清晰,代码结构规范,为后续的功能扩展和性能优化奠定了坚实基础。
通过引入缓存机制、微服务改造、智能推荐等优化方案,系统有望在性能、用户体验和业务价值方面实现质的飞跃。当前实现已经充分证明了SSH框架在构建复杂企业应用方面的成熟性和可靠性,为同类项目的开发提供了有价值的参考范例。
