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

一、环境要求
myEclipse+jdk1.8+tomcat7+ssm框架需要jar
https://pan.baidu.com/s/1eNn658Hkncm4z28yQtEqxg
提取码:18fd
二、项目结构截图:
ssm框架整合(最简单的增删改查)_第1张图片
三、配置文件详细配置:
1、web.xml配置文件:


  
		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文件:



 	
	
		
	

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、数据库设计:
ssm框架整合(最简单的增删改查)_第2张图片

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

//接口类
public interface AnswerMapper {
//	回答问题,就是添加answer
	int 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 question
		
			
				q_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 {
//	回答问题,就是添加answer
	int addAnswer(Answer answer);
//	查询出问题下面的回答
	List selAllAnswer(@Param("q_Id") int q_Id);
//	查询最后一条记录
	Answer selAnswerByLastId();
}



//AnswerServiceImpl.java
@Service("answerServiceImpl")
public class AnswerServiceImpl implements AnswerService{
	@Resource
	private AnswerMapper answerMapper;
	@Resource
	private QuestionMapper questionMapper;
/**
 * 回答问题,就是添加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;
	}

	/**
	 * 查询出问题下面的回答
	 * @param q_Id 问题编号
	 */
	@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 {
//	查询得出所有的问题
	List selAllQuestion ();
//	添加一个问题
	int addQuestion(Question question);
//	通过编号查询出问题
	Question selQuestionById(@Param("q_id") int q_id);
}




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

	/**
	 * 查询得出所有的问题
	 */
	@Override
	public List selAllQuestion() {
		List selAllQuestion = questionMapper.selAllQuestion();
		return selAllQuestion;
	}

	/**
	 * 添加一个问题
	 */
	@Override
	public int addQuestion(Question question) {
		question.setQ_lastModify(MyUtil.getDate());
		int addQuestionIndex = questionMapper.addQuestion(question);
		return addQuestionIndex;
	}

	/**
	 * 通过编号查询出问题
	 */
	@Override
	public Question selQuestionById(int q_id) {
		Question selQuestionById = questionMapper.selQuestionById(q_id);
		return selQuestionById;
	}
}

七、controller层(控制器层)的实现
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";
	}
	/**
	 * 回答问题的controller
	 * 就是添加回答
	 * 需要:问题编号、回答问题的时间(实现类添加)、问题正文、添加成功在问题回答的数量加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";
	}
}

八、前端页面层:
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 代码,实现局部刷新(局部添加问题)

你可能感兴趣的:(ssm)