答案是一个基于Spring Boot的在线问卷系统,涵盖用户角色、问卷管理、多种题型支持、答卷存储及统计展示功能。
开发一个在线问卷调查系统在Java中是一个典型的Web应用项目,涉及前后端交互、数据库设计、用户权限管理等多个方面。下面从需求分析到核心代码实现,带你一步步完成这个系统的搭建。
一个基本的在线问卷调查系统应包含以下核心功能:
使用主流Java Web技术栈构建系统:
建议采用MVC分层结构: Controller处理请求,Service封装业务逻辑,DAO操作数据库。
关键表结构如下:
-- 问卷表 CREATE TABLE survey ( id BIGINT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(200), description TEXT, created_by BIGINT, status TINYINT DEFAULT 0, -- 0未发布 1已发布 created_time DATETIME );-- 题目表 CREATE TABLE question ( id BIGINT PRIMARY KEY AUTO_INCREMENT, survey_id BIGINT, content TEXT, type TINYINT, -- 1:单选 2:多选 3:填空 4:评分 sort_order INT );
-- 选项表 CREATE TABLE option_item ( id BIGINT PRIMARY KEY AUTO_INCREMENT, question_id BIGINT, content VARCHAR(500), sort_order INT );
-- 用户答卷表 CREATE TABLE answer_sheet ( id BIGINT PRIMARY KEY AUTO_INCREMENT, survey_id BIGINT, user_id BIGINT, submit_time DATETIME );
-- 答案记录表 CREATE TABLE answer ( id BIGINT PRIMARY KEY AUTO_INCREMENT, sheet_id BIGINT, question_id BIGINT, text_value VARCHAR(1000), -- 填空内容 option_ids VARCHAR(100) -- 多选可用逗号分隔存储ID );
以“创建问卷”为例,展示Spring Boot中的实现方式:
实体类 Survey.java
@Entity
@Table(name = "survey")
public class Survey {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String description;
private Integer status;
private LocalDateTime createdTime;
// getter/setter
}Controller 层接口
@RestController
@RequestMapping("/api/survey")
public class SurveyController {
@Autowired
private SurveyService surveyService;
@PostMapping
public ResponseEntity createSurvey(@RequestBody SurveyDTO dto) {
Survey saved = surveyService.create(dto);
return ResponseEntity.ok(saved);
}
} Service 层逻辑
@Service
public class SurveyService {
@Autowired
private SurveyRepository surveyRepo;
@Transactional
public Survey create(SurveyDTO dto) {
Survey survey = new Survey();
survey.setTitle(dto.getTitle());
survey.setDescription(dto.getDescription());
survey.setStatus(0);
survey.setCreatedTime(LocalDateTime.now());
Survey saved = surveyRepo.save(survey);
// 保存题目和选项
for (QuestionDTO q : dto.getQuestions()) {
Question question = new Question();
question.setSurveyId(saved.getId());
question.setContent(q.getContent());
question.setType(q.getType());
question.setSortOrder(q.getOrder());
questionRepo.save(question);
if (q.getOptions() != null) {
for (OptionDTO
o : q.getOptions()) {
OptionItem item = new OptionItem();
item.setQuestionId(question.getId());
item.setContent(o.getContent());
optionRepo.save(item);
}
}
}
return saved;
}
}前端可使用Vue.js动态渲染问卷:
对于统计功能,后端提供聚合接口:
```java @GetMapping("/{id}/stats") public Map