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="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.sq</groupId> 6 <artifactId>News</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>war</packaging> 9 <name>News Maven Webapp</name> 10 <url>http://maven.apache.org</url> 11 <dependencies> 12 <dependency> 13 <groupId>org.springframework</groupId> 14 <artifactId>spring-web</artifactId> 15 <version>4.3.18.RELEASE</version> 16 <scope>compile</scope> 17 </dependency> 18 <dependency> 19 <groupId>org.springframework</groupId> 20 <artifactId>spring-webmvc</artifactId> 21 <version>4.3.18.RELEASE</version> 22 <scope>compile</scope> 23 </dependency> 24 <dependency> 25 <groupId>org.mybatis</groupId> 26 <artifactId>mybatis</artifactId> 27 <version>3.4.1</version> 28 <scope>compile</scope> 29 </dependency> 30 <dependency> 31 <groupId>org.mybatis</groupId> 32 <artifactId>mybatis-spring</artifactId> 33 <version>1.3.1</version> 34 <scope>compile</scope> 35 </dependency> 36 <dependency> 37 <groupId>mysql</groupId> 38 <artifactId>mysql-connector-java</artifactId> 39 <version>8.0.11</version> 40 <scope>compile</scope> 41 </dependency> 42 <dependency> 43 <groupId>javax.servlet</groupId> 44 <artifactId>jstl</artifactId> 45 <version>1.2</version> 46 <scope>compile</scope> 47 </dependency> 48 <dependency> 49 <groupId>org.apache.logging.log4j</groupId> 50 <artifactId>log4j-core</artifactId> 51 <version>2.11.0</version> 52 <scope>compile</scope> 53 </dependency> 54 <dependency> 55 <groupId>com.mchange</groupId> 56 <artifactId>c3p0</artifactId> 57 <version>0.9.5.2</version> 58 <scope>compile</scope> 59 </dependency> 60 <dependency> 61 <groupId>org.springframework</groupId> 62 <artifactId>spring-jdbc</artifactId> 63 <version>4.3.18.RELEASE</version> 64 <scope>compile</scope> 65 </dependency> 66 <dependency> 67 <groupId>org.springframework</groupId> 68 <artifactId>spring-tx</artifactId> 69 <version>4.3.18.RELEASE</version> 70 <scope>compile</scope> 71 </dependency> 72 <dependency> 73 <groupId>org.slf4j</groupId> 74 <artifactId>slf4j-api</artifactId> 75 <version>1.7.25</version> 76 <scope>compile</scope> 77 </dependency> 78 <dependency> 79 <groupId>org.aspectj</groupId> 80 <artifactId>aspectjweaver</artifactId> 81 <version>1.8.13</version> 82 <scope>compile</scope> 83 </dependency> 84 <dependency> 85 <groupId>junit</groupId> 86 <artifactId>junit</artifactId> 87 <version>3.8.1</version> 88 <scope>test</scope> 89 </dependency> 90 </dependencies> 91 <repositories> 92 <repository> 93 <snapshots> 94 <enabled>false</enabled> 95 </snapshots> 96 <id>central</id> 97 <name>Central Repository</name> 98 <url>https://repo.maven.apache.org/maven2</url> 99 </repository> 100 </repositories> 101 <pluginRepositories> 102 <pluginRepository> 103 <releases> 104 <updatePolicy>never</updatePolicy> 105 </releases> 106 <snapshots> 107 <enabled>false</enabled> 108 </snapshots> 109 <id>central</id> 110 <name>Central Repository</name> 111 <url>https://repo.maven.apache.org/maven2</url> 112 </pluginRepository> 113 </pluginRepositories> 114 <build> 115 <sourceDirectory>D:\工作环境\News-master\src\main\java</sourceDirectory> 116 <scriptSourceDirectory>D:\工作环境\News-master\src\main\scripts</scriptSourceDirectory> 117 <testSourceDirectory>D:\工作环境\News-master\src\test\java</testSourceDirectory> 118 <outputDirectory>D:\工作环境\News-master\target\classes</outputDirectory> 119 <testOutputDirectory>D:\工作环境\News-master\target\test-classes</testOutputDirectory> 120 <resources> 121 <resource> 122 <directory>D:\工作环境\News-master\src\main\resources</directory> 123 </resource> 124 </resources> 125 <testResources> 126 <testResource> 127 <directory>D:\工作环境\News-master\src\test\resources</directory> 128 </testResource> 129 </testResources> 130 <directory>D:\工作环境\News-master\target</directory> 131 <finalName>News</finalName> 132 <pluginManagement> 133 <plugins> 134 <plugin> 135 <artifactId>maven-antrun-plugin</artifactId> 136 <version>1.3</version> 137 </plugin> 138 <plugin> 139 <artifactId>maven-assembly-plugin</artifactId> 140 <version>2.2-beta-5</version> 141 </plugin> 142 <plugin> 143 <artifactId>maven-dependency-plugin</artifactId> 144 <version>2.8</version> 145 </plugin> 146 <plugin> 147 <artifactId>maven-release-plugin</artifactId> 148 <version>2.3.2</version> 149 </plugin> 150 </plugins> 151 </pluginManagement> 152 <plugins> 153 <plugin> 154 <artifactId>maven-clean-plugin</artifactId> 155 <version>2.5</version> 156 <executions> 157 <execution> 158 <id>default-clean</id> 159 <phase>clean</phase> 160 <goals> 161 <goal>clean</goal> 162 </goals> 163 </execution> 164 </executions> 165 </plugin> 166 <plugin> 167 <artifactId>maven-resources-plugin</artifactId> 168 <version>2.6</version> 169 <executions> 170 <execution> 171 <id>default-testResources</id> 172 <phase>process-test-resources</phase> 173 <goals> 174 <goal>testResources</goal> 175 </goals> 176 </execution> 177 <execution> 178 <id>default-resources</id> 179 <phase>process-resources</phase> 180 <goals> 181 <goal>resources</goal> 182 </goals> 183 </execution> 184 </executions> 185 </plugin> 186 <plugin> 187 <artifactId>maven-war-plugin</artifactId> 188 <version>2.2</version> 189 <executions> 190 <execution> 191 <id>default-war</id> 192 <phase>package</phase> 193 <goals> 194 <goal>war</goal> 195 </goals> 196 </execution> 197 </executions> 198 </plugin> 199 <plugin> 200 <artifactId>maven-compiler-plugin</artifactId> 201 <version>3.1</version> 202 <executions> 203 <execution> 204 <id>default-compile</id> 205 <phase>compile</phase> 206 <goals> 207 <goal>compile</goal> 208 </goals> 209 </execution> 210 <execution> 211 <id>default-testCompile</id> 212 <phase>test-compile</phase> 213 <goals> 214 <goal>testCompile</goal> 215 </goals> 216 </execution> 217 </executions> 218 </plugin> 219 <plugin> 220 <artifactId>maven-surefire-plugin</artifactId> 221 <version>2.12.4</version> 222 <executions> 223 <execution> 224 <id>default-test</id> 225 <phase>test</phase> 226 <goals> 227 <goal>test</goal> 228 </goals> 229 </execution> 230 </executions> 231 </plugin> 232 <plugin> 233 <artifactId>maven-install-plugin</artifactId> 234 <version>2.4</version> 235 <executions> 236 <execution> 237 <id>default-install</id> 238 <phase>install</phase> 239 <goals> 240 <goal>install</goal> 241 </goals> 242 </execution> 243 </executions> 244 </plugin> 245 <plugin> 246 <artifactId>maven-deploy-plugin</artifactId> 247 <version>2.7</version> 248 <executions> 249 <execution> 250 <id>default-deploy</id> 251 <phase>deploy</phase> 252 <goals> 253 <goal>deploy</goal> 254 </goals> 255 </execution> 256 </executions> 257 </plugin> 258 <plugin> 259 <artifactId>maven-site-plugin</artifactId> 260 <version>3.3</version> 261 <executions> 262 <execution> 263 <id>default-site</id> 264 <phase>site</phase> 265 <goals> 266 <goal>site</goal> 267 </goals> 268 <configuration> 269 <outputDirectory>D:\工作环境\News-master\target\site</outputDirectory> 270 <reportPlugins> 271 <reportPlugin> 272 <groupId>org.apache.maven.plugins</groupId> 273 <artifactId>maven-project-info-reports-plugin</artifactId> 274 </reportPlugin> 275 </reportPlugins> 276 </configuration> 277 </execution> 278 <execution> 279 <id>default-deploy</id> 280 <phase>site-deploy</phase> 281 <goals> 282 <goal>deploy</goal> 283 </goals> 284 <configuration> 285 <outputDirectory>D:\工作环境\News-master\target\site</outputDirectory> 286 <reportPlugins> 287 <reportPlugin> 288 <groupId>org.apache.maven.plugins</groupId> 289 <artifactId>maven-project-info-reports-plugin</artifactId> 290 </reportPlugin> 291 </reportPlugins> 292 </configuration> 293 </execution> 294 </executions> 295 <configuration> 296 <outputDirectory>D:\工作环境\News-master\target\site</outputDirectory> 297 <reportPlugins> 298 <reportPlugin> 299 <groupId>org.apache.maven.plugins</groupId> 300 <artifactId>maven-project-info-reports-plugin</artifactId> 301 </reportPlugin> 302 </reportPlugins> 303 </configuration> 304 </plugin> 305 </plugins> 306 </build> 307 <reporting> 308 <outputDirectory>D:\工作环境\News-master\target\site</outputDirectory> 309 </reporting> 310 </project>
WEB XML
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 id="WebApp_ID" version="3.1"> 6 7 <!-- 配置spring字符编码为utf-8 --> 8 <filter> 9 <filter-name>encodingFilter</filter-name> 10 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 11 <init-param> 12 <param-name>encoding</param-name> 13 <param-value>UTF-8</param-value> 14 </init-param> 15 <init-param> 16 <param-name>forceEncoding</param-name> 17 <param-value>true</param-value> 18 </init-param> 19 </filter> 20 <filter-mapping> 21 <filter-name>encodingFilter</filter-name> 22 <url-pattern>/*</url-pattern> 23 </filter-mapping> 24 25 <!-- 配置springmvc前端控制器 --> 26 <servlet> 27 <servlet-name>dispatcherservlet</servlet-name> 28 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 29 <init-param> 30 <param-name>contextConfigLocation</param-name> 31 <param-value>classpath:springmvc.xml</param-value> 32 </init-param> 33 <load-on-startup>1</load-on-startup> 34 </servlet> 35 <servlet-mapping> 36 <servlet-name>dispatcherservlet</servlet-name> 37 <url-pattern>*.action</url-pattern> 38 </servlet-mapping> 39 40 <!-- 实现jsp通过超链接直接访问 --> 41 <servlet> 42 <servlet-name>goto</servlet-name> 43 <jsp-file>/WEB-INF/View/deleteNews.jsp</jsp-file> 44 </servlet> 45 <servlet-mapping> 46 <servlet-name>goto</servlet-name> 47 <url-pattern>/deleteNews</url-pattern> 48 </servlet-mapping> 49 50 <!-- 配置监听spring --> 51 <listener> 52 <description>启动spring容器</description> 53 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 54 </listener> 55 <!-- 指定spring的配置文件 --> 56 <context-param> 57 <param-name>contextConfigLocation</param-name> 58 <param-value>classpath:applicationContext.xml</param-value> 59 </context-param> 60 </web-app>
APP配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 7 xmlns:cache="http://www.springframework.org/schema/cache" 8 xsi:schemaLocation=" 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 <!-- 自动扫描News包 ,将带有注解的类 纳入spring容器管理 --> 15 <context:component-scan base-package="com.sq.News"></context:component-scan> 16 17 <!-- c3p0连接池 --> 18 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 19 <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property> 20 <property name="jdbcUrl" 21 value="jdbc:mysql://localhost:3306/news?user=root&password=1729456sq&serverTimezone=UTC 22 &useUnicode=true&characterEncoding=utf-8"></property> 23 </bean> 24 25 <!-- 注册SqlSessionFactoryBean --> 26 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 27 <property name="dataSource" ref="dataSource" /> 28 <!--自动扫描mapper.xml文件 --> 29 <property name="mapperLocations" value="classpath:com/sq/News/dao/*.xml"></property> 30 <!--mybatis配置 --> 31 <property name="configLocation" value="classpath:mybatis-config.xml"></property> 32 </bean> 33 34 <!-- Mapper扫描器 --> 35 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 36 <property name="basePackage" value="com.sq.News.dao" /> 37 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 38 </bean> 39 40 </beans>
SPRING MVC
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" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 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 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 <!-- 1、启用注解驱动--> 18 <mvc:annotation-driven /> 19 20 <mvc:default-servlet-handler/> 21 22 <!-- 3、自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> 23 <context:component-scan base-package="com.sq.News.controller" /> 24 25 <!-- 4、视图解析器 --> 26 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 27 28 <property name="prefix" value="/WEB-INF/View/" /><!-- 指定jsp文件存放的位置 --> 29 <property name="suffix" value=".jsp" /> 30 </bean> 31 32 </beans>
Mybatis
1 <?xml version="1.0" encoding="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="com.sq.News.pojo.NewsInfo" alias="NI" /> 9 </typeAliases> 10 </configuration>
4.百度云地址
链接:https://pan.baidu.com/s/1BFxuW2f3RgRHlteNA8j29A
提取码:to5a
复制这段内容后打开百度网盘手机App,操作更方便哦