这里主要是为了使用ehcache,具体表现就是我们显示数据的页面,第一次刷新的时候,会看的hibernate输出了查询的sql语句(开启hibernate的sql输出开关),后面刷新,就没有sql查询语句了,这就说明ehcahe生效了
直接进入正题:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tch.test</groupId> <artifactId>sshcache</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ssh Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit-version}</version> <scope>test</scope> </dependency> <!-- spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <!-- spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring-version}</version> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hiberante-version}</version> </dependency> <!-- hibernate-proxool --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-proxool</artifactId> <version>${hiberante-version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>${hiberante-version}</version> </dependency> <!-- struts2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts2-version}</version> </dependency> <!-- struts2-spring-plugin --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>${struts2-version}</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-version}</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j-version}</version> </dependency> <!-- slf4j-log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j-version}</version> </dependency> <!-- aspectjrt --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj-version}</version> </dependency> <!-- aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj-version}</version> </dependency> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>${postgresql-version}</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>${ehcacheVersion}</version> </dependency> </dependencies> <properties> <junit-version>4.11</junit-version> <spring-version>3.2.6.RELEASE</spring-version> <hiberante-version>3.6.10.Final</hiberante-version> <struts2-version>2.3.16</struts2-version> <mysql-version>5.1.28</mysql-version> <log4j-version>1.2.17</log4j-version> <slf4j-version>1.7.5</slf4j-version> <aspectj-version>1.7.4</aspectj-version> <postgresql-version>9.1-901-1.jdbc4</postgresql-version> <ehcacheVersion>2.6.11</ehcacheVersion> </properties> <build> <finalName>sshcache</finalName> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <!-- http port --> <port>8080</port> <!-- application path always starts with / --> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
applicationContext.xml:
<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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 启动注入功能 --> <context:annotation-config /> <!-- 启动扫描component功能 --> <context:component-scan base-package="com.tch.test" /> <!-- 启动注解实物配置功能 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!--读取数据库配置文件 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="mappingLocations"> <list> <!-- <value>classpath*:com/tch/test/ssh/entity/*.hbm.xml</value> --> <!-- <value>classpath*:com/lubansoft/lbapp/note/po/*.hbm.xml</value> --> </list> </property> <property name="packagesToScan"> <list> <value>com.tch.test</value> </list> </property> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- cache manager --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="shared" value="true" /> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> <property name="cacheManager" ref="cacheManager" /> <property name="cacheName" value="root" /> </bean> </beans>
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property> <property name="hibernate.connection.username">postgres</property> <property name="hibernate.connection.password">postgres</property> <property name="hibernate.dialect">com.tch.test.template.common.util.hibernate.MMySQLDialect</property> <!-- <property name="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property> --> <property name="hibernate.current_session_context_class">thread</property> <!-- use query cache --> <property name="hibernate.cache.use_query_cache">true</property> <!-- use second level cache --> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.cache.region.factory_class"> net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</property> </session-factory> </hibernate-configuration>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results> <result name="error">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error" /> </global-exception-mappings> <action name="show" class="userAction" method="show"> <result>/user/pages/User.jsp</result> </action> <action name="add" class="userAction" method="add"> <result>/user/pages/User.jsp</result> </action> </package> </struts>
log4j.properties:
log4j.rootLogger=debug,CONSOLE ################### # Console Appender ################### log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=debug log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p (%c:%L) - %m%n
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <!-- 配置log4j --> <!--说明:如果没有webAppRootKey这个设置,同一个tomcat下的多个项目要使用log4j就会导致WebApp.root冲突的错误--> <context-param> <param-name>webAppRootKey</param-name> <param-value>mywebapp1.root</param-value> </context-param> <!-- 配置log4j.properties的位置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <!-- 配置spring扫描log4j配置文件(log4j.properties)的时间间隔,当修改了配置文件之后可以立即生效 --> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>3000</param-value> </context-param> <!-- log4j listener配置到ContextLoaderListener之前 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- struts2 filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.htm</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
User.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'User.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <style type="text/css"> a{ text-decoration: none; color:red; } </style> <script type="text/javascript"> //添加用户 function addUser(){ var f = document.forms[0]; f.action = "add.action"; f.submit(); } </script> </head> <body> <form action="" method="post"> <table cellpadding="0" cellspacing="0" border="1" width="60%" align="center"> <colgroup> <col width="20%" align="center"> <col width="20%" align="center"> <col width="20%" align="center"> </colgroup> <thead> <tr> <th width="15%">id</th> <th width="15%">用户名</th> <th width="15%">密码</th> </tr> </thead> <tbody> <s:iterator value="users"> <tr> <td width="15%" align="center"><s:property value="id"/></td> <td width="15%" align="center"><s:property value="name"/></td> <td width="15%" align="center"><s:property value="password"/></td> </tr> </s:iterator> </tbody> </table> 名字:<input type="text" name="user.name"><br> 密码:<input type="text" name="user.password"><br> <input type="button" onclick="addUser();" value="提交"> </form> </body> </html>
其他的java相关代码就是很普通的代码
最后会附上代码