集成SSSP
Posted z8z87878
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集成SSSP相关的知识,希望对你有一定的参考价值。
逆水行舟
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>Sssp</display-name>
<!-- SpringMvc用来拦截提交中文转换的,防止提交乱码 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring的核心监听器 -->
<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>
<!-- Spring的核心Servlet分发器 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 实现SpringMvc的Rest风格的拦截器 -->
<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>
</web-app>
Spring配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- Spring的扫描包,不与SpringMvc扫描冲突,生成两次 -->
<context:component-scan base-package="com.ee.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>
<!-- 配置C3P0数据源 -->
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="$jdbc.user" />
<property name="password" value="$jdbc.password" />
<property name="driverClass" value="$jdbc.driverClass" />
<property name="jdbcUrl" value="$jdbc.jdbcUrl" />
</bean>
<!-- JPA的session管理工厂,依赖于C3P0数据源 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- JPA扫描的数据包,来扫描对应的注解来和生成或和表形成关联 -->
<property name="packagesToScan" value="com.ee.sssp.entities" />
<property name="jpaVendorAdapter"> <!-- jpa自动设配器 -->
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
<property name="jpaProperties">
<props> <!-- jpa和表的映射规则 -->
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<!-- 相关数据库属性 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.mysql5InnoDBDialect</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>
<!-- 开启JPA二级缓存 -->
<property name="sharedCacheMode" value="ENABLE_SELECTIVE"></property>
</bean>
<!-- jpa事务管理器,依赖于上面配置的entityManagerFactory -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- 注解驱动,默认依赖于上面配置的 transactionManager-->
<tx:annotation-driven/>
<!-- SpringData基于jpa使用配置,对应的包和session管理工程 -->
<jpa:repositories base-package="com.ee.sssp.repository"
entity-manager-factory-ref="entityManagerFactory"
/>
</beans>
SpringMvc配置
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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-4.0.xsd">
<!-- 扫描包配置,不与SpringMvc产生冲突 -->
<context:component-scan base-package="com.ee.sssp.handle" 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/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 静态资源默认handler -->
<mvc:default-servlet-handler/>
<!-- 注解驱动 ,上面和这个都不能却,标配-->
<mvc:annotation-driven/>
</beans>
对应二级缓存使用方法:
bean
package com.ee.sssp.entities;
import java.io.Serializable;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Cacheable
@Table(name="ssh_department") //表名
@Entity
public class Department implements Serializable
private static final long serialVersionUID = 1L;
private Integer id;
private String departmentName; //默认驼峰命名转下划线即对应表中字段department_name
@GeneratedValue
@Id
public Integer getId() //主键
return id;
public void setId(Integer id)
this.id = id;
public String getDepartmentName()
return departmentName;
public void setDepartmentName(String departmentName)
this.departmentName = departmentName;
@Override
public String toString()
return "Department [id=" + id + ", departmentName=" + departmentName
+ "]";
repository
package com.ee.sssp.repository;
import java.util.List;
import javax.persistence.QueryHint;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import com.ee.sssp.entities.Department;
public interface DepartmentRepository extends JpaRepository<Department, Integer>
@QueryHints( @QueryHint(name="org.hibernate.cacheable", value="true") )
@Query("FROM Department d")
List<Department> getAll();
以上是关于集成SSSP的主要内容,如果未能解决你的问题,请参考以下文章