疫情期间,先来没事做,就简单的用ssm 写了一个问卷调查系统。用于我们学校得青协调查用。
这就是我做得一个首页的页面,首页做的比较简单嘻嘻。
因为刚接触ssm 框架,我首先的思路是先把整体的ssm框架都搭建好,再去写具体的代码。
所以我在这里也不多说了,直接上代码:
pom.xml也都是按着网上教程一点点走的,
pom.xml
4.0.0
cn.huapei
queston_ssm
1.0-SNAPSHOT
war
queston_ssm Maven Webapp
http://www.example.com
UTF-8
UTF-8
4.2.5.RELEASE
3.2.8
5.1.29
1.7.18
1.2.17
jstl
jstl
1.2
javax
javaee-api
7.0
junit
junit
4.11
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
mysql
mysql-connector-java
${mysql-driver.version}
commons-dbcp
commons-dbcp
1.2.2
com.alibaba
fastjson
1.1.41
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.codehaus.jackson
jackson-mapper-asl
1.9.13
com.fasterxml.jackson.core
jackson-core
2.8.0
com.fasterxml.jackson.core
jackson-databind
2.8.0
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
com.google.code.gson
gson
2.8.0
web-ssm
org.apache.maven.plugins
maven-compiler-plugin
6
6
resources 里的文件如下:
jdbc.properties:
driverClasss=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/yijian?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
username=root
password=123456
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
log4j.properties
添加log4j的日志文件
log4j.rootLogger=INFO,Console,File
#控制台日志
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
#普通文件日志
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=logs/ssm.log
log4j.appender.File.MaxFileSize=10MB
#输出日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
spring-mvc.xml
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
text/html;charset=UTF-8
spring-mybatis.xml
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
这是利用mybatis 实现对数据库的操作。
/p>
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into question(question,a, b ,c,d,type) values (
#{questions.question},#{questions.a},#{questions.b}
,#{questions.c},#{questions.d},#{questions.type}
)
select * from question;
insert into feedback(qu_id, question, daan,q_type)
select #{feedback.qu_id},#{feedback.question},#{feedback.daan},#{feedback.q_type}
from dual
where not exists (
select qu_id from feedback
where daan = #{feedback.daan} )
select * from feedback;
update feedback set num_daan=num_daan+1
where daan=#{daan}
delete from question where q_id=#{id}
delete from feedback where qu_id=#{id}
这个是我的数据库 代码:
/*SQLyog Professional v12.08 (64 bit)
MySQL - 5.5.19 : Database - yijian
**********************************************************************/
/*!40101 SET NAMES utf8*/;/*!40101 SET SQL_MODE=''*/;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0*/;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0*/;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO'*/;/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0*/;CREATE DATABASE /*!32312 IF NOT EXISTS*/`yijian` /*!40100 DEFAULT CHARACTER SET utf8*/;USE`yijian`;/*Table structure for table `feedback`*/
DROP TABLE IF EXISTS`feedback`;CREATE TABLE`feedback` (
`fb_id`int(10) NOT NULL AUTO_INCREMENT COMMENT '反馈id',
`qu_id`int(10) DEFAULT NULL COMMENT '问题id',
`question`varchar(100) DEFAULT NULL COMMENT '问题题目',
`daan`varchar(100) DEFAULT NULL COMMENT '问题答案',
`q_type`int(1) DEFAULT NULL COMMENT '问题类型id',
`num_daan`int(100) DEFAULT '1' COMMENT '回答问题数量',PRIMARY KEY(`fb_id`),KEY`qu_id` (`qu_id`),KEY`q_type` (`q_type`),CONSTRAINT `feedback_ibfk_1` FOREIGN KEY (`qu_id`) REFERENCES`question` (`q_id`),CONSTRAINT `feedback_ibfk_2` FOREIGN KEY (`q_type`) REFERENCES`q_type` (`type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=70 DEFAULT CHARSET=utf8;/*Data for the table `feedback`*/
insert into `feedback`(`fb_id`,`qu_id`,`question`,`daan`,`q_type`,`num_daan`) values (54,1,'请问您在校青协身','副部长',1,13),(55,1,'请问您在校青协身份是','部长',1,17),(57,3,'青协哪点让你欣赏?','制度严明,职位职位清晰',2,16),(59,3,'青协哪点让你欣赏?','职位职位清晰',2,13),(61,34,'你的留言','你好这是留言',3,1),(62,34,'你的留言','你好这是留言1',3,1),(63,34,'你的留言','你好这是留言2',3,1),(64,2,'您进入青协至今多长时间','2',1,1),(65,3,'青协哪点让你欣赏?','制度严明,职位职位清晰,制度清晰,作事有意义',2,1),(66,34,'你的留言','青协很不错,加油继续干',3,0),(67,2,'您进入青协至今多长时间','4',1,1),(68,34,'你的留言','这是我写的',3,1);/*Table structure for table `q_type`*/
DROP TABLE IF EXISTS`q_type`;CREATE TABLE`q_type` (
`type_id`int(4) NOT NULLAUTO_INCREMENT,
`type_name`varchar(25) DEFAULT NULL,PRIMARY KEY(`type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;/*Data for the table `q_type`*/
insert into `q_type`(`type_id`,`type_name`) values (1,'单选'),(2,'多选'),(3,'填空');/*Table structure for table `question`*/
DROP TABLE IF EXISTS`question`;CREATE TABLE`question` (
`q_id`int(25) NOT NULL AUTO_INCREMENT COMMENT '问题id',
`question`varchar(50) DEFAULT NULL COMMENT '问题',
`a`varchar(50) DEFAULT NULL COMMENT 'a选项',
`b`varchar(50) DEFAULT NULL,
`c`varchar(50) DEFAULT NULL,
`d`varchar(50) DEFAULT NULL,
`type`int(4) DEFAULT NULL,PRIMARY KEY(`q_id`),KEY`type` (`type`),CONSTRAINT `question_ibfk_1` FOREIGN KEY (`type`) REFERENCES`q_type` (`type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;/*Data for the table `question`*/
insert into `question`(`q_id`,`question`,`a`,`b`,`c`,`d`,`type`) values (1,'请问您在校青协身份是','部长','副部长','干事','会员',1),(2,'您进入青协至今多长时间','1','2','3','4',1),(3,'青协哪点让你欣赏?','制度严明','职位职位清晰','制度清晰','作事有意义',2),(34,'你的留言',NULL,NULL,NULL,NULL,3);/*!40101 SET SQL_MODE=@OLD_SQL_MODE*/;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS*/;/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS*/;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES*/;
最后是我们的web.xml
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0">
web-ssm
contextConfigLocation
classpath:spring-mybatis.xml
log4jConfigLocation
classpath:log4j.properties
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
SpringMVC
/
/index.jsp
15
现在已经把 框架建立完成,万事具备,就差逻辑代码
首先我们先看实体类:
实体类我定义了两个,一个反馈, 一个问题实体。
如下:
反馈:
packagecn.huapei.model;importjava.util.List;public classfeedback {private intfb_id;private intqu_id;privateString question;privateString daan;private intq_type;private intnum_daan;private Listfeedbacks;
@OverridepublicString toString() {return "feedback{" +
"fb_id=" + fb_id +
", qu_id=" + qu_id +
", question='" + question + '\'' +
", daan='" + daan + '\'' +
", q_type=" + q_type +
", num_daan=" + num_daan +
", feedbacks=" + feedbacks +
'}';
}public intgetNum_daan() {returnnum_daan;
}public void setNum_daan(intnum_daan) {this.num_daan =num_daan;
}public intgetQ_type() {returnq_type;
}public void setQ_type(intq_type) {this.q_type =q_type;
}public intgetFb_id() {returnfb_id;
}public void setFb_id(intfb_id) {this.fb_id =fb_id;
}publicString getQuestion() {returnquestion;
}public voidsetQuestion(String question) {this.question =question;
}publicString getDaan() {returndaan;
}public voidsetDaan(String daan) {this.daan =daan;
}public ListgetFeedbacks() {returnfeedbacks;
}public void setFeedbacks(Listfeedbacks) {this.feedbacks =feedbacks;
}public intgetQu_id() {returnqu_id;
}public void setQu_id(intqu_id) {this.qu_id =qu_id;
}
}
问题:
packagecn.huapei.model;importjava.util.List;public classquestion {privateInteger q_id;privateString question;privateString a;privateString b;privateString c;privateString d;privateInteger type;private Listquestions ;
@OverridepublicString toString() {return "question{" +
"q_id=" + q_id +
", question='" + question + '\'' +
", a='" + a + '\'' +
", b='" + b + '\'' +
", c='" + c + '\'' +
", d='" + d + '\'' +
", type=" + type +
'}';
}public intgetQ_id() {returnq_id;
}public void setQ_id(intq_id) {this.q_id =q_id;
}publicString getQuestion() {returnquestion;
}public voidsetQuestion(String question) {this.question =question;
}publicString getA() {returna;
}public voidsetA(String a) {this.a =a;
}publicString getB() {returnb;
}public voidsetB(String b) {this.b =b;
}publicString getC() {returnc;
}public voidsetC(String c) {this.c =c;
}publicString getD() {returnd;
}public voidsetD(String d) {this.d =d;
}public intgetType() {returntype;
}public void setType(inttype) {this.type =type;
}public ListgetQuestions() {returnquestions;
}public void setQuestions(Listquestions) {this.questions =questions;
}
}
接着我们创建我们dao和service
dao
packagecn.huapei.dao;importcn.huapei.model.feedback;importcn.huapei.model.question;importorg.apache.ibatis.annotations.Param;importorg.springframework.stereotype.Repository;importjava.util.List;
@Repository("QuestionDao")public interfaceQuestionDao {//创建问题列表
public int inserQuestion(@Param("questions") question questions);public ListgetAllQuestions();public int insertFeedback(@Param("feedback") feedback feedback);public ListgetAllFeedback();public int updateNum(@Param("daan") String daan);//通过id 删除问题
public int delQuestionByid(@Param("id") intid);//通过问题id 删除反馈
public int delFeedbackByid(@Param("id") intid);
}
service
packagecn.huapei.service;importcn.huapei.model.feedback;importcn.huapei.model.question;importjava.util.List;public interfaceQuestionService {//创建问题列表
public int inserQuestion( Listquestions);public ListgetAllQuestions();public intinsertFeedback(feedback feedback);//输出问卷调查结果
public Listjieguoshuchu();//通过id 删除问题
public int delQuestionByid( intid);
}
service实现类
packagecn.huapei.service.impl;importcn.huapei.dao.QuestionDao;importcn.huapei.model.feedback;importcn.huapei.model.question;importcn.huapei.service.QuestionService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importjava.util.List;
@Service("QuestionService")
@Transactionalpublic class QuestionServiceImpl implementsQuestionService {
@AutowiredprivateQuestionDao questionDao ;public int inserQuestion(Listquestions) {int i = 0;try{for(question q:questions) {
i+=questionDao.inserQuestion(q);
}
}catch(Exception ex){
System.out.println(ex);
}
System.out.println("Service"+i);returni;
}public ListgetAllQuestions() {returnquestionDao.getAllQuestions();
}public intinsertFeedback(feedback feedback) {int i = 0;int s = 0;try{if (feedback.getQ_type()==1||feedback.getQ_type()==2){
String daan=feedback.getDaan();
i+=questionDao.updateNum(daan);
System.out.println("更新》》》》"+i+"条数据");
}
s=questionDao.insertFeedback(feedback);
System.out.println("添加》》》》"+s+"条数据");
}catch(Exception ex){
System.err.println(ex);
}if(s==0){returni;
}else{returns;
}
}
@Overridepublic Listjieguoshuchu() {returnquestionDao.getAllFeedback();
}
@Overridepublic int delQuestionByid(intid) {int i= 0;try{
List allFeedback =questionDao.getAllFeedback();for(feedback f : allFeedback){if(f.getQu_id() ==id){
questionDao.delFeedbackByid(id);break;
}
}
i=questionDao.delQuestionByid(id);
System.out.println("service"+i);
}catch(Exception ex){
System.err.println(ex);
}returni;
}
}
最后是我们的控制层 :
packagecn.huapei.controller;importcn.huapei.model.feedback;importcn.huapei.model.question;importcn.huapei.service.QuestionService;importcom.google.gson.Gson;importcom.google.gson.GsonBuilder;importcom.google.gson.reflect.TypeToken;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;importjavax.annotation.Resource;importjavax.servlet.http.HttpServletRequest;importjava.util.ArrayList;importjava.util.List;
@Controllerpublic classQuestionController {//@Autowired
@ResourceprivateQuestionService questionService;
@RequestMapping("/chuangjian")
@ResponseBodypublicString inserQuestion(String jsons, HttpServletRequest request) {
System.out.println(jsons);
Gson gson= newGsonBuilder().create();
List list = gson.fromJson(jsons, new TypeToken>() {
}.getType());
System.out.println("---->listJsonStr convert List " +list);int i =questionService.inserQuestion(list);
System.out.println(i);if (i > 0) {return "true";
}else{return "false";
}
}
@RequestMapping("/getQuestion")publicString getAllQuestions(Model model) {
List allQuestions =questionService.getAllQuestions();
System.out.println("所有的问题列表——---------》" +allQuestions);
model.addAttribute("questions", allQuestions);return "wenjuan";
}
@RequestMapping("/question_jg")
@ResponseBodypublicString feedbacks(feedback feedback) {
System.out.println("返回集合》》》》" +feedback);
List feedbacks =feedback.getFeedbacks();
System.out.println("反馈》》》》》》" +feedbacks);int i = 0;for(feedback f : feedbacks) {
System.out.println(f);
i=questionService.insertFeedback(f);
}
System.out.println(i);if (i > 0) {return "true";
}else{return "false";
}
}
@RequestMapping("/Results_page")publicString Results(Model model) {
List feedbackList =questionService.jieguoshuchu();
List allQuestions =questionService.getAllQuestions();
model.addAttribute("feedbackList", feedbackList);
model.addAttribute("allQuestions", allQuestions);return "list";
}//跳转问题页面。。。。
@RequestMapping("createqu")publicString create() {return "createqution";
}//跳转管理页面
@RequestMapping("/managementpage")publicString managementPage(Model model) {
List allQuestions =questionService.getAllQuestions();
model.addAttribute("allQuestions", allQuestions);return "management";
}//管理问题
@RequestMapping("/management")
@ResponseBodypublic String Wjmanagement(@RequestParam(value = "id",required = false) String id) {
Integer del_Id=Integer.parseInt(id);
System.out.println(del_Id);int i =questionService.delQuestionByid(del_Id);
System.out.println(i);if (i > 0) {return "true";
}else{return "false";
}
}
}
最后是我们的前端页面这里jsp 我使用了简单的Bootstrap 前端框架进行美化。
我们首页的jsp
controller.jsp
Created by IntelliJ IDEA.
User: 99573
Date: 2020/2/27
Time: 20:25
To change this template use File | Settings | File Templates.
--%>
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
中心控制页面你好,世界! 欢迎来到我的调查问卷系统。
创建
问卷
结果查询
问卷管理
创建--%>
创建2--%>
问卷 --%>
结果查询--%>
$(function () {
$(".wenjuan").click(function () {
location.assign("getQuestion")
})
$(".jieguo").click(function () {
location.assign("Results_page")
})
$(".chuangjian").click(function () {
location.assign("createqu")
})
$(".createqutions").click(function () {
location.assign("createqu")
})
$(".guanli").click(function () {
location.assign("managementpage")
})
})
创建问题页面
createqution.jsp
User:99573
Date:2020/2/24
Time:14:38
Tochange this template use File | Settings | File Templates.--%>
Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
调查问卷创建页面css/quesstionnair.css"type="text/css">
单选
多选
填空
单选