SSM项目实战

一、项目创建:

1、创建一个Javaweb项目并且配置好tomcat等

(之前做过 此步省略)

 

2、在setting中引入MAVEN

SSM项目实战_第1张图片

 

 

 

 注意:

MAVEN 的版本不能早于编辑器的版本否则会报错。

 

3、创建项目结构

SSM项目实战_第2张图片

 

 

 

二、代码编写:

1、在pom.xml中引入需要的包的信息

 

pom.xml

 

 

 2、创建spring的配置文件applicationContext.xml,并整合SpringMVC和Mybatis

 1 
 2  3        xmlns="http://www.springframework.org/schema/beans"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:tx="http://www.springframework.org/schema/tx"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 7                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
 8                         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
 9     
10     
11 
12     
13     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
14         
15         
16         
17         
18     
19     
20     class="org.mybatis.spring.SqlSessionFactoryBean">
21         
22         
23     
24 
25     
26     class="org.mybatis.spring.mapper.MapperScannerConfigurer">
27         
28     
29 
30     
31     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
32         
33     
34     
35     
36 
37 
applicationContext.xml

 

 3、创建SpringMVC配置文件

 1 
 2  3        xmlns="http://www.springframework.org/schema/beans"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 7                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
 8                         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
 9 
10     
11     package="com" />
12 
13     
14     
15 
16     
17 
18     
19     
20     
21     
22     
23     
24 
25     
26     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
27         
28         
29         
30         
31 
32     
33 
34 
springmvc.xml

 

4、编写实体类

 1 package com.entity;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 
 7 @Data
 8 @AllArgsConstructor
 9 @NoArgsConstructor
10 public class User {
11     String userID;
12     String userPWD;
13 
14     public String getUsername() {
15         return userID;
16     }
17 
18     public void setUsername(String username) { this.userID = username; }
19 
20     public String getPassword() {
21         return userPWD;
22     }
23 
24     public void setPassword(String password) {
25         this.userPWD = password;
26     }
27 }
user
 1 package com.entity;
 2 import lombok.AllArgsConstructor;
 3 import lombok.Data;
 4 import lombok.NoArgsConstructor;
 5 
 6 @Data
 7 @AllArgsConstructor
 8 @NoArgsConstructor
 9 public class News {
10     private int newsId;
11     private String newsContent;
12     private  String newsAuthor;
13     private  String newsTitle;
14 
15     public int getNewsId() {
16         return newsId;
17     }
18 
19     public void setNewsId(int newsId) {
20         this.newsId = newsId;
21     }
22 
23     public String getNewsContent() {
24         return newsContent;
25     }
26 
27     public void setNewsContent(String newsContent) {
28         this.newsContent = newsContent;
29     }
30 
31     public String getNewsAuthor() {
32         return newsAuthor;
33     }
34 
35     public void setNewsAuthor(String newsAuthor) {
36         this.newsAuthor = newsAuthor;
37     }
38 
39     public String getNewsTitle() {
40         return newsTitle;
41     }
42 
43     public void setNewsTitle(String newsTitle) {
44         this.newsTitle = newsTitle;
45     }
46 
47 }
news

 

5、service层

 1 package com.service;
 2 
 3 import com.entity.News;
 4 
 5 import java.sql.*;
 6 import java.util.ArrayList;
 7 import java.util.List;
 8 
 9 public class NewsService {
10     //添加新闻
11     public boolean AddNews(News news) throws ClassNotFoundException, SQLException {
12         Statement statement;
13         Connection conn ;
14         Class.forName("com.mysql.cj.jdbc.Driver");
15         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?serverTimezone=GMT&characterEncoding=utf-8","root","1234567890");
16         String sql="insert into new_schema.news (newsID,newsTitle,newsContent,newsAuthor) " +
17                 "values ('"+news.getNewsId()+"','"+news.getNewsTitle()+"','"+news.getNewsContent()+"','"+news.getNewsAuthor()+"')";
18         statement=conn.createStatement();
19         int result =statement.executeUpdate(sql);
20         if (result>0)return true;
21         else return false;
22     }
23 
24     //删除新闻
25     public boolean DeleteNews(int newsId) throws ClassNotFoundException, SQLException {
26         Statement statement = null;
27         Connection conn ;
28         Class.forName("com.mysql.cj.jdbc.Driver");
29         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?serverTimezone=GMT&characterEncoding=utf-8","root","1234567890");
30         String sql= "delete from new_schema.news where newsID="+newsId;
31         statement=conn.createStatement();
32         int result = statement.executeUpdate(sql);
33         if(result>0)return true;
34         else return false;
35 
36     }
37 
38     //更新新闻
39     public  boolean UpdateNews(News news) throws ClassNotFoundException, SQLException {
40         Statement statement = null;
41         Connection conn ;
42         ResultSet rs;
43         Class.forName("com.mysql.cj.jdbc.Driver");
44         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?serverTimezone=GMT&characterEncoding=utf-8","root","1234567890");
45         String sql="update new_schema.news set newsTitle ='"+news.getNewsTitle()+"', newsContent ='"+news.getNewsContent()+"', newsAuthor ='"+news.getNewsAuthor()+"' where newsID="+news.getNewsId();
46         statement = conn.createStatement();
47         int result = statement.executeUpdate(sql);
48         if(result>0)return true;
49         else  return false;
50     }
51 
52     //查询新闻
53     public News QueryIndividualNews(int newsID)throws ClassNotFoundException, SQLException {
54         Statement statement = null;
55         Connection conn;
56         ResultSet rs;
57         Class.forName("com.mysql.cj.jdbc.Driver");
58         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/news?serverTimezone=GMT&characterEncoding=utf-8", "root", "1234567890");
59         String sql = "Select * from new_schema.news where newsID=" + newsID;
60         statement = conn.createStatement();
61         rs = statement.executeQuery(sql);
62 
63         News n = new News();
64         if (rs.next()) {
65             n.setNewsId(rs.getInt("newsID"));
66             n.setNewsTitle(rs.getString("newsTitle"));
67             n.setNewsAuthor(rs.getString("newsAuthor"));
68             n.setNewsContent(rs.getString("newsContent"));
69         }
70         return n;
71     }
72 
73     //查询数据库内所有新闻
74     public List QueryNews() throws ClassNotFoundException, SQLException {
75         Statement statement = null;
76         Connection conn ;
77         ResultSet rs;
78         Class.forName("com.mysql.cj.jdbc.Driver");
79         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/news?serverTimezone=GMT&characterEncoding=utf-8","root","Guozhaojie610");
80         String sql = "Select * from new_schema.news";
81 
82         statement=conn.createStatement();
83         rs = statement.executeQuery(sql);
84 
85         List NewsList = new ArrayList();
86 
87         while (rs.next()){
88             News n = new News();
89             n.setNewsId(rs.getInt("newsID"));
90             n.setNewsTitle(rs.getString("newsTitle"));
91             n.setNewsAuthor(rs.getString("newsAuthor"));
92             n.setNewsContent(rs.getString("newsContent"));
93             NewsList.add(n);
94         }
95         return  NewsList;
96     }
97 
98 
99 }
service

 

6、controller层

  1 package com.controller;
  2 
  3 import com.entity.News;
  4 import com.entity.User;
  5 import com.service.NewsService;
  6 import org.springframework.beans.factory.annotation.Autowired;
  7 import org.springframework.stereotype.Controller;
  8 import org.springframework.web.bind.annotation.RequestMapping;
  9 
 10 
 11 import javax.servlet.ServletException;
 12 import javax.servlet.http.HttpServletRequest;
 13 import javax.servlet.http.HttpServletResponse;
 14 import java.io.IOException;
 15 import java.io.UnsupportedEncodingException;
 16 import java.sql.SQLException;
 17 import java.util.Arrays;
 18 import java.util.List;
 19 
 20 public class NewsController {
 21 
 22     @Autowired
 23     private NewsService newsService;
 24 
 25     //显示所有新闻
 26     @RequestMapping("/ShowNews")
 27     public void ShowNews(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
 28         request.setCharacterEncoding("utf-8");
 29         response.setCharacterEncoding("utf-8");
 30         try {
 31             List newsList = newsService.QueryNews();
 32             for (News s:newsList
 33             ) {
 34                 System.out.println(s.getNewsId());
 35             }
 36 
 37             request.setAttribute("News", newsList);
 38             request.getRequestDispatcher("ShowNews.jsp").forward(request, response);
 39 
 40         } catch (Exception e) {
 41             e.printStackTrace();
 42         }
 43     }
 44 
 45     @RequestMapping("/AddNews")
 46     public void addNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
 47         request.setCharacterEncoding("utf-8");
 48         response.setCharacterEncoding("utf-8");
 49         response.setHeader("Context-Type","text/html;charset=utf-8");
 50         News news =new News();
 51         news.setNewsId(Integer.valueOf(request.getParameter("newsid")));
 52         news.setNewsAuthor(request.getParameter("author"));
 53         news.setNewsContent(request.getParameter("Content"));
 54         news.setNewsTitle(request.getParameter("title"));
 55         NewsService newsService=new NewsService();
 56         try {
 57             newsService.AddNews(news);
 58             request.getRequestDispatcher("ShowNews").forward(request,response);
 59         } catch (ClassNotFoundException e) {
 60             e.printStackTrace();
 61         } catch (SQLException e) {
 62             e.printStackTrace();
 63         }
 64     }
 65 
 66     @RequestMapping("/UpdateNews")
 67     public void editNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
 68         response.setContentType("text/html; charset=UTF-8");
 69         request.setCharacterEncoding("UTF-8");
 70         response.setHeader("Context-Type", "text/html;charset=utf-8");
 71         News news = new News();
 72         news.setNewsId(Integer.valueOf(request.getParameter("newsid")));
 73         news.setNewsAuthor(request.getParameter("author"));
 74         news.setNewsContent(request.getParameter("Content"));
 75         news.setNewsTitle(request.getParameter("title"));
 76         NewsService newsService = new NewsService();
 77         try {
 78             newsService.UpdateNews(news);
 79             request.getRequestDispatcher("ShowNews").forward(request, response);
 80         } catch (ClassNotFoundException e) {
 81             e.printStackTrace();
 82         } catch (SQLException e) {
 83             e.printStackTrace();
 84         }
 85     }
 86 
 87 
 88     @RequestMapping("/deleteNew")
 89     public void deleteNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
 90 
 91         int newsID=Integer.valueOf(request.getParameter("newsid"));
 92         NewsService newsService=new NewsService();
 93         try {
 94             newsService.DeleteNews(newsID);
 95         } catch (ClassNotFoundException e) {
 96             e.printStackTrace();
 97         } catch (SQLException e) {
 98             e.printStackTrace();
 99         }
100         request.getRequestDispatcher("com.controller.ShowNewsServlet").forward(request,response);
101     }
102 
103     @RequestMapping("/viewNew")
104     public void viewNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
105         int newsID=Integer.valueOf(request.getParameter("newsid"));
106         NewsService newsService=new NewsService();
107         try {
108             News news=newsService.QueryIndividualNews(newsID);
109             request.setAttribute("news",news);
110             request.getRequestDispatcher("NewsDetail.jsp").forward(request,response);
111         }catch (ClassNotFoundException e) {
112             e.printStackTrace();
113         } catch (SQLException e) {
114             e.printStackTrace();
115         }
116     }
117 
118 }
newsController
package com.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

@Controller
public class UserController {

    @RequestMapping("/Login")
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setHeader("Context-Type", "text/html;charset=utf-8");

        PrintWriter output = response.getWriter();
        String userID = request.getParameter("username");
        String userPWD = request.getParameter("pwd");
        ResultSet rs;
        Statement statement = null;
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?serverTimezone=GMT&characterEncoding=utf-8", "root", "1234567890");
            String sql = "select * from userinfo where id='" + userID + "' and password='" + userPWD + "'";
            statement = conn.createStatement();
            rs = statement.executeQuery(sql);
            if (rs.next()) {
                request.setAttribute("username", userID);
                response.sendRedirect("index.html");
            } else {
                output.println("账号或密码错误");
                response.sendRedirect("index.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
userController

 

 

7、web.xml的修改

 1 
 2  3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 5           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 6          version="2.5">
 7 
 8   SSM
 9 
10   
11     index.jsp
12   
13 
14   
15   
16     class>org.springframework.web.context.ContextLoaderListenerclass>
17   
18 
19   
20   
21     contextConfigLocation
22     classpath:applicationContext.xml
23   
24 
25   
26   
27     encoding
28     class>org.springframework.web.filter.CharacterEncodingFilterclass>
29     
30       encoding
31       UTF-8
32     
33   
34   
35     encoding
36     /*
37   
38 
39   
40   
41     springmvc
42     org.springframework.web.servlet.DispatcherServlet
43     
44     
45       contextConfigLocation
46       classpath:springmvc.xml
47     
48     1
49   
50   
51     springmvc
52     /
53   
54 
55   
56     AFilter
57     com.Filter.AFilter
58   
59   
60     AFilter
61     /AFilter
62   
63 
64 
web.xml

 

三、效果展示:

1、新闻首页

SSM项目实战_第3张图片

 

 

2、登录界面

 

 

 SSM项目实战_第4张图片

 

3、后台添加新闻

SSM项目实战_第5张图片

 

 

4、新闻管理

 

 

 

码云地址:https://gitee.com/lbs727299681/SpringMVC.git

 

你可能感兴趣的:(SSM项目实战)