- 浏览: 3420515 次
- 性别:
- 来自: 珠海
文章分类
- 全部博客 (1633)
- Java (250)
- Android&HTML5 (111)
- Struts (10)
- Spring (236)
- Hibernate&MyBatis (115)
- SSH (49)
- jQuery插件收集 (55)
- Javascript (145)
- PHP (77)
- REST&WebService (18)
- BIRT (27)
- .NET (7)
- Database (105)
- 设计模式 (16)
- 自动化和测试 (19)
- Maven&Ant (43)
- 工作流 (36)
- 开源应用 (156)
- 其他 (16)
- 前台&美工 (119)
- 工作积累 (0)
- OS&Docker (83)
- Python&爬虫 (28)
- 工具软件 (157)
- 问题收集 (61)
- OFbiz (6)
- noSQL (12)
最新评论
-
HEZR曾嶸:
你好博主,这个不是很理解,能解释一下嘛//左边+1,上边+1, ...
java 两字符串相似度计算算法 -
天使建站:
写得不错,可以看这里,和这里的这篇文章一起看,有 ...
jquery 遍历对象、数组、集合 -
xue88ming:
很有用,谢谢
@PathVariable映射出现错误: Name for argument type -
jnjeC:
厉害,困扰了我很久
MyBatis排序时使用order by 动态参数时需要注意,用$而不是# -
TopLongMan:
非常好,很实用啊。。
PostgreSQL递归查询实现树状结构查询
附件是源码
pom.xml
------------------------------------------------
SQL
--------------------------------------------------------------------
web.xml
------------------------------------------------------
src/main/resources/applicationContext.xml
--------------------------------------------------------------------
JBPM5sh-servlet.xml
------------------------------
src/main/resources/ehcache.xml
-------------------------------------------------------
这个是缓存配置
src/main/resources/jdbc.properties
---------------------------------------------------------
jdbc.driver=net.sf.log4jdbc.DriverSpy
jdbc.url=jdbc:log4jdbc:mysql://localhost:3306/jbpmtest?createDatabaseIfNotExist=true
#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/jbpmtest?createDatabaseIfNotExist=true
jdbc.username=root
jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
src/main/resources/log4j.properties
----------------------------------------------------
log4j.rootLogger=WARN, stdout
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# log4jdbc
log4j.logger.jdbc.sqlonly=INFO
log4j.logger.jdbc.sqltiming=INFO
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.connection=OFF
src/main/java/com/pandy/ssh4/domian/BasicDomian.java
--------------------------------------------------------------------------------------
src/main/java/com/pandy/ssh4/domian/Table3.java
------------------------------------------------------
src/main/java/com/pandy/ssh4/dao/BasicDao.java
----------------------------------------------------------------------------
src/main/java/com/pandy/ssh4/dao/impl/Table3Dao.java
----------------------------------------------------------------
注意,query.setCacheable(true);//设定使用缓存是设定了缓存
src/main/java/com/pandy/ssh4/service/SampleService.java
-------------------------------------------------------------
src/main/java/com/pandy/ssh4/controller/TestController.java
-------------------------------------------------
src/main/webapp/index.jsp
--------------------------------------------------
src/main/webapp/views/success.jsp
---------------------------------------
src/main/webapp/views/failure.jsp
--------------------------------------------
http://localhost:8080/JBPM5sh/doTest.do
-----------------------------------------------OUTPUT
12:47:13,628 INFO sqlonly:236 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy', NULL)
12:47:13,630 INFO sqltiming:357 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy', NULL) {executed in 0 msec}
12:47:13,638 WARN JdbcCoordinatorImpl:411 - HHH000387: ResultSet's statement was not registered
----------------------------------------增加完成
12:47:13,887 INFO sqlonly:236 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_
12:47:13,888 INFO sqltiming:357 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_ {executed in 1 msec}
Table3 [id=13, name=pandy, password=null, email=panyongzheng@163.com]
验证完成
----------------------------------------修改完成
12:47:13,917 INFO sqlonly:236 - update TABLE3 set EMAIL='panyongzheng@163.com', NAME='New Name', PASSWORD=NULL where ID=13
12:47:13,918 INFO sqltiming:357 - update TABLE3 set EMAIL='panyongzheng@163.com', NAME='New Name', PASSWORD=NULL where ID=13
{executed in 1 msec}
12:47:13,920 INFO sqlonly:236 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_
12:47:13,921 INFO sqltiming:357 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_ {executed in 1 msec}
Table3 [id=13, name=New Name, password=null, email=panyongzheng@163.com]
验证完成
----------------------------------------删除完成 id=13
12:47:13,924 INFO sqlonly:236 - delete from TABLE3 where ID=13
12:47:13,925 INFO sqltiming:357 - delete from TABLE3 where ID=13 {executed in 0 msec}
12:47:13,926 INFO sqlonly:236 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_
12:47:13,927 INFO sqltiming:357 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_ {executed in 1 msec}
验证完成
----------------------------------------增加两个记录来测试缓存
12:47:14,021 INFO sqlonly:236 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy1', NULL)
12:47:14,022 INFO sqltiming:357 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy1', NULL)
{executed in 0 msec}
12:47:14,023 WARN JdbcCoordinatorImpl:411 - HHH000387: ResultSet's statement was not registered
12:47:14,024 INFO sqlonly:236 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy2', NULL)
12:47:14,025 INFO sqltiming:357 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy2', NULL)
{executed in 0 msec}
12:47:14,026 WARN JdbcCoordinatorImpl:411 - HHH000387: ResultSet's statement was not registered
----------------------------------------增加两个记录信息
12:47:14,028 INFO sqlonly:236 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_
12:47:14,029 INFO sqltiming:357 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_ {executed in 1 msec}
listAll-> Table3 [id=14, name=pandy1, password=null, email=panyongzheng@163.com]
listAll-> Table3 [id=15, name=pandy2, password=null, email=panyongzheng@163.com]
---------------第一次查询--------------------
12:47:14,103 INFO sqlonly:236 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_
12:47:14,104 INFO sqltiming:357 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_ {executed in 1 msec}
2
---------------第二次查询--------------------
2
---------------缓存信息--------------------
Statistics[start time=1368852423495,sessions opened=3,sessions closed=2,transactions=2,successful transactions=2,optimistic lock failures=0,flushes=3,connections obtained=3,statements prepared=10,statements closed=0,second level cache puts=2,second level cache hits=0,second level cache misses=0,entities loaded=2,entities updated=1,entities inserted=3,entities deleted=1,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,naturalId queries executed to database=0,naturalId cache puts=0,naturalId cache hits=0,naturalId cache misses=0,naturalId max query time=0,queries executed to database=5,query cache puts=1,query cache hits=1,query cache misses=1,update timestamps cache puts=7,update timestamps cache hits=1,update timestamps cache misses=0,max query time=12]
put:2
hit:0
miss:0
注意到第二次查询。不再查询数据库,就是缓存了。
什么意思 没有附件?有pom.xml还没足够么?
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.pandy</groupId> <artifactId>JBPM5sh</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ssm3 Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.2.2.RELEASE</spring.version> <spring.jdbc.version>3.2.2.RELEASE</spring.jdbc.version> <aspectj.version>1.6.10</aspectj.version> <jetty.version>6.1.26</jetty.version> <tomcat.version>2.0</tomcat.version> <junit.version>4.11</junit.version> <mysql.version>5.1.25</mysql.version> <jtds.version>1.2.4</jtds.version> <hibernate.version>4.2.1.Final</hibernate.version> <hibernateCommAnn.version>3.2.0.Final</hibernateCommAnn.version> <hibernateAnn.version>3.5.6-Final</hibernateAnn.version> <cglib.version>2.2.2</cglib.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- JBOSS Cache & Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.2.1.Final</version> </dependency> <dependency> <groupId>hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>1.8.0.4</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-testing</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.jdbc.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.jdbc.version}</version> </dependency> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>${hibernateCommAnn.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>${hibernateAnn.version}</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>${cglib.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency> <!-- Log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.lazyluke</groupId> <artifactId>log4jdbc-remix</artifactId> <version>0.2.7</version> </dependency> </dependencies> <build> <finalName>JBPM5sh</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>${jetty.version}</version> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat6-maven-plugin</artifactId> <version>${tomcat.version}</version> </plugin> <plugin> <!-- 注解支持,jdk5才具有的新特性,我们需要设置compile插件,具体可以参考Setting the -source and -target of the Java Compiler,根据说明,我们继续向pom文件中加入 --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>5</source> <target>5</target> </configuration> </plugin> <plugin> <!-- 我们想将所有的依赖库都打包,直接交给用户,这样用户不需要在做其他设置了,这里需要使用Assembly插件了,其说明参考Pre-defined Descriptor Files,这个参考文件也说明了有四种默认定义的打包方式,我们选择jar-with-dependencies,继续添加pom文件 --> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>$/lib</outputDirectory> <!-- 拷贝所以依赖存放位置 --> </configuration> </execution> </executions> </plugin> </plugins> </pluginManagement> </build> </project>
SQL
--------------------------------------------------------------------
CREATE TABLE TABLE3 ( id INT auto_increment NOT NULL, name VARCHAR(90), password VARCHAR(90), email VARCHAR(90), PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
web.xml
------------------------------------------------------
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <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> <servlet> <servlet-name>JBPM5sh</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>JBPM5sh</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
src/main/resources/applicationContext.xml
--------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" default-autowire="byName" default-lazy-init="true"> <context:component-scan base-package="com"> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /> </context:component-scan> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:packagesToScan="com.pandy.ssh4.domian"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> ${hibernate.dialect} </prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.cache.use_query_cache">true</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_structured_entries">true</prop> <prop key="hibernate.generate_statistics">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> </beans>
JBPM5sh-servlet.xml
------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" /> </context:component-scan> <mvc:annotation-driven/> <mvc:resources location="/resources/" mapping="/resources/**"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
src/main/resources/ehcache.xml
-------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <!--如果缓存中的对象存储超过指定的缓存数量的对象存储的磁盘地址 --> <diskStore path="D:/ehcache" /> <!-- 默认cache:如果没有对应的特定区域的缓存,就使用默认缓存 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" /> <!-- 指定区域cache:通过name指定,name对应到Hibernate中的区域名即可 --> <cache name="cn.javass.h3test.model.UserModel" eternal="false" maxElementsInMemory="100" timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"> </cache> </ehcache>
这个是缓存配置
src/main/resources/jdbc.properties
---------------------------------------------------------
jdbc.driver=net.sf.log4jdbc.DriverSpy
jdbc.url=jdbc:log4jdbc:mysql://localhost:3306/jbpmtest?createDatabaseIfNotExist=true
#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/jbpmtest?createDatabaseIfNotExist=true
jdbc.username=root
jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
src/main/resources/log4j.properties
----------------------------------------------------
log4j.rootLogger=WARN, stdout
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# log4jdbc
log4j.logger.jdbc.sqlonly=INFO
log4j.logger.jdbc.sqltiming=INFO
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.connection=OFF
src/main/java/com/pandy/ssh4/domian/BasicDomian.java
--------------------------------------------------------------------------------------
package com.pandy.ssh4.domian; import java.io.Serializable; @SuppressWarnings("serial") public abstract class BasicDomian implements Serializable { public String getDomainName() { return getClass().getName(); } public abstract String getIdName(); }
src/main/java/com/pandy/ssh4/domian/Table3.java
------------------------------------------------------
package com.pandy.ssh4.domian; // Generated 2013-3-7 17:34:40 by Hibernate Tools 4.0.0 import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Transient; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; /** * Table3 generated by hbm2java */ @SuppressWarnings("serial") @Entity @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) @Table(name = "TABLE3") public class Table3 extends BasicDomian implements java.io.Serializable { private int id; private String name; private String password; private String email; public Table3() { } public Table3(int id) { this.id = id; } public Table3(int id, String name, String password, String email) { this.id = id; this.name = name; this.password = password; this.email = email; } @Id @GeneratedValue @Column(name = "ID", unique = true, nullable = false) public int getId() { return this.id; } public void setId(int id) { this.id = id; } @Column(name = "NAME", length = 1000) public String getName() { return this.name; } public void setName(String name) { this.name = name; } @Column(name = "PASSWORD", length = 1000) public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } @Column(name = "EMAIL", length = 1000) public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } @Transient @Override public String getIdName() { return "id"; } @Override public String toString() { return "Table3 [id=" + id + ", name=" + name + ", password=" + password + ", email=" + email + "]"; } }
src/main/java/com/pandy/ssh4/dao/BasicDao.java
----------------------------------------------------------------------------
package com.pandy.ssh4.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; @SuppressWarnings("rawtypes") public abstract class BasicDao extends HibernateDaoSupport { @Autowired public void setSessionFactoryOverride(SessionFactory sessionFactory) { super.setSessionFactory(sessionFactory); } public Session getCurrentSession(){ return getSessionFactory().getCurrentSession(); } public List listAll(String domianName) { Session session = super.getSessionFactory().getCurrentSession(); Query query = session.createQuery("from com.pandy.ssh4.domian." + domianName + " t"); List list = query.list(); return list; } public void save(Object o) { Session session = getSessionFactory().getCurrentSession(); session.save(o); } public void saveOrUpdate(Object o) { Session session = getSessionFactory().getCurrentSession(); session.saveOrUpdate(o); } public void delete(Object o) { try { Session session = getSessionFactory().getCurrentSession(); session.delete(o); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } }
src/main/java/com/pandy/ssh4/dao/impl/Table3Dao.java
----------------------------------------------------------------
package com.pandy.ssh4.dao.impl; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.stat.Statistics; import org.springframework.stereotype.Repository; import com.pandy.ssh4.dao.BasicDao; @Repository public class Table3Dao extends BasicDao { @SuppressWarnings("rawtypes") public void doEhcache1() { Session session = getCurrentSession(); String hql = " from com.pandy.ssh4.domian.Table3"; Query query = session.createQuery(hql); query.setCacheable(true);//设定使用缓存 List list = query.list(); System.out.println(list.size()); } @SuppressWarnings("rawtypes") public void doEhcache2() { Session session = getCurrentSession(); String hql = " from com.pandy.ssh4.domian.Table3"; Query query = session.createQuery(hql); query.setCacheable(true);//设定使用缓存 List list = query.list(); System.out.println(list.size()); } public void printCacheInfo() { Statistics s = getSessionFactory().getStatistics(); System.out.println(s); System.out.println("put:" + s.getSecondLevelCachePutCount()); System.out.println("hit:" + s.getSecondLevelCacheHitCount()); System.out.println("miss:" + s.getSecondLevelCacheMissCount()); } }
注意,query.setCacheable(true);//设定使用缓存是设定了缓存
src/main/java/com/pandy/ssh4/service/SampleService.java
-------------------------------------------------------------
package com.pandy.ssh4.service; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.pandy.ssh4.dao.BasicDao; import com.pandy.ssh4.dao.impl.Table3Dao; import com.pandy.ssh4.domian.Table3; @Service @Transactional @SuppressWarnings("rawtypes") public class SampleService { @Resource(name = "table3Dao") public Table3Dao table3Dao; /** * 对单表增、删、改应用方式文字描述和示例 */ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class) public void sample1() { // 增加 Table3 domain = new Table3(); domain.setName("pandy"); domain.setEmail("panyongzheng@163.com"); table3Dao.save(domain); System.out.println("----------------------------------------增加完成"); validation(table3Dao); // 修改 domain.setName("New Name"); table3Dao.saveOrUpdate(domain); System.out.println("----------------------------------------修改完成"); validation(table3Dao); // 删除 table3Dao.delete(domain); System.out.println("----------------------------------------删除完成 id=" + domain.getId()); validation(table3Dao); } /** * 对单表查询的应用方式文字描述和示例? */ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class) public void sample3() { System.out.println("----------------------------------------增加两个记录来测试缓存"); Table3 domain1 = new Table3(); domain1.setName("pandy1"); domain1.setEmail("panyongzheng@163.com"); table3Dao.save(domain1); Table3 domain2 = new Table3(); domain2.setName("pandy2"); domain2.setEmail("panyongzheng@163.com"); table3Dao.save(domain2); System.out.println("----------------------------------------增加两个记录信息"); List list = table3Dao.listAll("Table3"); for (Object o : list) { System.out.println("listAll-> " + o.toString()); } } @Transactional(propagation = Propagation.REQUIRED) public void validation(BasicDao dao) { String className = dao.getClass().getName(); className = className.substring(className.lastIndexOf(".") + 1, className.indexOf("Dao")); List list = dao.listAll(className); for (Object o : list) { System.out.println(" " + o.toString()); } System.out.println("验证完成"); } @Transactional(propagation = Propagation.REQUIRED) public void doEhcache() { System.out.println("---------------第一次查询--------------------"); table3Dao.doEhcache1(); System.out.println("---------------第二次查询--------------------"); table3Dao.doEhcache2(); System.out.println("---------------缓存信息--------------------"); table3Dao.printCacheInfo(); } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class) public void listAll() { Table3 t = new Table3(); t.setName("panyongzheng"); table3Dao.save(t); List list = table3Dao.listAll("Table3"); for (Object o : list) { System.out.println(o.toString()); } } }
src/main/java/com/pandy/ssh4/controller/TestController.java
-------------------------------------------------
package com.pandy.ssh4.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.pandy.ssh4.service.SampleService; @Controller public class TestController { @Resource(name = "sampleService") public SampleService sampleService; @RequestMapping(value = "/doTest.do") public String doTest() { // testService.listAll(); sampleService.sample1(); sampleService.sample3(); sampleService.doEhcache(); return "success"; } }
src/main/webapp/index.jsp
--------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试首页</title> </head> <body> <center><h1>首页</h1></center> <a href="doTest.do">go test</a> </body> </html>
src/main/webapp/views/success.jsp
---------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试首页</title> </head> <body> <center><h1>成功</h1></center> </body> </html>
src/main/webapp/views/failure.jsp
--------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试首页</title> </head> <body> <center><h1>失败</h1></center> </body> </html>
http://localhost:8080/JBPM5sh/doTest.do
-----------------------------------------------OUTPUT
12:47:13,628 INFO sqlonly:236 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy', NULL)
12:47:13,630 INFO sqltiming:357 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy', NULL) {executed in 0 msec}
12:47:13,638 WARN JdbcCoordinatorImpl:411 - HHH000387: ResultSet's statement was not registered
----------------------------------------增加完成
12:47:13,887 INFO sqlonly:236 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_
12:47:13,888 INFO sqltiming:357 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_ {executed in 1 msec}
Table3 [id=13, name=pandy, password=null, email=panyongzheng@163.com]
验证完成
----------------------------------------修改完成
12:47:13,917 INFO sqlonly:236 - update TABLE3 set EMAIL='panyongzheng@163.com', NAME='New Name', PASSWORD=NULL where ID=13
12:47:13,918 INFO sqltiming:357 - update TABLE3 set EMAIL='panyongzheng@163.com', NAME='New Name', PASSWORD=NULL where ID=13
{executed in 1 msec}
12:47:13,920 INFO sqlonly:236 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_
12:47:13,921 INFO sqltiming:357 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_ {executed in 1 msec}
Table3 [id=13, name=New Name, password=null, email=panyongzheng@163.com]
验证完成
----------------------------------------删除完成 id=13
12:47:13,924 INFO sqlonly:236 - delete from TABLE3 where ID=13
12:47:13,925 INFO sqltiming:357 - delete from TABLE3 where ID=13 {executed in 0 msec}
12:47:13,926 INFO sqlonly:236 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_
12:47:13,927 INFO sqltiming:357 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_ {executed in 1 msec}
验证完成
----------------------------------------增加两个记录来测试缓存
12:47:14,021 INFO sqlonly:236 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy1', NULL)
12:47:14,022 INFO sqltiming:357 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy1', NULL)
{executed in 0 msec}
12:47:14,023 WARN JdbcCoordinatorImpl:411 - HHH000387: ResultSet's statement was not registered
12:47:14,024 INFO sqlonly:236 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy2', NULL)
12:47:14,025 INFO sqltiming:357 - insert into TABLE3 (EMAIL, NAME, PASSWORD) values ('panyongzheng@163.com', 'pandy2', NULL)
{executed in 0 msec}
12:47:14,026 WARN JdbcCoordinatorImpl:411 - HHH000387: ResultSet's statement was not registered
----------------------------------------增加两个记录信息
12:47:14,028 INFO sqlonly:236 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_
12:47:14,029 INFO sqltiming:357 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_ {executed in 1 msec}
listAll-> Table3 [id=14, name=pandy1, password=null, email=panyongzheng@163.com]
listAll-> Table3 [id=15, name=pandy2, password=null, email=panyongzheng@163.com]
---------------第一次查询--------------------
12:47:14,103 INFO sqlonly:236 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_
12:47:14,104 INFO sqltiming:357 - select table3x0_.ID as ID1_0_, table3x0_.EMAIL as EMAIL2_0_, table3x0_.NAME as NAME3_0_, table3x0_.PASSWORD
as PASSWORD4_0_ from TABLE3 table3x0_ {executed in 1 msec}
2
---------------第二次查询--------------------
2
---------------缓存信息--------------------
Statistics[start time=1368852423495,sessions opened=3,sessions closed=2,transactions=2,successful transactions=2,optimistic lock failures=0,flushes=3,connections obtained=3,statements prepared=10,statements closed=0,second level cache puts=2,second level cache hits=0,second level cache misses=0,entities loaded=2,entities updated=1,entities inserted=3,entities deleted=1,entities fetched=0,collections loaded=0,collections updated=0,collections removed=0,collections recreated=0,collections fetched=0,naturalId queries executed to database=0,naturalId cache puts=0,naturalId cache hits=0,naturalId cache misses=0,naturalId max query time=0,queries executed to database=5,query cache puts=1,query cache hits=1,query cache misses=1,update timestamps cache puts=7,update timestamps cache hits=1,update timestamps cache misses=0,max query time=12]
put:2
hit:0
miss:0
注意到第二次查询。不再查询数据库,就是缓存了。
评论
2 楼
pyzheng
2013-09-30
vip_wst 写道
我去 没有附近的啊
什么意思 没有附件?有pom.xml还没足够么?
1 楼
vip_wst
2013-09-30
我去 没有附近的啊
发表评论
-
Spring Boot 属性配置
2016-06-24 11:04 1180Spring Boot 属性配置和使用 http://blog ... -
Spring Boot 集成MyBatis
2016-06-24 10:55 2024Spring Boot 集成MyBatis http://bl ... -
Spring MVC防重复提交
2016-06-17 15:47 1643http://my.oschina.net/zyqjustin ... -
Spring容器加载完之后执行特定任务
2016-06-17 15:36 2281http://my.oschina.net/simpleton ... -
使用spring-session和shiro来代理session的配置
2016-06-16 11:21 12055使用spring-session和redis来代理sessio ... -
JSTL 的 if else : 有 c:if 没有 else 的处理
2016-06-14 09:52 1332http://blog.csdn.net/xiyuan1999 ... -
spring mvc 请求转发和重定向
2016-06-14 09:48 1396http://blog.csdn.net/jackpk/art ... -
mvc:view-controller
2016-05-18 10:26 1080http://blog.csdn.net/lzwglory/a ... -
spring配置事物的方式:注解和aop配置
2016-05-14 00:26 4101参考: Spring AOP中pointcut express ... -
分布式任务调度组件 Uncode-Schedule
2016-05-13 14:47 2286http://www.oschina.net/p/uncode ... -
写个mybatis的拦截插件,实现将所有执行的sql写入文件里
2016-05-12 15:59 5103原文 http://3131854.blog.51cto.co ... -
Mybatis分库分表扩展插件
2016-05-12 15:47 1620http://fangjialong.iteye.com/bl ... -
spring+mybatis+atomikos 实现JTA事务
2016-05-11 22:00 5521sping配置多个数据源 不同用户操作不同数据库 http:/ ... -
Spring中使用注解 @Scheduled执行定时任务
2016-05-10 09:39 1565原文:http://dwf07223.blog.51cto.c ... -
Spring中配置Websocket
2016-05-05 16:55 1275spring+websocket整合(springMVC+sp ... -
redis 集群中Session解决方案之Spring Session
2016-05-04 08:54 1313集群中Session解决方案之Spring Session h ... -
使用Spring-data进行Redis操作
2016-05-04 08:54 4790使用Spring-data进行Redis操作 http://z ... -
Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC
2016-05-03 13:35 1059Spring4新特性——集成Bean Validation 1 ... -
SpringMVC介绍之Validation
2016-05-03 13:10 982SpringMVC介绍之Validation http://h ... -
spring 注解方式下使用commons-validator 验证表单
2016-05-03 11:08 3075原文: http://www.programgo.com/ar ...
相关推荐
最新的springMVC+srping3.2+hibernate4.2的一个登陆示例,连接池使用bonecp,中间加了一个检测页面URL合法性的filter,里面可以增加session检测等,适用于此框架的初学者(因只是通过一个登陆页面将框架构建出来) 注意:这...
本项目框架搭建基于"spring5.x+springmvc5.x+mybatis3.5x+tomcat9+jdk8+maven",这是一个经典且广泛使用的Java开发配置。下面将详细阐述这些组件的功能、相互关系以及如何进行集成。 1. **Spring 5.x**: Spring是...
整合Spring MVC、Spring 5.x和Hibernate 5.x,我们可以构建一个强大的后端系统。首先,我们需要在Spring配置中声明Hibernate的DataSource和SessionFactory,然后通过Spring的JdbcTemplate或Hibernate的Session接口...
Hibernate4.2+Spring3.2+SpringMVC+shiro1.2.2+Maven整合和简单的登陆,导入工程后见description,内容简单基础,想做成脚手架供自己以后使用,望高手勿喷有什么宝贵意见望留言,谢谢
springMVC3.2+Hibernate4+freemarker 代码框架采用springMVC3.2.4+hibernate4.2.8+freemarker2.3.16 功能方面只是一个简单的注册登录,前台使用freemarker渲染,使用了freemarker自定义标签。
在本文中,我们将深入探讨如何配置一个基于SpringMVC 3.2和Hibernate 4的基础项目。这个项目配置涵盖了核心的框架组件,如数据库连接池、事务管理、缓存策略以及拦截器,这些是构建现代Java Web应用的关键要素。我们...
以前都是用SSH,第一次使用spring+springMVC+mybatis自己做的测试练习,主要是配置注解的使用,加了稍微详细的注释说明。另外在测试ehcache的时候也继承进去了,紧做练习参考. 环境:tomcat使用7.x,JDK1.7开发工具...
maven3.05+springmvc3.2+spring3.2+hibernate3.6重写传智播客OA源代码。菜单部分采用ztree.界面有一部分使用bootstrap3.11。听了汤阳光大神的视频收获很大,也学习springmvc,期间有一个想法用springmvc来改写汤大神...
SSH整合是Java Web开发中的一个经典组合,由Spring、Struts1.x和Hibernate三个框架构成。这个整合提供了全面的企业级应用解决方案,涵盖了控制层、业务层和服务数据持久化层的管理。下面将详细讲解SSH整合的主要知识...
Spring MVC、Spring 和 Hibernate 是Java Web开发中的三大主流框架,它们各司其职,共同构建了一个强大而灵活的后端架构。Spring MVC 负责处理HTTP请求并将其路由到相应的控制器,Spring 提供了依赖注入(DI)和面向...
在本项目中,我们主要探讨的是如何将Spring MVC、Spring 4.x 和 Hibernate 4.x 框架整合,以实现对学生信息的CRUD(创建、读取、更新和删除)操作。数据库方面,选用的是Microsoft的SQL Server 2005。下面,我们将...
在SpringMVC+Spring3.2+Hibernate4的整合中,Spring管理着所有bean,包括SpringMVC的控制器、服务层接口及实现、以及Hibernate的SessionFactory和DAO。通过Spring的TransactionManagement,可以方便地进行事务控制,...
SpringMVC、Spring3和Hibernate4是Java Web开发中三个非常重要的框架,它们共同构建了一个强大的后端架构。SpringMVC作为Spring框架的一部分,主要负责处理HTTP请求和响应,Spring3则提供了依赖注入(DI)和面向切面...
springMVC+maven+hibernate框架,搭建的过程 可以参考 java进阶(五)------springMVC---springMVC+Hibernate+maven完整搭建运行步骤 http://blog.csdn.net/zzq900503/article/details/49892783
基于springmvc+Hibernate实现的在线购物商城.zip基于springmvc+Hibernate实现的在线购物商城.zip基于springmvc+Hibernate实现的在线购物商城.zip基于springmvc+Hibernate实现的在线购物商城.zip基于springmvc+...
**SpringMVC+Spring3.2+Hibernate4整合详解** 在现代Java开发中,Spring框架以其模块化、可扩展性和企业级应用支持而备受青睐。本篇将详细讲解如何将SpringMVC、Spring3.2和Hibernate4进行整合,构建一个高效、灵活...
SpringMVC+Hibernate+annotation+Easyui权限管理系统,Session过期控制(包括Ajax请求的过期控制),访问权限控制(权限控制到按钮),hibernate数据库用户名与密码加密,其它的不说了,绝对物有所值
1. 工具和版本选择:在搭建Spring+SpringMVC+JPA+Hibernate平台时,可以选择myeclipse8.6作为开发工具,并使用mysql5.1作为数据库。作者建议使用Spring2.5和Hibernate3.3版本来避免jar包冲突,相较于Spring2.5和...