需要的jar包: 测试需要的jar包 junit
spring系列的jar包 spring-webmvc(spring-aop spring-beans spring-context spring-core spring-expression spring-web) spring-jdbc spring-orm aspectjweaver aspectjrt aopalliance aop切面
hibernate jar包 hibernate-core hibernate-ehcache
连接数据库的jar包 commons-logging commons-dbcp(commons-pool) mysql-connector-java
日志的jar包 slf4j-api slf4j-log4j12 log4j-core
文件上传 commons-fileupload
转json fastjson
缓存 ehcache
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启注解扫描功能-->
<context:annotation-config></context:annotation-config>
<!--定义注解扫描的包-->
<context:component-scan base-package="com.qy.dao"></context:component-scan>
<context:component-scan base-package="com.qy.service"></context:component-scan>
<context:component-scan base-package="com.qy.domain"></context:component-scan>
<!--开启aop自动代理模式-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!--从上下文环境中读取db.properties配置文件-->
<context:property-placeholder location="classpath*:db.properties"></context:property-placeholder>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${dbcp.driverClassName}"></property>
<property name="url" value="${dbcp.url}"></property>
<property name="username" value="${dbcp.username}"></property>
<property name="password" value="${dbcp.password}"></property>
<property name="maxActive" value="${dbcp.maxActive}"></property>
<property name="initialSize" value="${dbcp.initialSize}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--指定当前会话工厂使用的数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--指定实体类的位置-->
<property name="packagesToScan" value="com.qy.domain"/>
<!-- <property name="annotatedClasses">
<list>
<value>com.qy.domain.Province</value>
</list>
</property>-->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
<!-- hibernate.hbm2ddl.auto=create -->
</value>
</property>
</bean>
<!-- 配置 HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置spring的声明性事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 要根据hibernate的版本配置 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" propagation="NOT_SUPPORTED"></tx:method>
</tx:attributes>
</tx:advice>
<!-- 配置事务切入点 -->
<aop:config>
<aop:pointcut expression="execution(* com.qy.service.impl.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>
</beans>
springmvc-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--指定视图的前缀和后缀,Controller返回的String类型与这里的前后缀拼接,构成返回的视图页面地址-->
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--开启注解扫描功能-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 处理请求返回json字符串的中文乱码问题 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 解决Controller返回json中文乱码问题 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<!-- <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> -->
<!-- <property name="supportedMediaTypes" value="application/json;charset=UTF-8" > -->
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- fastJson配置 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--定义注解扫描的包-->
<context:component-scan base-package="com.qy.controller"></context:component-scan>
<!--处理静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--将链接中的静态的访问路径映射为URL,常用于加载html、js、css、图片、视频等静态资源-->
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<!--springmvc定义好的,用来处理上传文件的类-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<value>1048576</value>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- Spring的OpenSessionInView实现 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
</filter-class>
<!-- singleSession默认为true,若设为false则等于没用OpenSessionInView 。所以默认可以不写-->
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<!--
指定org.springframework.orm.hibernate3.LocalSessionFactoryBean在spring配置文件中的名称,默认值为sessionFactory。 如果LocalSessionFactoryBean在spring中的名称不是sessionFactory,该参数一定要指定,否则会出现找不到sessionFactory的例外。所以默认可以不写
-->
<init-param>
<param-name>sessionFactoryBean</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--filter 过滤器 用来解决中文乱码问题-->
<filter>
<filter-name>characterEncoding</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>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>default1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>default1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置spring-->
<!-- 配置去哪裡加載spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置spring的監聽器,加載資源初始化文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
db.properties
dbcp.driverClassName=com.mysql.jdbc.Driver
dbcp.url=jdbc:mysql://localhost:3306/onetomore
dbcp.username=root
dbcp.password=123456
dbcp.maxActive=100
dbcp.initialSize=5
在applicationContext.xml中配置读取db.properties的配置
使用${}读取文件,这里我前面加个前缀,不然读取不了,一定跟前面的name=“”不一样就行
<!--从上下文环境中读取db.properties配置文件-->
<context:property-placeholder location="classpath*:db.properties"></context:property-placeholder>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${dbcp.driverClassName}"></property>
<property name="url" value="${dbcp.url}"></property>
<property name="username" value="${dbcp.username}"></property>
<property name="password" value="${dbcp.password}"></property>
<property name="maxActive" value="${dbcp.maxActive}"></property>
<property name="initialSize" value="${dbcp.initialSize}"></property>
</bean>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/java1807</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!--数据库表示什么引擎,它就是什么引擎-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--显示sql-->
<property name="hibernate.show_sql">true</property>
<!--美化sql,格式化sql语句-->
<property name="hibernate.format_sql">true</property>
<!--使用二级缓存-->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!--配置二级缓存服务商-->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!--使用查询缓存-->
<property name="cache.use_query_cache">true</property>
<mapping resource="Area.hbm.xml"/>
<mapping resource="City.hbm.xml"/>
<mapping resource="Province.hbm.xml"/>
<!-- <mapping class="com.qy.domain.Province"></mapping>
<mapping class="com.qy.domain.City"></mapping>
<mapping class="com.qy.domain.Area"></mapping>-->
<!-- <property name="connection.username"/> -->
<!-- <property name="connection.password"/> -->
<!--配置读写-->
<class-cache class="com.qy.domain.Province" usage="read-write"></class-cache>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
删除hibernate.cgf.xml文件,把hibernate的文件配置在application.xml中
<!-- 配置Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--指定当前会话工厂使用的数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--指定实体类的位置-->
<property name="packagesToScan" value="com.qy.domain"/>
<!-- <property name="annotatedClasses">
<list>
<value>com.qy.domain.Province</value>
</list>
</property>-->
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
<!-- hibernate.hbm2ddl.auto=create -->
</value>
</property>
</bean>
项目运行时,先读取web.xml文件,根据web.xml读取到applicationContext.xml和springmvc-servlet.xml文件,在springmvc-servlet文件主要配置view层的东西,视图解析器,返回json字符串乱码问题,文件上传,定义注解扫描的包,这里只扫描controller层。applicationContext.xml中也要开始注解扫面,扫描的是除了controller层的其他层。读取db.properties配置文件,配置Hibernate的sessionFacory,替代hibernate.cfg.xml文件,最后是配置事务的属性,切入点。这样框架就可以运行起来。可以在seivice层或者test中测试。