作者主页:源码空间站2022
简介:Java领域优质创作者、Java项目、学习资料、技术互助
文末获取源码
基于Spring Boot的问卷调查系统
项目介绍
> * 本项目的在线问卷调查调查系统是基于SpringBoot开发的,采用了前后端分离模式来开发。
> * 前端开发使用了Vue、Element UI,后端的开发则是使用了SpringBoot、MyBatis技术。
项目配置
> * 下载项目压缩包,在IDEA或者Eclipse软件中打开,并修改**application.yml**文件中数据库的用户和密码
> * 在本地的MySQL数据库中导入项目的**sql文件**
> * 在编译器中运行该项目:此为SpringBoot框架,启动主类Main方法
> * 在浏览器中输入**localhost:8181**后按回车键进入系统
运行截图
相关代码
用户控制器
@Controller
public class UserCtrl {
@Resource
private UserService userService;
@PostMapping(value = "/allUser")
@ResponseBody //获取所有用户
public JSONObject selectAllUsers(){
JSONObject json = new JSONObject();
//获取所有用户信息
List<User> users = userService.selectAllUsers();
//把结果json化
JSONObject userList = (JSONObject) JSON.toJSON(users);
json.put("userlist",userList);
return json;
}
@CrossOrigin
@PostMapping(value = "api/login")
@ResponseBody //用户登录
public JSONObject login(@RequestBody User loginUser){
JSONObject res = new JSONObject();
//获取输入的用户名
String username = loginUser.getUsername();
username = HtmlUtils.htmlEscape(username);
//获取输入的密码
String password = loginUser.getPassword();
password = HtmlUtils.htmlEscape(password);
//在数据库查找
User curUser = userService.findUser(username,password);
if (curUser==null){
//用户名或密码输入错误
res.put("msg","fail");
}else{
//登录成功
res.put("msg","success");
// 把登录用户的id和name传过去
int id = curUser.getId();
String name = curUser.getUsername();
res.put("id",id);
res.put("name",name);
}
return res;
}
@CrossOrigin
@PostMapping(value = "api/register")
@ResponseBody //用户注册
public JSONObject register(@RequestBody User registerUser){
JSONObject data = new JSONObject();
// 获取注册用户的用户名
String username = registerUser.getUsername();
username = HtmlUtils.htmlEscape(username);
if (!"".equals(username)){
User temp = userService.findUserByName(username);//查找用户名是否被占用
if (temp == null){
//可以进行注册
int res = userService.addUser(registerUser);
if (res > 0) // 注册成功
data.put("msg", "success");
else // 注册失败
data.put("msg","fail");
}else {
//用户名不可用
data.put("msg","unusable");
}
}
return data;
}
@CrossOrigin
@PostMapping(value = "api/getName")
@ResponseBody // 获取当前用户的用户名
public JSONObject getName(@RequestBody User user){
JSONObject json = new JSONObject();
int id = user.getId();
String username = userService.getNameById(id);
if (username != null && !"".equals(username))
json.put("username",username);
else
json.put("username","error");
return json;
}
@CrossOrigin
@PostMapping(value = "api/userInfo")
@ResponseBody //获取当前用户信息
public JSONObject getUserInfo(@RequestBody User curUser){
JSONObject json = new JSONObject();
int id = curUser.getId();
//根据id获取当前用户信息
User user = userService.findUserById(id);
//将结果json化
JSONObject userInfo = (JSONObject) JSON.toJSON(user);
json.put("user",userInfo);
return json;
}
@CrossOrigin
@PostMapping(value = "api/resetPass")
@ResponseBody // 重置账号密码
public JSONObject resetPassword(@RequestBody User user){
JSONObject json = new JSONObject();
//获取账号的用户名
String username = user.getUsername();
//获取账号的手机号
String telephone = user.getTelephone();
//获取账号的新密码
String newPass = user.getPassword();
//调用业务逻辑层接口,检查系统中是否存在与用户名对应的账号
User res = userService.findUserByName(username);
//1.存在该账号
if (res!=null){
if (res.getTelephone().equals(telephone)){
//1.1 手机号与该账号的手机号对应,可以重置密码
int count = userService.resetPass(res.getId(),newPass);
if (count > 0){
//1.1.1 密码重置成功,返回操作结果
json.put("msg","success");
}else{
//1.1.2 出现错误,重置失败,返回操作结果
json.put("msg","fail");
}
}else{
//1.2 手机号不匹配,返回错误信息
json.put("msg","phone-err");
}
}else{
//2.用户名输入错误,不存在该账号,返回错误信息
json.put("msg","name-err");
}
return json;
}
问卷控制器
@Controller
public class SurveyCtrl {
@Resource
private SurveyService surveyService;
@Resource
private QuestionService questionService;
@Resource
private OptionService optionService;
@Resource
private ResponseService responseService;
//加载所有问卷信息_old
@CrossOrigin
@PostMapping(value = "api/allSurveys")
@ResponseBody
public JSONObject getAllSurveys(){
JSONObject json = new JSONObject();
//获取SurveyList
List<Survey> surveys = surveyService.getAllSurveys();
//把结果json化
JSONArray allSurveyList = JSONArray.parseArray(JSON.toJSONString(surveys));
json.put("allSurveyList",allSurveyList);
return json;
}
//加载所有问卷信息_05.05
@CrossOrigin
@PostMapping(value = "api/surveysInfo")
@ResponseBody
public JSONObject getSurveysInfo(){
JSONObject json = new JSONObject();
//获取List jsonObject:sid,stitle,count
List<JSONObject> surveysList = surveyService.getSurveysInfo();
JSONArray surveys = JSONArray.parseArray(JSON.toJSONString(surveysList));
json.put("tableData",surveys);
return json;
}
//根据关键字查找相关的问卷_old
@CrossOrigin
@PostMapping(value = "api/SurveySearch")
@ResponseBody
public JSONObject getSurveyList(@RequestBody String keyword){
JSONObject json = new JSONObject();
System.out.println(keyword);
//判断关键字是否为空
if (keyword == null || "".equals(keyword)){
//关键字为空 返回所有问卷
List<Survey> surveys = surveyService.getAllSurveys();
//把结果json化
JSONArray allSurveyList = JSONArray.parseArray(JSON.toJSONString(surveys));
json.put("res",allSurveyList);
}else{
//根据关键字查找
List<Survey> surveyList = surveyService.getSurveysByKey('%'+keyword+'%');
//把结果json化
JSONArray keySurveyList = JSONArray.parseArray(JSON.toJSONString(surveyList));
json.put("res",keySurveyList);
}
return json;
}
//根据关键字查找相关的问卷_05.05
@CrossOrigin
@PostMapping(value = "api/search")
@ResponseBody
public JSONObject surveysSearch(@RequestBody String keyword){
JSONObject json = new JSONObject();
System.out.println(keyword);
//判断关键字是否为空
if (keyword == null || "".equals(keyword)){
//关键字为空 返回所有问卷
List<JSONObject> surveys = surveyService.getSurveysInfo();
//把结果json化
JSONArray allSurveyList = JSONArray.parseArray(JSON.toJSONString(surveys));
json.put("res",allSurveyList);
}else{
//根据关键字查找
List<JSONObject> surveyList = surveyService.surveysSearch('%'+keyword+'%');
//把结果json化
JSONArray keySurveyList = JSONArray.parseArray(JSON.toJSONString(surveyList));
json.put("res",keySurveyList);
}
return json;
}
//根据关键字搜索个人问卷信息
@CrossOrigin
@PostMapping(value = "api/getSurveyByUid")
@ResponseBody
public JSONObject getSurveyListByUid(@RequestBody JSONObject data){
JSONObject json = new JSONObject();
//获取用户编号
int uid = data.getInteger("uid");
//获取关键字
String keyword = data.getString("keyword");
if (keyword!=null && !"".equals(keyword) && uid>0){
//根据关键字搜索问卷
List<Survey> surveyList = surveyService.getMySurveysByKey(uid,'%'+keyword+'%');
JSONArray surveys = JSONArray.parseArray(JSON.toJSONString(surveyList));
//返回搜索结果
json.put("surveyData",surveys);
}
return json;
}
如果也想学习本系统,下面领取。回复:002springboot