1024programmer Blog ssm framework integration (simple addition, deletion, modification and query)

ssm framework integration (simple addition, deletion, modification and query)

class=”markdown_views prism-atom-one-dark”>

1. Environmental requirements
myEclipse+jdk1.8+tomcat7+ssm framework requires jar
https://pan.baidu.com/s/1eNn658Hkncm4z28yQtEqxg
Extraction code: 18fd
Second, project structure screenshot:
Insert picture description here
Third, detailed configuration of the configuration file:
1. web. xml configuration file:


   
 springMVC
 org.springframework.web.servlet.DispatcherServlet
 
 contextConfigLocation
 classpath:applicationContext.xml
 
 1
 
     
 springMVC
 /
 
  
   
 
 charcterEncoding
 org.springframework.web.filter.CharacterEncodingFilter
 
 encoding
 utf-8
 
 
 
 charcterEncoding
 /*
 
  
   
 
 org.springframework.web.context.ContextLoaderListener
 
 	
 
 
 contextConfigLocation
 classpath:applicationContext.xml
 
 

2. mybatis.xml file:


 
  
 
 
 
 
 

3, applicationContext.xml file configuration


 
 
 
 
 
 
 	
 
 
 
 
 
 
 
 
 
 	
 
 
 
 
 
 
 
 	
 
 
 
 
 
 
 	
 
 
 
 
 
 
     
 q_lastModify=#{q_lastModify},
 
 
 where q_id = #{q_id}
 
 

 

6. Service layer and its implementation class:
Answer

//AnswerService.java
 public interface AnswerService {
 // To answer the question, just add answer
 int addAnswer(Answer answer);
 // Query the answer below the question
 List selAllAnswer(@Param("q_Id") int q_Id);
 // query the last record
 Answer selAnswerByLastId();
 }



 //AnswerServiceImpl.java
 @Service("answerServiceImpl")
 public class AnswerServiceImpl implements AnswerService{
 @Resource
 private AnswerMapper answerMapper;
 @Resource
 private QuestionMapper questionMapper;
 /**
  * To answer a question, just add an answer
  * @param answer
  * @return
  */
 @Override
 public 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;
 }

 /**
 * Look up the answer below the question
 * @param q_Id question number
 */
 @Override
 public List selAllAnswer(int q_Id) {
 List selAllAnswer = answerMapper. selAllAnswer(q_Id);
 return selAllAnswer;
 }

 @Override
 public Answer selAnswerByLastId() {
 Answer selAnswerByLastId = answerMapper. selAnswerByLastId();
 return selAnswerByLastId;
 }
 }
 

Question:

//QuestionService.java
 public interface QuestionService {
 // query to get all the questions
 List selAllQuestion ();
 // add a question
 int addQuestion(Question question);
 // Query the problem by number
 Question selQuestionById(@Param("q_id") int q_id);
 }




 //QuestionServiceImpl.java
 @Service("questionServiceImpl")
 public class QuestionServiceImpl implements QuestionService{
 @Resource
 private QuestionMapper questionMapper;

 /**
 * Query to get all the questions
 */
 @Override
 public List selAllQuestion() {
 List selAllQuestion = questionMapper. selAllQuestion();
 return selAllQuestion;
 }

 /**
 * add a question
 */
 @Override
 public int addQuestion(Question question) {
 question.setQ_lastModify(MyUtil.getDate());
 int addQuestionIndex = questionMapper. addQuestion(question);
 return addQuestionIndex;
 }

 /**
 * Query the problem by number
 */
 @Override
 public Question selQuestionById(int q_id) {
 Question selQuestionById = questionMapper. selQuestionById(q_id);
 return selQuestionById;
 }
 }
 

Seven, the implementation of the controller layer (controller layer)
1. AnswerController.java

@Controller
 public class AnswerController {
 @Resource
 private AnswerService answerService;
 @Resource
 private 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";
 }
 /**
 * The controller that answers the question
 * is to add answer
 * Requirements: question number, time to answer the question (adding the implementation class), question text, and the number of questions answered after the addition is successful plus 1
 * @return
 */
 @RequestMapping("/answerQuestion")
 @ResponseBody
 public 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 {
 @Resource
 private QuestionService questionService;
 @RequestMapping("/listQuestion")
 public String listQuestion(ModelMap modelMap){
 List questions = questionService. selAllQuestion();
 modelMap. put("questions", questions);
 return "listQuestion";
 }
 }
 

VIII. Front-end page layer:
1. listQuestion.jsp (this page displays all information)

 
   

Online Q&A

I want to ask a question
serial number question Responses Last Modified
${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 When you click on a question, it will jump to the question answering interface


   

Online Q&A

<!-- Return to home page --> Back to Homepage
Question: ${requestScope.question.q_title}
Problem Description: ${requestScope.question.q_detailDesc}
Netizens answer:
${answer.a_date}
${answer.a_content}
Let me answer
//ajax code to achieve partial refresh (partially added problem) function confirmAnswer(){ var q_id=$("#q_id").val(); var a_content=$("#a_content").val(); alert(q_id+"-----"+a_content); $.ajax({ url : "answerQuestion", type: "GET", dataType : "json", data: { q_id:q_id, a_content:a_content }, async : true, timeout: 50000, success : function(data) { alert(data.lastAnswer.a_content+"--"+data.lastAnswer.a_date+"---"+data.lastAnswer.a_id); var str=""+ ""+ ""+data.lastAnswer.a_date+""+ ""+ ""+ ""+ ""+data.lastAnswer.a_content+""+ ""; $("#showAnswer").append(str); }, error : function() { } }); }

tion”>

${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 When you click on a question, it will jump to the question answering interface


   

Online Q&A

<!-- Return to home page --> Back to Homepage
Question: ${requestScope.question.q_title}
Problem Description: ${requestScope.question.q_detailDesc}
Netizens answer:
${answer.a_date}
${answer.a_content}
Let me answer
//ajax code to achieve partial refresh (partially added problem) function confirmAnswer(){ var q_id=$("#q_id").val(); var a_content=$("#a_content").val(); alert(q_id+"-----"+a_content); $.ajax({ url : "answerQuestion", type: "GET", dataType : "json", data: { q_id:q_id, a_content:a_content }, async : true, timeout: 50000, success : function(data) { alert(data.lastAnswer.a_content+"--"+data.lastAnswer.a_date+"---"+data.lastAnswer.a_id); var str=""+ ""+ ""+data.lastAnswer.a_date+""+ ""+ ""+ ""+ ""+data.lastAnswer.a_content+""+ ""; $("#showAnswer").append(str); }, error : function() { } }); }

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/ssm-framework-integration-simple-addition-deletion-modification-and-query/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索