概述
涉及技術
- SpringMVC、Spring、SpringData/JPA 整合完成CRUD、翻頁
- 基於Restful風格
- 使用JPA二級緩存
- 使用@PesponseBody註解完成Ajax
搭建環境
1. 配置Spring
2. 配置SpringMVC
3. 加入JPA
4. 加入SpringDataweb.xml
spring配置
<!-- 1. 配置spring : 配置啓動IOC 容器的 Listener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
springMVC的配置
<!--2 .配置springMVC 相關 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<!-- 定位springMvc 配置文件的位置, 另一種方法:servletname-servlet.xml 默認位置 默認在WEB-INFO下
<init-param> <param-name>contextConfigLocation</param-name> <param-value>location</param-value>
</init-param> -->
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
字符編碼過濾器
<!--3. 配置字符編碼過濾器 必須配置在所有過濾器的最前面() 獲取請求參數 或 讀取之前被調用否則無效 HiddenHttpMethodFilter
在執行前會調用getFilter 將導致此過濾器無效 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Resetful風格支持
<!-- 4. 配置可以把POST 請求轉化爲PUT DELETE 請求的FILTER 風格 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- applicationContext.xml
<!--配置自動掃描的包 -->
<context:component-scan base-package="com.atguigu.sssp">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<!--配置數據源 -->
<context:property-placeholder location="classpath:db.propertiers" />
<!-- 配置數據源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
</bean>
<!--配置 entityManagerFactory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
<!--自動去掃描哪些 帶有@entity 註解 的包 -->
<property name="packagesToScan" value="com.atguigu.sssp"></property>
<property name="jpaProperties">
<props>
<!-- 生成的數據表的列的映射策略 -->
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<!-- hibernate 基本屬性 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 二級緩存相關 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
<!--如何用二級緩存
1. 配置相關屬性
2.添加配置文件
3.在需要緩存的bean上添加註解
4.需要自定義一個JPQL語句 ,讓後設置一個setHint
ENABLE_SELECTIVE :表使只緩存添加了@Cacheable 註解的bean
-->
<property name="sharedCacheMode" value="ENABLE_SELECTIVE"></property>
</bean>
<!-- 配置事務 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!-- 配置支持註解的事務 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置spring Data -->
<jpa:repositories base-package="com.atguigu.sssp"
entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
springDispatcherServlet-servlet.xml
<!-- 配置自動掃描的包 -->
<context:component-scan base-package="com.atguigu.sssp" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--處理靜態資源 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>