宠物领养与饲养交流平台技术解析
项目背景与意义
随着城市化进程加快和人们生活水平提高,宠物已经成为许多家庭的重要成员。然而,宠物领养信息不对称、饲养经验交流渠道匮乏等问题日益突出。传统宠物领养主要依靠线下救助站和口口相传,存在信息传播范围有限、审核流程不规范等痛点。饲养者在遇到宠物健康或行为问题时,也缺乏专业的交流平台寻求帮助。
本平台采用成熟的SSH(Struts2 + Spring + Hibernate)框架技术栈,构建了一个功能完善的宠物领养与饲养交流社区。系统不仅实现了宠物领养的信息化管理,还提供了丰富的社区互动功能,为爱宠人士打造了一个专业、可信赖的线上交流空间。
系统架构与技术栈
系统采用经典的三层架构设计,各层职责分明,耦合度低,便于维护和扩展。
表现层基于Struts2框架实现,通过配置Action类处理前端请求,利用拦截器机制进行统一的权限验证和数据校验。Struts2的标签库和模板机制使得页面开发更加高效。
// 用户登录Action示例
public class UserLoginAction extends ActionSupport {
private String username;
private String password;
private UserService userService;
public String execute() {
try {
User user = userService.login(username, password);
if (user != null) {
ActionContext.getContext().getSession().put("currentUser", user);
return SUCCESS;
} else {
addActionError("用户名或密码错误");
return ERROR;
}
} catch (Exception e) {
addActionError("系统错误,请稍后重试");
return ERROR;
}
}
// getter和setter方法
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
// 其他getter/setter...
}
业务逻辑层由Spring框架的IoC容器管理,通过依赖注入方式解耦各服务组件。Spring的声明式事务管理确保了数据操作的一致性。
<!-- Spring配置示例 -->
<bean id="userService" class="com.petplatform.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
<property name="adoptionService" ref="adoptionService"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
数据持久层基于Hibernate实现,通过对象关系映射将Java实体类与数据库表关联。Hibernate的缓存机制和延迟加载特性提升了系统性能。
// Hibernate实体映射示例
@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "username", nullable = false, length = 50)
private String username;
@Column(name = "password", nullable = false, length = 100)
private String password;
@Column(name = "email", length = 100)
private String email;
@Column(name = "phone", length = 20)
private String phone;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Set<AdoptionApplication> adoptionApplications = new HashSet<>();
// 其他字段和方法...
}
数据库设计亮点
主题表设计分析
t_zhuti表作为社区交流的核心表,设计上体现了对论坛功能的深度思考:
CREATE TABLE `t_zhuti` (
`id` int(11) NOT NULL COMMENT 'ID',
`title` varchar(255) DEFAULT NULL COMMENT '标题',
`content` varchar(5000) DEFAULT NULL COMMENT '内容',
`fujian` varchar(255) DEFAULT NULL COMMENT '附件',
`fujianYuanshiming` varchar(255) DEFAULT NULL COMMENT '附件原名',
`shijian` varchar(255) DEFAULT NULL COMMENT '时间',
`userid` int(11) DEFAULT NULL COMMENT '用户ID',
`catelog_id` int(11) NOT NULL COMMENT '分类ID',
`shifouding` varchar(255) DEFAULT NULL COMMENT '是否置顶',
`shifoujing` varchar(255) DEFAULT NULL COMMENT '是否精华',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci COMMENT='主题表'
设计亮点分析:
- 内容字段优化:
content字段采用varchar(5000)类型,既保证了长文本的存储需求,又避免了使用text类型可能带来的性能问题。 - 附件管理:设计了
fujian和fujianYuanshiming两个字段,分别存储服务器文件名和原始文件名,便于文件管理和用户下载。 - 分类与标识:通过
catelog_id实现主题分类,shifouding和shifoujing字段支持内容运营,提升社区内容质量。
宠物秀点赞表设计
t_show_star表的设计体现了对社交互动功能的精细考量:
CREATE TABLE `t_show_star` (
`star_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '点赞id',
`star_show` int(11) NOT NULL COMMENT '点赞的宠物秀的id',
`star_user` int(11) NOT NULL COMMENT '点赞宠物秀的用户Id',
PRIMARY KEY (`star_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci COMMENT='宠物秀点赞表'
设计优化建议:
- 唯一性约束:建议增加联合唯一索引,防止用户重复点赞:
ALTER TABLE t_show_star ADD UNIQUE KEY uk_show_user (star_show, star_user); - 外键约束:虽然InnoDB支持外键,但在高并发场景下可能影响性能,需要在业务层保证数据一致性。
寄养表业务设计
t_care表的设计涵盖了寄养业务的完整流程:
CREATE TABLE `t_care` (
`care_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`care_desc` varchar(120) NOT NULL COMMENT '寄养备注',
`care_time` int(11) NOT NULL COMMENT '寄养时间 单位天',
`care_user` int(11) NOT NULL COMMENT '寄养人的ID',
`care_state` int(11) NOT NULL DEFAULT 0 COMMENT '寄养的状态 0 表示未通过 1 表示通过',
`care_phone` varchar(255) NOT NULL COMMENT '寄养人的电话号码',
`care_start_date` date NOT NULL COMMENT '寄养开始时间',
`care_price` decimal(7,2) NOT NULL COMMENT '寄养价格',
PRIMARY KEY (`care_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci COMMENT='寄养表'
业务逻辑实现:
// 寄养服务实现
@Service
public class CareServiceImpl implements CareService {
@Autowired
private CareDao careDao;
@Transactional
public boolean applyForCare(CareApplication careApp) {
try {
// 验证寄养时间合理性
if (careApp.getCareTime() <= 0) {
throw new IllegalArgumentException("寄养时间必须大于0");
}
// 验证开始日期不能早于当前日期
if (careApp.getCareStartDate().before(new Date())) {
throw new IllegalArgumentException("寄养开始日期不能早于当前日期");
}
// 保存寄养申请
careDao.save(careApp);
return true;
} catch (Exception e) {
// 记录日志
logger.error("寄养申请保存失败", e);
throw new RuntimeException("寄养申请失败");
}
}
}
核心功能实现
宠物领养管理功能
领养功能是平台的核心业务,通过t_lingyang表记录领养申请信息:
CREATE TABLE `t_lingyang` (
`id` int(11) NOT NULL COMMENT 'ID',
`xingming` varchar(255) DEFAULT NULL COMMENT '姓名',
`zhuzhi` varchar(3000) DEFAULT NULL COMMENT '住址',
`lianxi` varchar(255) DEFAULT NULL COMMENT '联系方式',
`jieshao` varchar(255) DEFAULT NULL COMMENT '介绍',
`chongwuId` int(11) DEFAULT NULL COMMENT '宠物ID',
`user_id` int(11) DEFAULT NULL COMMENT '用户ID',
`huifu` varchar(255) DEFAULT NULL COMMENT '回复',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci COMMENT='领养表'
领养申请业务流程实现:
// 领养服务核心代码
@Service
public class AdoptionServiceImpl implements AdoptionService {
@Autowired
private AdoptionDao adoptionDao;
@Autowired
private PetDao petDao;
@Autowired
private UserDao userDao;
@Transactional
public AdoptionResult applyForAdoption(AdoptionApplication application) {
// 验证宠物是否可领养
Pet pet = petDao.findById(application.getPetId());
if (pet == null || !pet.isAvailableForAdoption()) {
return AdoptionResult.failure("该宠物不可领养");
}
// 验证用户是否已有待处理的领养申请
List<AdoptionApplication> pendingApplications =
adoptionDao.findPendingApplicationsByUserId(application.getUserId());
if (pendingApplications.size() >= 3) {
return AdoptionResult.failure("您已有3个待处理的领养申请,请等待审核");
}
// 保存领养申请
application.setApplyTime(new Date());
application.setStatus(AdoptionStatus.PENDING);
adoptionDao.save(application);
// 更新宠物状态
pet.setAdoptionStatus(PetAdoptionStatus.UNDER_REVIEW);
petDao.update(pet);
return AdoptionResult.success("领养申请提交成功,请等待审核");
}
@Transactional
public void processAdoptionApplication(int applicationId, boolean approved, String reply) {
AdoptionApplication application = adoptionDao.findById(applicationId);
Pet pet = petDao.findById(application.getPetId());
if (approved) {
application.setStatus(AdoptionStatus.APPROVED);
pet.setAdoptionStatus(PetAdoptionStatus.ADOPTED);
// 发送领养成功通知
sendAdoptionSuccessNotification(application.getUserId(), pet);
} else {
application.setStatus(AdoptionStatus.REJECTED);
pet.setAdoptionStatus(PetAdoptionStatus.AVAILABLE);
}
application.setReply(reply);
application.setProcessTime(new Date());
adoptionDao.update(application);
petDao.update(pet);
}
}

社区交流论坛功能
论坛模块基于t_zhuti表实现,支持主题发布、分类浏览、置顶精华等操作:
// 论坛主题服务实现
@Service
public class ForumServiceImpl implements ForumService {
@Autowired
private ThemeDao themeDao;
@Autowired
private UserDao userDao;
public Page<Theme> getThemesByCatalog(int catalogId, int page, int size) {
// 构建查询条件
DetachedCriteria criteria = DetachedCriteria.forClass(Theme.class);
criteria.add(Restrictions.eq("catalogId", catalogId));
criteria.addOrder(Order.desc("isTop")); // 置顶主题优先
criteria.addOrder(Order.desc("isElite")); // 精华主题次之
criteria.addOrder(Order.desc("createTime")); // 最后按时间排序
return themeDao.findByPage(criteria, page, size);
}
@Transactional
public void postTheme(Theme theme) {
// 内容安全检查
if (!contentSecurityCheck(theme.getContent())) {
throw new SecurityException("内容包含违规信息");
}
// 设置主题属性
theme.setCreateTime(new Date());
theme.setIsTop("0");
theme.setIsElite("0");
theme.setViewCount(0);
theme.setReplyCount(0);
themeDao.save(theme);
// 更新用户发帖数
User user = userDao.findById(theme.getUserId());
user.setPostCount(user.getPostCount() + 1);
userDao.update(user);
}
private boolean contentSecurityCheck(String content) {
// 实现内容安全检测逻辑
List<String> forbiddenWords = Arrays.asList("违规词1", "违规词2");
for (String word : forbiddenWords) {
if (content.contains(word)) {
return false;
}
}
return true;
}
}

宠物秀社交功能
宠物秀功能通过t_show和t_show_star表实现图片分享和点赞互动:
// 宠物秀服务实现
@Service
public class PetShowServiceImpl implements PetShowService {
@Autowired
private ShowDao showDao;
@Autowired
private ShowStarDao starDao;
@Transactional
public void publishShow(PetShow show) {
// 图片文件处理
String fileName = processImageFile(show.getImageFile());
show.setFileName(fileName);
show.setPublishTime(new Date());
show.setStarCount(0);
showDao.save(show);
}
@Transactional
public boolean toggleStar(int showId, int userId) {
// 检查是否已点赞
ShowStar existingStar = starDao.findByShowAndUser(showId, userId);
if (existingStar != null) {
// 取消点赞
starDao.delete(existingStar);
updateShowStarCount(showId, -1);
return false;
} else {
// 添加点赞
ShowStar newStar = new ShowStar();
newStar.setStarShow(showId);
newStar.setStarUser(userId);
starDao.save(newStar);
updateShowStarCount(showId, 1);
return true;
}
}
private void updateShowStarCount(int showId, int delta) {
PetShow show = showDao.findById(showId);
show.setStarCount(show.getStarCount() + delta);
showDao.update(show);
}
}
寄养服务管理
寄养服务通过t_care表实现宠物临时寄养的业务流程:
// 寄养服务业务逻辑
@Service
public class CareServiceImpl implements CareService {
@Autowired
private CareDao careDao;
@Transactional
public CareApplication applyForCare(CareApplication application) {
// 参数验证
validateCareApplication(application);
// 设置申请状态
application.setCareState(0); // 0-未审核
application.setApplyTime(new Date());
// 保存申请
careDao.save(application);
return application;
}
@Transactional
public void auditCareApplication(int careId, boolean approved, String auditOpinion) {
CareApplication application = careDao.findById(careId);
if (approved) {
application.setCareState(1); // 1-审核通过
// 通知寄养双方
notifyCareParties(application, true);
} else {
application.setCareState(2); // 2-审核不通过
application.setAuditOpinion(auditOpinion);
notifyCareParties(application, false);
}
application.setAuditTime(new Date());
careDao.update(application);
}
private void validateCareApplication(CareApplication application) {
if (application.getCareTime() <= 0) {
throw new IllegalArgumentException("寄养时间必须大于0");
}
if (application.getCarePrice().compareTo(BigDecimal.ZERO) <= 0) {
throw new IllegalArgumentException("寄养价格必须大于0");
}
// 验证开始日期合理性
if (application.getCareStartDate().before(new Date())) {
throw new IllegalArgumentException("寄养开始日期不能早于当前日期");
}
}
}

实体模型设计
系统采用面向对象的设计思想,通过Hibernate实现对象关系映射:
// 核心实体关系设计
@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username;
private String password;
private String email;
private String phone;
// 一对多关系:用户与领养申请
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<AdoptionApplication> adoptionApplications = new HashSet<>();
// 一对多关系:用户与发布的主题
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Theme> themes = new HashSet<>();
// 一对多关系:用户与宠物秀
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch