依赖配置
(1)junit和spring-test包:junit测试结合Spring测试
(2)log4j、slf4j和slf4jlog4j12包:日志依赖包,利用log4j和slf4j的结合。
(3)hibernate-entitymanager和mysql-connector-java和druid包:Spring与JPA连接的包以及mysql与java的连接包,和dataSource引进druid连接池。
(4)fastjson、jackson-core、jackson-databind和jackson-annotations包:JSON的工具包
(5)Lombok包:自动生成get和set方法的包
(6)spring-webmvc、spring-data-jpa、spring-aop、spring-aspects、spring-beans、spring-context、spring-orm、spring-core、spring-expression、spring-tx包:Spring的一些基础包
(7)jstl、javax.servlet-api包:Jsp的依赖包
(8)qiniu-java-sdk:七牛云文件上传依赖包
Spring配置(包括整合JPA)
(1) 配置包扫描:将所有的后端文件扫描到Spring容器中
(2) 配置dataSource:注入druid数据源,连接数据库
(3) 配置entityManagerFactory:将Spring与JPA连接起来
(4) 配置transactionManager:使得数据的操作具有事务的特性
(5) 配置基于事务的注解:方便使用 @Transactional,注解驱动
SpringMVC配置
(1) 配置MVC注解驱动
(2) 配置包扫描:扫描全部的Controller文件
(3) 配置mappingJacksonHttpMessageConverter:JSON数据格式的转换器
(4) 配置viewResolver:配置视图解析器,引入对Jsp文件的访问
(5) 配置interceptors:配置项目的拦截器,包括登录拦截器和权限拦截器
(6) 配置mvc:resources mapping:将所有静态资源全部放行
(7) 配置mvc:default-servlet-handler
配置web.xml
(1) 配置加载spring配置文件:使用listener
(2) 配置encodingFilter:将所有字符编码都改成UTF-8
(3) 配置log4jConfigLocation:加载log4j的配置文件
(4) 配置DispatcherServlet:加载SpringMVC
(5) 配置encodingFilter:拦截所有已*.do结尾的请求
技巧
之后增加新的功能的时候,若需要添加配置,直接往SpringMVC配置文件上加即可。
使用SSH整合开发的一个项目
github地址:[图片管理系统](https://github.com/rnzhiw/imageplatform)
若觉得这个项目对您有帮助,请帮助star和点赞这篇博文一下
github首页截图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c7e6315040e619e36ef8a02615737e51.png)
整合配置代码
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
">
<context:component-scan base-package="com.zust.itee"/>
<aop:aspectj-autoproxy/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!-- 基本属性 url、user、password -->
<property name="url" value="jdbc:mysql://localhost/imageplatform?useUnicode=true&characterEncoding=utf-8&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1"/>
<property name="minIdle" value="1"/>
<property name="maxActive" value="20"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="SELECT 'x'"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="false"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.zust.itee.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- jpa repositories -->
<jpa:repositories base-package="com.zust.itee.dao" entity-manager-factory-ref="entityManagerFactory"/>
</beans>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd ">
<!-- 把标记了@Controller注解的类转换为bean -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.zust.itee.controller"/>
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
</list>
</property>
</bean>
<!-- 配置ViewResolver视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">
</bean>
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截所有的请求,这个必须写在前面,也就是写在【不拦截】的上面 -->
<mvc:mapping path="/**"/>
<!-- 但是排除下面这些,也就是不拦截请求 -->
<mvc:exclude-mapping path="/login.do"></mvc:exclude-mapping>
<mvc:exclude-mapping path="/static/**"></mvc:exclude-mapping>
<bean class="com.zust.itee.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/static/**"/>
<!--<mvc:exclude-mapping path="/static/**"></mvc:exclude-mapping>-->
<!--<mvc:exclude-mapping path="/login.do"></mvc:exclude-mapping>-->
<bean class="com.zust.itee.interceptor.AuthInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- spring mvc 能够访问静态内容 -->
<mvc:resources mapping="/static/**" location="/static"/>
<mvc:default-servlet-handler/>
</beans>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
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">
<display-name>我的第一个SpringMVC项目</display-name>
<!-- 加载spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- 以下3项参数与log4j的配置相关 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!-- end -->
<!-- 拦截.html的请求 -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!--Spring ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露,在销毁ServletContext的时候清空对应缓存 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- 加载springmvc -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>