SSM重新开发计科院新闻网站
学号:201631062509
姓名:杨菓
1.什么是SSM
SSM就是Spring+SpringMVC+MyBatis框架的整合。
1.1 Spring
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
1.2 SpringMVC
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
1.3 MyBatis
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
这次我分了2个配置文件,分别是spring-mybatis.xml,包含spring和mybatis的配置文件,还有个是spring-mvc的配置文件,此外有2个资源文件:jdbc.propertis和log4j.properties。完整目录结构如下:
使用框架的版本:
Spring 4.0.2 RELEASE
Spring MVC 4.0.2 RELEASE
MyBatis 3.2.6
2.效果图
登陆页面
登陆成功
登陆成功后跳转新闻详情页面
可以选择新闻进行增删查改
增加和修改页面
导入jar包
搭建框架
User.java、UserExample.java、userMapper.xml、userMapper.java
3.源代码
POM配置
1 xml version="1.0"?>
2 <project xsi:schemaLocation&#61;"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns&#61;"http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance">
4 <modelVersion>4.0.0modelVersion>
5 <groupId>com.sqgroupId>
6 <artifactId>NewsartifactId>
7 <version>0.0.1-SNAPSHOTversion>
8 <packaging>warpackaging>
9 <name>News Maven Webappname>
10 <url>http://maven.apache.orgurl>
11 <dependencies>
12 <dependency>
13 <groupId>org.springframeworkgroupId>
14 <artifactId>spring-webartifactId>
15 <version>4.3.18.RELEASEversion>
16 <scope>compilescope>
17 dependency>
18 <dependency>
19 <groupId>org.springframeworkgroupId>
20 <artifactId>spring-webmvcartifactId>
21 <version>4.3.18.RELEASEversion>
22 <scope>compilescope>
23 dependency>
24 <dependency>
25 <groupId>org.mybatisgroupId>
26 <artifactId>mybatisartifactId>
27 <version>3.4.1version>
28 <scope>compilescope>
29 dependency>
30 <dependency>
31 <groupId>org.mybatisgroupId>
32 <artifactId>mybatis-springartifactId>
33 <version>1.3.1version>
34 <scope>compilescope>
35 dependency>
36 <dependency>
37 <groupId>mysqlgroupId>
38 <artifactId>mysql-connector-javaartifactId>
39 <version>8.0.11version>
40 <scope>compilescope>
41 dependency>
42 <dependency>
43 <groupId>javax.servletgroupId>
44 <artifactId>jstlartifactId>
45 <version>1.2version>
46 <scope>compilescope>
47 dependency>
48 <dependency>
49 <groupId>org.apache.logging.log4jgroupId>
50 <artifactId>log4j-coreartifactId>
51 <version>2.11.0version>
52 <scope>compilescope>
53 dependency>
54 <dependency>
55 <groupId>com.mchangegroupId>
56 <artifactId>c3p0artifactId>
57 <version>0.9.5.2version>
58 <scope>compilescope>
59 dependency>
60 <dependency>
61 <groupId>org.springframeworkgroupId>
62 <artifactId>spring-jdbcartifactId>
63 <version>4.3.18.RELEASEversion>
64 <scope>compilescope>
65 dependency>
66 <dependency>
67 <groupId>org.springframeworkgroupId>
68 <artifactId>spring-txartifactId>
69 <version>4.3.18.RELEASEversion>
70 <scope>compilescope>
71 dependency>
72 <dependency>
73 <groupId>org.slf4jgroupId>
74 <artifactId>slf4j-apiartifactId>
75 <version>1.7.25version>
76 <scope>compilescope>
77 dependency>
78 <dependency>
79 <groupId>org.aspectjgroupId>
80 <artifactId>aspectjweaverartifactId>
81 <version>1.8.13version>
82 <scope>compilescope>
83 dependency>
84 <dependency>
85 <groupId>junitgroupId>
86 <artifactId>junitartifactId>
87 <version>3.8.1version>
88 <scope>testscope>
89 dependency>
90 dependencies>
91 <repositories>
92 <repository>
93 <snapshots>
94 <enabled>falseenabled>
95 snapshots>
96 <id>centralid>
97 <name>Central Repositoryname>
98 <url>https://repo.maven.apache.org/maven2url>
99 repository>
100 repositories>
101 <pluginRepositories>
102 <pluginRepository>
103 <releases>
104 <updatePolicy>neverupdatePolicy>
105 releases>
106 <snapshots>
107 <enabled>falseenabled>
108 snapshots>
109 <id>centralid>
110 <name>Central Repositoryname>
111 <url>https://repo.maven.apache.org/maven2url>
112 pluginRepository>
113 pluginRepositories>
114 <build>
115 <sourceDirectory>D:\工作环境\News-master\src\main\javasourceDirectory>
116 <scriptSourceDirectory>D:\工作环境\News-master\src\main\scriptsscriptSourceDirectory>
117 <testSourceDirectory>D:\工作环境\News-master\src\test\javatestSourceDirectory>
118 <outputDirectory>D:\工作环境\News-master\target\classesoutputDirectory>
119 <testOutputDirectory>D:\工作环境\News-master\target\test-classestestOutputDirectory>
120 <resources>
121 <resource>
122 <directory>D:\工作环境\News-master\src\main\resourcesdirectory>
123 resource>
124 resources>
125 <testResources>
126 <testResource>
127 <directory>D:\工作环境\News-master\src\test\resourcesdirectory>
128 testResource>
129 testResources>
130 <directory>D:\工作环境\News-master\targetdirectory>
131 <finalName>NewsfinalName>
132 <pluginManagement>
133 <plugins>
134 <plugin>
135 <artifactId>maven-antrun-pluginartifactId>
136 <version>1.3version>
137 plugin>
138 <plugin>
139 <artifactId>maven-assembly-pluginartifactId>
140 <version>2.2-beta-5version>
141 plugin>
142 <plugin>
143 <artifactId>maven-dependency-pluginartifactId>
144 <version>2.8version>
145 plugin>
146 <plugin>
147 <artifactId>maven-release-pluginartifactId>
148 <version>2.3.2version>
149 plugin>
150 plugins>
151 pluginManagement>
152 <plugins>
153 <plugin>
154 <artifactId>maven-clean-pluginartifactId>
155 <version>2.5version>
156 <executions>
157 <execution>
158 <id>default-cleanid>
159 <phase>cleanphase>
160 <goals>
161 <goal>cleangoal>
162 goals>
163 execution>
164 executions>
165 plugin>
166 <plugin>
167 <artifactId>maven-resources-pluginartifactId>
168 <version>2.6version>
169 <executions>
170 <execution>
171 <id>default-testResourcesid>
172 <phase>process-test-resourcesphase>
173 <goals>
174 <goal>testResourcesgoal>
175 goals>
176 execution>
177 <execution>
178 <id>default-resourcesid>
179 <phase>process-resourcesphase>
180 <goals>
181 <goal>resourcesgoal>
182 goals>
183 execution>
184 executions>
185 plugin>
186 <plugin>
187 <artifactId>maven-war-pluginartifactId>
188 <version>2.2version>
189 <executions>
190 <execution>
191 <id>default-warid>
192 <phase>packagephase>
193 <goals>
194 <goal>wargoal>
195 goals>
196 execution>
197 executions>
198 plugin>
199 <plugin>
200 <artifactId>maven-compiler-pluginartifactId>
201 <version>3.1version>
202 <executions>
203 <execution>
204 <id>default-compileid>
205 <phase>compilephase>
206 <goals>
207 <goal>compilegoal>
208 goals>
209 execution>
210 <execution>
211 <id>default-testCompileid>
212 <phase>test-compilephase>
213 <goals>
214 <goal>testCompilegoal>
215 goals>
216 execution>
217 executions>
218 plugin>
219 <plugin>
220 <artifactId>maven-surefire-pluginartifactId>
221 <version>2.12.4version>
222 <executions>
223 <execution>
224 <id>default-testid>
225 <phase>testphase>
226 <goals>
227 <goal>testgoal>
228 goals>
229 execution>
230 executions>
231 plugin>
232 <plugin>
233 <artifactId>maven-install-pluginartifactId>
234 <version>2.4version>
235 <executions>
236 <execution>
237 <id>default-installid>
238 <phase>installphase>
239 <goals>
240 <goal>installgoal>
241 goals>
242 execution>
243 executions>
244 plugin>
245 <plugin>
246 <artifactId>maven-deploy-pluginartifactId>
247 <version>2.7version>
248 <executions>
249 <execution>
250 <id>default-deployid>
251 <phase>deployphase>
252 <goals>
253 <goal>deploygoal>
254 goals>
255 execution>
256 executions>
257 plugin>
258 <plugin>
259 <artifactId>maven-site-pluginartifactId>
260 <version>3.3version>
261 <executions>
262 <execution>
263 <id>default-siteid>
264 <phase>sitephase>
265 <goals>
266 <goal>sitegoal>
267 goals>
268 <configuration>
269 <outputDirectory>D:\工作环境\News-master\target\siteoutputDirectory>
270 <reportPlugins>
271 <reportPlugin>
272 <groupId>org.apache.maven.pluginsgroupId>
273 <artifactId>maven-project-info-reports-pluginartifactId>
274 reportPlugin>
275 reportPlugins>
276 configuration>
277 execution>
278 <execution>
279 <id>default-deployid>
280 <phase>site-deployphase>
281 <goals>
282 <goal>deploygoal>
283 goals>
284 <configuration>
285 <outputDirectory>D:\工作环境\News-master\target\siteoutputDirectory>
286 <reportPlugins>
287 <reportPlugin>
288 <groupId>org.apache.maven.pluginsgroupId>
289 <artifactId>maven-project-info-reports-pluginartifactId>
290 reportPlugin>
291 reportPlugins>
292 configuration>
293 execution>
294 executions>
295 <configuration>
296 <outputDirectory>D:\工作环境\News-master\target\siteoutputDirectory>
297 <reportPlugins>
298 <reportPlugin>
299 <groupId>org.apache.maven.pluginsgroupId>
300 <artifactId>maven-project-info-reports-pluginartifactId>
301 reportPlugin>
302 reportPlugins>
303 configuration>
304 plugin>
305 plugins>
306 build>
307 <reporting>
308 <outputDirectory>D:\工作环境\News-master\target\siteoutputDirectory>
309 reporting>
310 project>
WEB XML
1 xml version&#61;"1.0" encoding&#61;"UTF-8"?>
2 <web-app xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"
3 xmlns&#61;"http://xmlns.jcp.org/xml/ns/javaee"
4 xsi:schemaLocation&#61;"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5 id&#61;"WebApp_ID" version&#61;"3.1">
6
7
8 <filter>
9 <filter-name>encodingFilterfilter-name>
10 <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
11 <init-param>
12 <param-name>encodingparam-name>
13 <param-value>UTF-8param-value>
14 init-param>
15 <init-param>
16 <param-name>forceEncodingparam-name>
17 <param-value>trueparam-value>
18 init-param>
19 filter>
20 <filter-mapping>
21 <filter-name>encodingFilterfilter-name>
22 <url-pattern>/*url-pattern>
23 filter-mapping>
24
25
26 <servlet>
27 <servlet-name>dispatcherservletservlet-name>
28 <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
29 <init-param>
30 <param-name>contextConfigLocationparam-name>
31 <param-value>classpath:springmvc.xmlparam-value>
32 init-param>
33 <load-on-startup>1load-on-startup>
34 servlet>
35 <servlet-mapping>
36 <servlet-name>dispatcherservletservlet-name>
37 <url-pattern>*.actionurl-pattern>
38 servlet-mapping>
39
40
41 <servlet>
42 <servlet-name>gotoservlet-name>
43 <jsp-file>/WEB-INF/View/deleteNews.jspjsp-file>
44 servlet>
45 <servlet-mapping>
46 <servlet-name>gotoservlet-name>
47 <url-pattern>/deleteNewsurl-pattern>
48 servlet-mapping>
49
50
51 <listener>
52 <description>启动spring容器description>
53 <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
54 listener>
55
56 <context-param>
57 <param-name>contextConfigLocationparam-name>
58 <param-value>classpath:applicationContext.xmlparam-value>
59 context-param>
60 web-app>
APP配置
1 xml version&#61;"1.0" encoding&#61;"UTF-8"?>
2 <beans xmlns&#61;"http://www.springframework.org/schema/beans"
3 xmlns:context&#61;"http://www.springframework.org/schema/context"
4 xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance" xmlns:aop&#61;"http://www.springframework.org/schema/aop"
5 xmlns:tx&#61;"http://www.springframework.org/schema/tx" xmlns:p&#61;"http://www.springframework.org/schema/p"
6 xmlns:util&#61;"http://www.springframework.org/schema/util" xmlns:jdbc&#61;"http://www.springframework.org/schema/jdbc"
7 xmlns:cache&#61;"http://www.springframework.org/schema/cache"
8 xsi:schemaLocation&#61;"
9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
11 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
13
14
15 <context:component-scan base-package&#61;"com.sq.News">context:component-scan>
16
17
18 <bean id&#61;"dataSource" class&#61;"com.mchange.v2.c3p0.ComboPooledDataSource">
19 <property name&#61;"driverClass" value&#61;"com.mysql.cj.jdbc.Driver">property>
20 <property name&#61;"jdbcUrl"
21 value&#61;"jdbc:mysql://localhost:3306/news?user&#61;root&password&#61;1729456sq&serverTimezone&#61;UTC
22 &useUnicode&#61;true&characterEncoding&#61;utf-8">property>
23 bean>
24
25
26 <bean id&#61;"sqlSessionFactory" class&#61;"org.mybatis.spring.SqlSessionFactoryBean">
27 <property name&#61;"dataSource" ref&#61;"dataSource" />
28
29 <property name&#61;"mapperLocations" value&#61;"classpath:com/sq/News/dao/*.xml">property>
30
31 <property name&#61;"configLocation" value&#61;"classpath:mybatis-config.xml">property>
32 bean>
33
34
35 <bean class&#61;"org.mybatis.spring.mapper.MapperScannerConfigurer">
36 <property name&#61;"basePackage" value&#61;"com.sq.News.dao" />
37 <property name&#61;"sqlSessionFactoryBeanName" value&#61;"sqlSessionFactory" />
38 bean>
39
40 beans>
SPRING MVC
1 xml version&#61;"1.0" encoding&#61;"UTF-8"?>
2 <beans xmlns&#61;"http://www.springframework.org/schema/beans"
3 xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance" xmlns:aop&#61;"http://www.springframework.org/schema/aop"
4 xmlns:mvc&#61;"http://www.springframework.org/schema/mvc" xmlns:context&#61;"http://www.springframework.org/schema/context"
5 xmlns:tx&#61;"http://www.springframework.org/schema/tx"
6 xsi:schemaLocation&#61;"http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
8 http://www.springframework.org/schema/tx
9 http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
10 http://www.springframework.org/schema/aop
11 http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
12 http://www.springframework.org/schema/context
13 http://www.springframework.org/schema/context/spring-context-4.1.xsd
14 http://www.springframework.org/schema/mvc
15 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
16
17
18 <mvc:annotation-driven />
19
20 <mvc:default-servlet-handler/>
21
22
23 <context:component-scan base-package&#61;"com.sq.News.controller" />
24
25
26 <bean id&#61;"viewResolver" class&#61;"org.springframework.web.servlet.view.InternalResourceViewResolver">
27
28 <property name&#61;"prefix" value&#61;"/WEB-INF/View/" />
29 <property name&#61;"suffix" value&#61;".jsp" />
30 bean>
31
32 beans>
Mybatis
1 xml version&#61;"1.0" encoding&#61;"UTF-8"?>
2 DOCTYPE configuration
3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5 <configuration>
6
7 <typeAliases>
8 <typeAlias type&#61;"com.sq.News.pojo.NewsInfo" alias&#61;"NI" />
9 typeAliases>
10 configuration>
4.百度云地址
链接&#xff1a;https://pan.baidu.com/s/1BFxuW2f3RgRHlteNA8j29A
提取码&#xff1a;to5a
复制这段内容后打开百度网盘手机App&#xff0c;操作更方便哦