SpringMVC+Spring+mybatis 项目实践

SpringMVC+Spring+mybatis 项目实践

一、配置环境

SpringMVC+Spring+mybatis 项目实践_第1张图片

 勾选Create from archetypeSpringMVC+Spring+mybatis 项目实践_第2张图片

 

SpringMVC+Spring+mybatis 项目实践_第3张图片

 

选择自己下载的maven地址,点击+号添加archetypeCatalog

SpringMVC+Spring+mybatis 项目实践_第4张图片

 

配置pom.xml文件

  1 xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0modelVersion>
  6 
  7   <groupId>org.examplegroupId>
  8   <artifactId>ssmartifactId>
  9   <version>1.0-SNAPSHOTversion>
 10   <packaging>warpackaging>
 11 
 12   <name>ssm Maven Webappname>
 13   
 14   <url>http://www.example.comurl>
 15 
 16   
 17   <properties>
 18     <spring.version>4.2.4.RELEASEspring.version>
 19     <mybatis.version>3.2.8mybatis.version>
 20     <mybatis.spring.version>1.2.2mybatis.spring.version>
 21     <slf4j.version>1.6.4slf4j.version>
 22     <log4j.version>1.2.17log4j.version>
 23     <jackson.version>2.4.2jackson.version>
 24     <druid.version>1.0.9druid.version>
 25     <sqlserver.version>10.50.4000sqlserver.version>
 26     <jstl.version>1.2jstl.version>
 27     <servlet-api.version>2.5servlet-api.version>
 28     <jsp-api.version>2.0jsp-api.version>
 29     <commons-io.version>1.3.2commons-io.version>
 30     <commons-fileupload.version>1.3.1commons-fileupload.version>
 31   properties>
 32 
 33   <dependencies>
 34     <dependency>
 35       <groupId>junitgroupId>
 36       <artifactId>junitartifactId>
 37       <version>4.11version>
 38       <scope>testscope>
 39     dependency>
 40   dependencies>
 41 
 42 
 43   
 44   <dependencyManagement>
 45     <dependencies>
 46       
 47       <dependency>
 48         <groupId>org.springframeworkgroupId>
 49         <artifactId>spring-contextartifactId>
 50         <version>${spring.version}version>
 51       dependency>
 52       <dependency>
 53         <groupId>org.springframeworkgroupId>
 54         <artifactId>spring-beansartifactId>
 55         <version>${spring.version}version>
 56       dependency>
 57       <dependency>
 58         <groupId>org.springframeworkgroupId>
 59         <artifactId>spring-webmvcartifactId>
 60         <version>${spring.version}version>
 61       dependency>
 62       <dependency>
 63         <groupId>org.springframeworkgroupId>
 64         <artifactId>spring-jdbcartifactId>
 65         <version>${spring.version}version>
 66       dependency>
 67       <dependency>
 68         <groupId>org.springframeworkgroupId>
 69         <artifactId>spring-aspectsartifactId>
 70         <version>${spring.version}version>
 71       dependency>
 72       <dependency>
 73         <groupId>org.springframeworkgroupId>
 74         <artifactId>spring-jmsartifactId>
 75         <version>${spring.version}version>
 76       dependency>
 77       <dependency>
 78         <groupId>org.springframeworkgroupId>
 79         <artifactId>spring-context-supportartifactId>
 80         <version>${spring.version}version>
 81       dependency>
 82 
 83       
 84       <dependency>
 85         <groupId>org.mybatisgroupId>
 86         <artifactId>mybatisartifactId>
 87         <version>${mybatis.version}version>
 88       dependency>
 89       <dependency>
 90         <groupId>org.mybatisgroupId>
 91         <artifactId>mybatis-springartifactId>
 92         <version>${mybatis.spring.version}version>
 93       dependency>
 94 
 95       
 96       <dependency>
 97         <groupId>org.slf4jgroupId>
 98         <artifactId>slf4j-log4j12artifactId>
 99         <version>${slf4j.version}version>
100       dependency>
101       <dependency>
102         <groupId>log4jgroupId>
103         <artifactId>log4jartifactId>
104         <version>${log4j.version}version>
105       dependency>
106 
107       
108       <dependency>
109         <groupId>com.fasterxml.jackson.coregroupId>
110         <artifactId>jackson-databindartifactId>
111         <version>${jackson.version}version>
112       dependency>
113 
114       
115       <dependency>
116         <groupId>com.alibabagroupId>
117         <artifactId>druidartifactId>
118         <version>${druid.version}version>
119       dependency>
120 
121       
122       <dependency>
123         <groupId>sqlservergroupId>
124         <artifactId>sqljdbc4-javaartifactId>
125         <version>${sqlserver.version}version>
126       dependency>
127 
128       
129       <dependency>
130         <groupId>org.apache.commonsgroupId>
131         <artifactId>commons-ioartifactId>
132         <version>${commons-io.version}version>
133       dependency>
134 
135       
136       <dependency>
137         <groupId>commons-fileuploadgroupId>
138         <artifactId>commons-fileuploadartifactId>
139         <version>${commons-fileupload.version}version>
140       dependency>
141 
142       
143       <dependency>
144         <groupId>jstlgroupId>
145         <artifactId>jstlartifactId>
146         <version>${jstl.version}version>
147       dependency>
148       <dependency>
149         <groupId>javax.servletgroupId>
150         <artifactId>servlet-apiartifactId>
151         <version>${servlet-api.version}version>
152         <scope>providedscope>
153       dependency>
154       <dependency>
155         <groupId>javax.servletgroupId>
156         <artifactId>jsp-apiartifactId>
157         <version>${jsp-api.version}version>
158         <scope>providedscope>
159       dependency>
160     dependencies>
161   dependencyManagement>
162 
163   <build>
164     
178     
179     <resources>
180       <resource>
181         <directory>src/main/javadirectory>
182         <includes>
183           <include>**/*.xmlinclude>
184         includes>
185         <filtering>falsefiltering>
186       resource>
187 
188       <resource>
189         <directory>src/main/resourcesdirectory>
190         <includes>
191           <include>**/*.propertiesinclude>
192           <include>**/*.xmlinclude>
193         includes>
194         <filtering>falsefiltering>
195       resource>
196     resources>
197 
198   build>
199 
200   <build>
201     <finalName>ssmfinalName>
202     <pluginManagement>
203       <plugins>
204         <plugin>
205           <artifactId>maven-clean-pluginartifactId>
206           <version>3.1.0version>
207         plugin>
208         
209         <plugin>
210           <artifactId>maven-resources-pluginartifactId>
211           <version>3.0.2version>
212         plugin>
213         <plugin>
214           <artifactId>maven-compiler-pluginartifactId>
215           <version>3.8.0version>
216         plugin>
217         <plugin>
218           <artifactId>maven-surefire-pluginartifactId>
219           <version>2.22.1version>
220         plugin>
221         <plugin>
222           <artifactId>maven-war-pluginartifactId>
223           <version>3.2.2version>
224         plugin>
225         <plugin>
226           <artifactId>maven-install-pluginartifactId>
227           <version>2.5.2version>
228         plugin>
229         <plugin>
230           <artifactId>maven-deploy-pluginartifactId>
231           <version>2.8.2version>
232         plugin>
233       plugins>
234     pluginManagement>
235   build>
236 project>

 

新建resources文件夹, 创建之后,选择Recourses,点击Apply

SpringMVC+Spring+mybatis 项目实践_第5张图片

 

配置applicationContext.xml

SpringMVC+Spring+mybatis 项目实践_第6张图片

 

 1 xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans
 8     http://www.springframework.org/schema/beans/spring-beans.xsd
 9     http://www.springframework.org/schema/context
10     http://www.springframework.org/schema/context/spring-context.xsd
11     http://www.springframework.org/schema/aop
12     http://www.springframework.org/schema/aop/spring-aop.xsd
13     http://www.springframework.org/schema/tx
14     http://www.springframework.org/schema/tx/spring-tx.xsd">
15 
16     
17     <context:component-scan base-package="com" >
18         
19         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
20     context:component-scan>
21 
22 beans>
applicationContext.xml

 

添加springmvc.xml

 
  

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










































 

 

配置web.xml

 1 xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 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   <display-name>ssmdisplay-name>
 9 
10   <welcome-file-list>
11     <welcome-file>index.jspwelcome-file>
12   welcome-file-list>
13 
14   
15   <listener>
16     <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
17   listener>
18   
19   <context-param>
20     <param-name>contextConfigLocationparam-name>
21     <param-value>classpath:applicationContext.xmlparam-value>
22   context-param>
23 
24 
25   
26   <servlet>
27     <servlet-name>dispatcherServletservlet-name>
28     <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
29     
30     <init-param>
31       <param-name>contextConfigLocationparam-name>
32       <param-value>classpath:springmvc.xmlparam-value>
33     init-param>
34     
35     <load-on-startup>1load-on-startup>
36   servlet>
37   <servlet-mapping>
38     <servlet-name>dispatcherServletservlet-name>
39     <url-pattern>/url-pattern>
40   servlet-mapping>
41 
42   
43   <filter>
44     <filter-name>characterEncodingFilterfilter-name>
45     <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
46     <init-param>
47       <param-name>encodingparam-name>
48       <param-value>UTF-8param-value>
49     init-param>
50   filter>
51   <filter-mapping>
52     <filter-name>characterEncodingFilterfilter-name>
53     <url-pattern>/*url-pattern>
54   filter-mapping>
55 
56   <filter>
57     <filter-name>indexFilterfilter-name>
58     <filter-class>com.zero.controller.IndexFilterfilter-class>
59   filter>
60   <filter-mapping>
61     <filter-name>indexFilterfilter-name>
62     <url-pattern>/index.jspurl-pattern>
63   filter-mapping>
64 
65 web-app>
web.xml

二、整合项目代码

1.把原项目的代码复制到现项目,Service,DAL,Entity的和原来一样,只是需要把Controller中的代码修改一下

  1 package com.Controller;
  2 
  3 import com.Entity.News;
  4 import com.Service.NewsService;
  5 import org.apache.commons.beanutils.BeanUtils;
  6 import org.springframework.beans.factory.annotation.Autowired;
  7 import org.springframework.stereotype.Controller;
  8 import org.springframework.web.bind.annotation.RequestMapping;
  9 import javax.servlet.FilterChain;
 10 import javax.servlet.ServletContext;
 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.lang.reflect.InvocationTargetException;
 16 import java.sql.Date;
 17 import java.sql.SQLException;
 18 import java.text.ParseException;
 19 import java.text.SimpleDateFormat;
 20 import java.util.List;
 21 import java.util.Map;
 22 
 23 public class NewsController {
 24 
 25     @RequestMapping("/Add")
 26     protected void Add(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
 27 
 28         request.setCharacterEncoding("utf-8");
 29         response.sendRedirect("AddNews.jsp");
 30     }
 31 
 32 
 33     @RequestMapping("/AddNews")
 34     protected void AddNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 35 
 36         request.setCharacterEncoding("utf-8");
 37         News news = new News();
 38         news.setCategory(request.getParameter("category"));
 39         news.setTitle(request.getParameter("title"));
 40         news.setContents(request.getParameter("contents"));
 41         news.setAuthor(request.getParameter("author"));
 42        /* String newsDate=request.getParameter("newsdate");
 43         SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd"); //加上时间
 44         Date date= null;
 45         try {
 46             date = sDateFormat.parse(newsDate);
 47         } catch (ParseException e) {
 48             e.printStackTrace();
 49         }*/
 50         news.setNewsdate(Date.valueOf(request.getParameter("newsdate")));
 51         NewsService service = new NewsService();
 52         service.AddNews(news);
 53         request.getRequestDispatcher("ShowNewsListServlet").forward(request, response);
 54     }
 55 
 56     @RequestMapping("/DeleteNews")
 57     protected void DeleteNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 58     {
 59 
 60         int newsid=Integer.valueOf( request.getParameter("newsid"));
 61         NewsService service= new NewsService();
 62         service.DeleteNews(newsid);
 63         request.getRequestDispatcher("ShowNewsListServlet").forward(request,response);
 64     }
 65 
 66     @RequestMapping("/ViewNews")
 67     protected void ViewNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 68     {
 69 
 70         response.setContentType(("text/html;charset=UTF-8"));
 71         request.setCharacterEncoding("UTF-8");
 72         int id = Integer.parseInt(request.getParameter("newsid"));
 73         System.out.println(id);
 74         NewsService service = new NewsService();
 75         try
 76         {
 77             News news = service.GetNews(id);
 78             request.setAttribute("news",news);
 79             request.getRequestDispatcher("ViewNews.jsp").forward(request,response);
 80         }
 81         catch (SQLException e)
 82         {
 83             e.printStackTrace();
 84         }
 85 
 86     }
 87 
 88     @RequestMapping("/ShowNews")
 89     protected void ShowNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 90     {
 91 
 92         NewsService newsService = new NewsService();
 93         try{
 94             List listNews  = newsService.QueryNews();
 95             request.setAttribute("listNews",listNews);
 96             request.getRequestDispatcher("ShowNewsList.jsp").forward(request,response);
 97         }catch (SQLException e)
 98         {
 99             e.printStackTrace();
100         }
101     }
102 
103     @RequestMapping("/SaveNews")
104     protected void SaveNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
105 
106         request.setCharacterEncoding("utf-8");
107         News news=new News ();
108         news.setNewsid(Integer.valueOf( request.getParameter("newsid")));
109         news.setCategory(request.getParameter("category"));
110         news.setTitle(request.getParameter("title"));
111         news.setAuthor(request.getParameter("author"));
112         String newsDate=request.getParameter("newsdate");
113         SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd"); //加上时间
114         java.util.Date date= null;
115         try {
116             date = sDateFormat.parse(newsDate);
117         } catch (ParseException e) {
118             e.printStackTrace();
119         }
120         news.setNewsdate(date);
121         news.setContents(request.getParameter("contents"));
122         NewsService service=new NewsService();
123         System.out.println(service.UpdateNews(news));
124         request.getRequestDispatcher("ShowNewsListServlet").forward(request,response);
125 
126     }
127 
128     @RequestMapping("/EditNews")
129     protected void EditNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
130     {
131 
132         request.setCharacterEncoding("UTF-8");
133         int newsid=Integer.valueOf( request.getParameter("newsid"));
134         System.out.println("newsID:"+newsid);
135         NewsService service= new NewsService();
136         try {
137             News news=service.GetNews(newsid);
138             request.setAttribute("news", news);
139             request.getRequestDispatcher("EditNews.jsp").forward(request,response);
140         } catch (SQLException e) {
141             e.printStackTrace();
142         }
143     }
144 
145 }
NewsController

 

 1 package com.Controller;
 2 
 3 import com.Entity.Users;
 4 import com.Service.UsersService;
 5 
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 
10 import javax.servlet.ServletException;
11 import javax.servlet.http.Cookie;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.servlet.http.HttpSession;
15 import java.io.IOException;
16 import java.io.PrintWriter;
17 
18 
19 public class UserController {
20 
21 
22     @RequestMapping("/Login")
23     protected void Login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
24     {
25         response.setContentType("text/html; charset=UTF-8");
26         request.setCharacterEncoding("UTF-8");
27         PrintWriter out = response.getWriter();
28         Users user = new Users();
29         user.setUsername(request.getParameter("txtName"));
30         user.setPassword(request.getParameter("txtPwd"));
31         UsersService userService = new UsersService();
32         if(userService.QueryUser(user))
33         {
34             Cookie cookieUserName = new Cookie("username", user.getUsername());//创建一个键值对的cookie对象
35             cookieUserName.setMaxAge(60*60*24*30);//设置cookie的生命周期
36             response.addCookie(cookieUserName);//添加到response中
37 
38             Cookie cookiePwd= new Cookie("password", user.getPassword());//创建一个键值对的cookie对象
39             cookiePwd.setMaxAge(60*60*24*30);//设置cookie的生命周期
40             response.addCookie(cookiePwd);//添加到response中
41 
42             HttpSession session = request.getSession(true);
43 
44             response.sendRedirect("page/index.html");
45 
46         }
47 
48         else{
49             out.println("错误的用户名和密码");
50 
51         }
52 
53     }
54 }
UserController

2.配置Tomcat并运行

SpringMVC+Spring+mybatis 项目实践_第7张图片

 

SpringMVC+Spring+mybatis 项目实践_第8张图片

 

SpringMVC+Spring+mybatis 项目实践_第9张图片

 SpringMVC+Spring+mybatis 项目实践_第10张图片

 

三、GitHub地址

https://github.com/youthcyl/SSM

 

你可能感兴趣的:(SpringMVC+Spring+mybatis 项目实践)