SSM

SpringMVC+Spring+mybatis项目实践

 

小组成员:赵鑫林201731062425  

                  陈天钧201731062429

gitee:https://gitee.com/anakin123/MVC2

一、  配置环境

创建mamven

 SSM_第1张图片

 

 

 SSM_第2张图片

 

 

 

 SSM_第3张图片

 

 

 

 SSM_第4张图片

 

 

 

 

 

 

 

导入包坐标:




  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.0.0

  com.zero
  javaEESSM
  1.0-SNAPSHOT
  war

  javaEESSM Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
    5.0.2.RELEASE
    1.6.6
    1.2.12
    5.1.6
    3.4.5
  

  
    
      junit
      junit
      4.11
      test
    

    
    
      org.aspectj
      aspectjweaver
      1.6.8
    

    
      org.springframework
      spring-aop
      ${spring.version}
    

    
      org.springframework
      spring-context
      ${spring.version}
    


    
      org.springframework
      spring-web
      ${spring.version}
    

    
      org.springframework
      spring-webmvc
      ${spring.version}
    

    
      org.springframework
      spring-test
      ${spring.version}
    

    
      org.springframework
      spring-tx
      ${spring.version}
    

    
      org.springframework
      spring-jdbc
      ${spring.version}
    

    
      junit
      junit
      4.12
      test
    

    
      mysql
      mysql-connector-java
      ${mysql.version}
    

    
      javax.servlet
      servlet-api
      2.5
      provided
    

    
      javax.servlet.jsp
      jsp-api
      2.0
      provided
    

    
      jstl
      jstl
      1.2
    

    
    
      log4j
      log4j
      ${log4j.version}
    

    
      org.slf4j
      slf4j-api
      ${slf4j.version}
    

    
      org.slf4j
      slf4j-log4j12
      ${slf4j.version}
    
    
    
      org.mybatis
      mybatis
      ${mybatis.version}
    

    
      org.mybatis
      mybatis-spring
      1.3.0
    

    
      commons-beanutils
      commons-beanutils
      1.8.0
    

  

  
    javaEESSM
    
      
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-war-plugin
          3.2.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

 

配置recource

1.配置JDBC

druid.driver=com.mysql.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/all_db?characterEncoding=utf-8
druid.username=root
druid.password=zxl19981019

2。配置Mybatis的sqlMapConfig.xml


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">


    
    
        <package name="com.xiao.pojo"/>
    

 

3.配置spring的配置文件



       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    
    package="com" />

    
    

    

    
    
    
    
    
    

    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        
        
        

    

 

配置web.xml文件



         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

  SSM

  
    index.jsp
  

  
  
    class>org.springframework.web.context.ContextLoaderListenerclass>
  

  
  
    contextConfigLocation
    classpath:applicationContext.xml
  

  
  
    encoding
    class>org.springframework.web.filter.CharacterEncodingFilterclass>
    
      encoding
      UTF-8
    
  
  
    encoding
    /*
  

  
  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:springmvc.xml
    
    1
  
  
    springmvc
    /
  

 

 

修改mvc项目中的java类文件

package com.controller;

import com.entity.News;
import com.entity.User;
import com.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class NewsController {

    @Autowired
    private NewsService newsService;

    //显示所有新闻
    @RequestMapping("/ShowNews")
    public void ShowNews(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        try {
            List newsList = newsService.QueryNews();
            for (News s:newsList
            ) {
                System.out.println(s.getNewsId());
            }

            request.setAttribute("News", newsList);
            request.getRequestDispatcher("ShowNews.jsp").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/AddNews")
    public void addNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Context-Type","text/html;charset=utf-8");
        News news =new News();
        news.setNewsId(Integer.valueOf(request.getParameter("newsid")));
        news.setNewsAuthor(request.getParameter("author"));
        news.setNewsContent(request.getParameter("Content"));
        news.setNewsTitle(request.getParameter("title"));
        NewsService newsService=new NewsService();
        try {
            newsService.AddNews(news);
            request.getRequestDispatcher("ShowNews").forward(request,response);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/UpdateNews")
    public void editNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setHeader("Context-Type", "text/html;charset=utf-8");
        News news = new News();
        news.setNewsId(Integer.valueOf(request.getParameter("newsid")));
        news.setNewsAuthor(request.getParameter("author"));
        news.setNewsContent(request.getParameter("Content"));
        news.setNewsTitle(request.getParameter("title"));
        NewsService newsService = new NewsService();
        try {
            newsService.UpdateNews(news);
            request.getRequestDispatcher("ShowNews").forward(request, response);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/deleteNew")
    public void deleteNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        int newsID=Integer.valueOf(request.getParameter("newsid"));
        NewsService newsService=new NewsService();
        try {
            newsService.DeleteNews(newsID);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("com.controller.ShowNewsServlet").forward(request,response);
    }

    @RequestMapping("/viewNew")
    public void viewNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int newsID=Integer.valueOf(request.getParameter("newsid"));
        NewsService newsService=new NewsService();
        try {
            News news=newsService.QueryIndividualNews(newsID);
            request.setAttribute("news",news);
            request.getRequestDispatcher("NewsDetail.jsp").forward(request,response);
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
}

 

原有的user类与news类导入:

package com.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class News {
    private int newsId;
    private String newsContent;
    private  String newsAuthor;
    private  String newsTitle;

    public int getNewsId() {
        return newsId;
    }

    public void setNewsId(int newsId) {
        this.newsId = newsId;
    }

    public String getNewsContent() {
        return newsContent;
    }

    public void setNewsContent(String newsContent) {
        this.newsContent = newsContent;
    }

    public String getNewsAuthor() {
        return newsAuthor;
    }

    public void setNewsAuthor(String newsAuthor) {
        this.newsAuthor = newsAuthor;
    }

    public String getNewsTitle() {
        return newsTitle;
    }

    public void setNewsTitle(String newsTitle) {
        this.newsTitle = newsTitle;
    }

}
package com.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    String username;
    String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) { this.username = username; }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

 

效果图与mvc相同

SSM_第5张图片

 

你可能感兴趣的:(SSM)