ssm框架整合(最简单的增删改查)

一、环境要求
myEclipse+jdk1.8+tomcat7+ssm框架需要jar
https://pan.baidu.com/s/1eNn658Hkncm4z28yQtEqxg
提取码:18fd
二、项目结构截图:
在这里插入图片描述
三、配置文件详细配置:
1、web.xml配置文件:

springMVCorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:applicationContext.xml1springMVC/charcterEncodingorg.springframework.web.filter.CharacterEncodingFilterencodingutf-8charcterEncoding/*org.springframework.web.context.ContextLoaderListenercontextConfigLocationclasspath:applicationContext.xml

2、mybatis.xml文件:




3、applicationContext.xml文件配置

                          

4、log4j文件的配置:

log4j.rootLogger=info, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

四|、实体类的创建和数据库表的设计:
1、实体类的创建:
Answer.java:

public class Answer {private int a_id;private String a_content;private String a_date;private int a_q_id;private Question question;......get/set方法...构造函数.....自己去生成......}

2、Question.java:

public class Question {private int q_id;private String q_title;private String q_detailDesc;private int q_answerCount;private String q_lastModify;......get/set方法...构造函数.....自己去生成......}

3、数据库设计:
在这里插入图片描述

五、mapper层(数据访问层)代码:
Answer:

//接口类
public interface AnswerMapper {
//	回答问题,就是添加answerint addAnswer(Answer answer);
//	查询出问题下面的回答List selAllAnswer(@Param("q_Id") int q_Id);
//	查询最后一条记录Answer selAnswerByLastId();
}//映射文件:
INSERT INTO answer (a_content,a_date,a_q_id) VALUES (#{a_content},#{a_date},#{a_q_id})

Question:

//接口
public interface QuestionMapper {
//	查询得出所有的问题List selAllQuestion ();
//	添加一个问题int addQuestion(Question question);
//	通过编号查询出问题Question selQuestionById(@Param("q_id") int q_id);
//	修改问题int updateQuestion(Question question);
}//映射文件
INSERT INTO question(q_title,q_detailDesc,q_answerCount,q_lastModify)VALUES(#{q_title},#{q_detailDesc},#{q_answerCount},#{q_lastModify})UPDATE questionq_title=#{q_title},q_detailDesc=#{q_detailDesc},q_answerCount=#{q_answerCount},q_lastModify=#{q_lastModify},where q_id = #{q_id}

六、service层与其实现类:
Answer

//AnswerService.java
public interface AnswerService {
//	回答问题,就是添加answerint addAnswer(Answer answer);
//	查询出问题下面的回答List selAllAnswer(@Param("q_Id") int q_Id);
//	查询最后一条记录Answer selAnswerByLastId();
}//AnswerServiceImpl.java
@Service("answerServiceImpl")
public class AnswerServiceImpl implements AnswerService{@Resourceprivate AnswerMapper answerMapper;@Resourceprivate QuestionMapper questionMapper;
/*** 回答问题,就是添加answer* @param answer* @return*/@Overridepublic int addAnswer(Answer answer) {answer.setA_date(MyUtil.getDate());int addAnswerIndex = answerMapper.addAnswer(answer);Question selQuestionById = questionMapper.selQuestionById(answer.getA_q_id());selQuestionById.setQ_answerCount(selQuestionById.getQ_answerCount()+1);int updateQuestionIndex = questionMapper.updateQuestion(selQuestionById);return addAnswerIndex+updateQuestionIndex;}/*** 查询出问题下面的回答* @param q_Id 问题编号*/@Overridepublic List selAllAnswer(int q_Id) {List selAllAnswer = answerMapper.selAllAnswer(q_Id);return selAllAnswer;}@Overridepublic Answer selAnswerByLastId() {Answer selAnswerByLastId = answerMapper.selAnswerByLastId();return selAnswerByLastId;}
}

Question:

//QuestionService.java
public interface QuestionService {
//	查询得出所有的问题List selAllQuestion ();
//	添加一个问题int addQuestion(Question question);
//	通过编号查询出问题Question selQuestionById(@Param("q_id") int q_id);
}//QuestionServiceImpl.java
@Service("questionServiceImpl")
public class QuestionServiceImpl implements QuestionService{@Resourceprivate QuestionMapper questionMapper;/*** 查询得出所有的问题*/@Overridepublic List selAllQuestion() {List selAllQuestion = questionMapper.selAllQuestion();return selAllQuestion;}/*** 添加一个问题*/@Overridepublic int addQuestion(Question question) {question.setQ_lastModify(MyUtil.getDate());int addQuestionIndex = questionMapper.addQuestion(question);return addQuestionIndex;}/*** 通过编号查询出问题*/@Overridepublic Question selQuestionById(int q_id) {Question selQuestionById = questionMapper.selQuestionById(q_id);return selQuestionById;}
}

七、controller层(控制器层)的实现
1、AnswerController.java

@Controller
public class AnswerController {@Resourceprivate AnswerService answerService;@Resourceprivate QuestionService questionService;@RequestMapping("/showQuestionAnswer/{q_id}")public String showQuestionAnswer(@PathVariable("q_id") int q_id,ModelMap modelMap){List answers = answerService.selAllAnswer(q_id);Question question = questionService.selQuestionById(q_id);modelMap.put("question", question);modelMap.put("answers", answers);return "showQuestionAnswer";}/*** 回答问题的controller* 就是添加回答* 需要:问题编号、回答问题的时间(实现类添加)、问题正文、添加成功在问题回答的数量加1* @return*/@RequestMapping("/answerQuestion")@ResponseBodypublic String answerQuestion(String q_id,String a_content){JSONObject obj=new JSONObject();int addAnswerIndex = answerService.addAnswer(new Answer(a_content,Integer.parseInt(q_id)));if(addAnswerIndex>=2){Answer lastAnswer = answerService.selAnswerByLastId();obj.put("lastAnswer",lastAnswer);}return obj.toJSONString();}
}

2、QuestionController.java

@Controller
public class QuestionController {@Resourceprivate QuestionService questionService;@RequestMapping("/listQuestion")public String listQuestion(ModelMap modelMap){List questions = questionService.selAllQuestion();modelMap.put("questions", questions);return "listQuestion";}
}

八、前端页面层:
1、listQuestion.jsp(该页面显示所有信息)

 

在线问答

我要提问
序号问题回答次数最后修改时间
${question.q_id}${question.q_title}${question.q_answerCount}${question.q_lastModify}
${question.q_id}${question.q_title}${question.q_answerCount}${question.q_lastModify}

2、showQuestionAnswer.jsp 点击问题时,跳转的回答问题界面

在线问答

返回首页
问题:${requestScope.question.q_title}
问题描述:${requestScope.question.q_detailDesc}
网友回答:
${answer.a_date}
${answer.a_content}
我来回答
//ajax 代码,实现局部刷新(局部添加问题)


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部