标签:
学习尚硅谷笔记:
首先配置application.xml:
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:tx="http://www.springframework.org/schema/tx" 6 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 11 12 <!-- 配置自动扫描的包 --> 13 <context:component-scan base-package="com.atguigu.sssp"> 14 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 15 <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 16 </context:component-scan> 17 18 <!-- 配置数据源 --> 19 <context:property-placeholder location="classpath:db.properties"/> 20 21 <bean id="dataSource" 22 class="com.mchange.v2.c3p0.ComboPooledDataSource"> 23 <property name="user" value="${jdbc.user}"></property> 24 <property name="password" value="${jdbc.password}"></property> 25 <property name="driverClass" value="${jdbc.driverClass}"></property> 26 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> 27 28 <!-- 配置其他属性 --> 29 </bean> 30 31 <!-- 配置 JPA 的 EntityManagerFactory --> 32 <bean id="entityManagerFactory" 33 class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 34 <property name="dataSource" ref="dataSource"></property> 35 <property name="jpaVendorAdapter"> 36 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean> 37 </property> 38 <property name="packagesToScan" value="com.atguigu.sssp"></property> 39 <property name="jpaProperties"> 40 <props> 41 <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> 42 <prop key="hibernate.hbm2ddl.auto">update</prop> 43 <prop key="hibernate.show_sql">true</prop> 44 <prop key="hibernate.format_sql">true</prop> 45 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> 46 47 <prop key="hibernate.cache.use_second_level_cache">true</prop> 48 <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> 49 <prop key="hibernate.cache.use_query_cache">true</prop> 50 </props> 51 </property> 52 <property name="sharedCacheMode" value="ENABLE_SELECTIVE"></property> 53 </bean> 54 55 <!-- 配置事务 --> 56 <bean id="transactionManager" 57 class="org.springframework.orm.jpa.JpaTransactionManager"> 58 <property name="entityManagerFactory" ref="entityManagerFactory"></property> 59 </bean> 60 61 <!-- 配置支持基于注解的事务 --> 62 <tx:annotation-driven transaction-manager="transactionManager"/> 63 64 <!-- 配置 SpringData --> 65 <jpa:repositories base-package="com.atguigu.sssp" 66 entity-manager-factory-ref="entityManagerFactory"></jpa:repositories> 67 68 </beans>
其中:db.properties
jdbc.user=root
jdbc.password=1111
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/springdata
还有二级缓存的配置:ehcache.xml
1 <ehcache> 2 3 <!-- Sets the path to the directory where cache .data files are created. 4 5 If the path is a Java System Property it is replaced by 6 its value in the running VM. 7 8 The following properties are translated: 9 user.home - User‘s home directory 10 user.dir - User‘s current working directory 11 java.io.tmpdir - Default temp file path --> 12 <diskStore path="java.io.tmpdir"/> 13 14 15 <!--Default Cache configuration. These will applied to caches programmatically created through 16 the CacheManager. 17 18 The following attributes are required for defaultCache: 19 20 maxInMemory - Sets the maximum number of objects that will be created in memory 21 eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element 22 is never expired. 23 timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used 24 if the element is not eternal. Idle time is now - last accessed time 25 timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used 26 if the element is not eternal. TTL is now - creation time 27 overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache 28 has reached the maxInMemory limit. 29 30 --> 31 <defaultCache 32 maxElementsInMemory="10000" 33 eternal="false" 34 timeToIdleSeconds="120" 35 timeToLiveSeconds="120" 36 overflowToDisk="true" 37 /> 38 39 <!--Predefined caches. Add your cache configuration settings here. 40 If you do not have a configuration for your cache a WARNING will be issued when the 41 CacheManager starts 42 43 The following attributes are required for defaultCache: 44 45 name - Sets the name of the cache. This is used to identify the cache. It must be unique. 46 maxInMemory - Sets the maximum number of objects that will be created in memory 47 eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element 48 is never expired. 49 timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used 50 if the element is not eternal. Idle time is now - last accessed time 51 timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used 52 if the element is not eternal. TTL is now - creation time 53 overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache 54 has reached the maxInMemory limit. 55 56 --> 57 58 <!-- Sample cache named sampleCache1 59 This cache contains a maximum in memory of 10000 elements, and will expire 60 an element if it is idle for more than 5 minutes and lives for more than 61 10 minutes. 62 63 If there are more than 10000 elements it will overflow to the 64 disk cache, which in this configuration will go to wherever java.io.tmp is 65 defined on your system. On a standard Linux system this will be /tmp" 66 --> 67 <cache name="sampleCache1" 68 maxElementsInMemory="10000" 69 eternal="false" 70 timeToIdleSeconds="300" 71 timeToLiveSeconds="600" 72 overflowToDisk="true" 73 /> 74 75 <!-- Sample cache named sampleCache2 76 This cache contains 1000 elements. Elements will always be held in memory. 77 They are not expired. --> 78 <cache name="sampleCache2" 79 maxElementsInMemory="1000" 80 eternal="true" 81 timeToIdleSeconds="0" 82 timeToLiveSeconds="0" 83 overflowToDisk="false" 84 /> --> 85 86 <!-- Place configuration for your caches following --> 87 88 </ehcache>
springmvc的配置:springDispatcherServlet-servlet.xml
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:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 9 10 <!-- 配置自动扫描的包 --> 11 <context:component-scan base-package="com.atguigu.sssp" use-default-filters="false"> 12 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 13 <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 14 </context:component-scan> 15 16 <!-- 配置视图解析器 --> 17 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 18 <property name="prefix" value="/WEB-INF/views/"></property> 19 <property name="suffix" value=".jsp"></property> 20 </bean> 21 22 <mvc:default-servlet-handler/> 23 <mvc:annotation-driven></mvc:annotation-driven> 24 25 </beans>
web.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 4 <!-- 配置启动 IOC 容器的 Listener --> 5 <context-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:applicationContext.xml</param-value> 8 </context-param> 9 10 <listener> 11 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 12 </listener> 13 14 <!-- 配置字符编码过滤器 --> 15 <!-- 字符编码过滤器必须配置在所有过滤器的最前面! --> 16 <filter> 17 <filter-name>CharacterEncodingFilter</filter-name> 18 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 19 <init-param> 20 <param-name>encoding</param-name> 21 <param-value>UTF-8</param-value> 22 </init-param> 23 </filter> 24 25 <filter-mapping> 26 <filter-name>CharacterEncodingFilter</filter-name> 27 <url-pattern>/*</url-pattern> 28 </filter-mapping> 29 30 <!-- 配置可以把 POST 请求转为 PUT、DELETE 请求的 Filter --> 31 <filter> 32 <filter-name>HiddenHttpMethodFilter</filter-name> 33 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 34 </filter> 35 36 <filter-mapping> 37 <filter-name>HiddenHttpMethodFilter</filter-name> 38 <url-pattern>/*</url-pattern> 39 </filter-mapping> 40 41 <!-- 配置 OpenEntityManagerInViewFilter. 可以解决懒加载异常的问题 --> 42 <filter> 43 <filter-name>OpenEntityManagerInViewFilter</filter-name> 44 <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> 45 </filter> 46 47 <filter-mapping> 48 <filter-name>OpenEntityManagerInViewFilter</filter-name> 49 <url-pattern>/*</url-pattern> 50 </filter-mapping> 51 52 <!-- 配置 SpringMVC 的 DispatcherServlet --> 53 <servlet> 54 <servlet-name>springDispatcherServlet</servlet-name> 55 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 56 <load-on-startup>1</load-on-startup> 57 </servlet> 58 59 <servlet-mapping> 60 <servlet-name>springDispatcherServlet</servlet-name> 61 <url-pattern>/</url-pattern> 62 </servlet-mapping> 63 64 </web-app>
标签:
原文地址:http://www.cnblogs.com/yzwhykd/p/5936573.html