关于struts2.3.1.2_spring3.1.1_hibernate4.1.2整合总结
在struts2.3.1.2_spring3.1.1_hibernate4.1.2整合时,今天我是特别的郁闷,以前都好好的,结果下午就出现问题,苦逼的到现在才解决。
附件: 就是这个项目所须的jar包,以及项目结构。
由于ssh 的jar包太大,就没有上传原代码上来,抱歉,不对的地方希望大家指正。
1.web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app 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_3_0.xsd" version="3.0"> <!-- Welcome File List --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Spring Encoding Filter --> <filter> <filter-name>encodingFilter</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> <!-- Spring Encoding Filter Mapping --> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Struts2 Filter --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <!-- Struts2 Filter Mapping --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Log4j ConfigurationFile Location --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <!-- Spring Log4j Listener --> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!-- 如果没有context-param标签的话,contextLoaderListener就读取application.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--Spring的ApplicationContext 载入 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- Spring 刷新Introspector防止内存泄露 --> <listener> <listener-class> org.springframework.web.util.IntrospectorCleanupListener </listener-class> </listener> <!-- Spring Web Request Listener --> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> <!-- session超时定义,单位为分钟 --> <session-config> <session-timeout>20</session-timeout> </session-config> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.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> <!-- 开发模式下使用,这样可以打印出更详细的错误信息 --> <constant name="struts.devMode" value="false" /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <!-- 默认的视图主题 --> <constant name="struts.ui.theme" value="simple" /> <constant name="struts.objectFactory" value="spring" /> <package name="default" extends="struts-default,json-default,jfreechart-default,spring-default"> <default-action-ref name="index" /> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"/> </global-exception-mappings> <action name="mylogin" class="loginAction"> <result name="input">/WEB-INF/page/personlist.jsp</result> <result name="add">/WEB-INF/page/addperson.jsp</result> <result name="success">/WEB-INF/page/message.jsp</result> </action> </package> </struts>
3.database.properties配置
dataSource.password=root dataSource.username=root dataSource.databaseName=Demo dataSource.driverClassName=com.mysql.jdbc.Driver dataSource.dialect=org.hibernate.dialect.MySQLDialect dataSource.serverName=localhost:3306 dataSource.url=jdbc:mysql://localhost:3306/Demo?autoReconnect=true&characterEncoding=utf-8 dataSource.auto=false dataSource.show_sql=false dataSource.format_sql=false
4.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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- 添加对Annotation的支持 --> <context:annotation-config /> <!-- 配置数据源 --> <context:property-placeholder location="classpath:database.properties"/> <!-- DataSource --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${dataSource.driverClassName}" /> <property name="jdbcUrl" value="${dataSource.url}" /> <property name="user" value="${dataSource.username}" /> <property name="password" value="${dataSource.password}" /> <!-- 当连接池中的连接用完时,C3P0一次性创建新连接的数目2 --> <property name="acquireIncrement" value="5"></property> <!-- 初始化时创建的连接数,必须在minPoolSize和maxPoolSize之间 --> <property name="initialPoolSize" value="10"></property> <property name="minPoolSize" value="5"></property> <property name="maxPoolSize" value="20"></property> <!-- 最大空闲时间,超过空闲时间的连接将被丢弃 [需要注意:mysql默认的连接时长为8小时(28800)【可在my.ini中添加 wait_timeout=30(单位秒)设置连接超时】,这里设置c3p0的超时必须<28800] --> <property name="maxIdleTime" value="300"></property> <!-- 每60秒检查连接池中的空闲连接 --> <property name="idleConnectionTestPeriod" value="60"></property> <!-- jdbc的标准参数 用以控制数据源内加载的PreparedStatement数量,但由于预缓存的Statement属 于单个Connection而不是整个连接 --> <property name="maxStatements" value="20"></property> </bean> <!-- SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--ORM映射文件:mappingResources--> <!-- ORM目录 --> <property name="mappingResources"> <list> <value>cn/itcast/bean/Person.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.autoReconnect">true</prop> <!-- 数据库方言 --> <prop key="hibernate.dialect">${dataSource.dialect}</prop> <prop key="hibernate.hbm2ddl.auto">${dataSource.auto}</prop> <!-- 控制台是否打印SQL --> <prop key="hibernate.show_sql">${dataSource.show_sql}</prop> <!-- 控制台是否格式化SQL语句显示样式 --> <prop key="hibernate.format_sql">${dataSource.format_sql}</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> <!-- 是否使用二级缓存 --> <prop key="hibernate.cache.use_second_level_cache">true</prop> <!-- QueryCacheFactory的实现类 --> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> </props> </property> </bean> <!-- TransactionManager --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/> <bean id="loginAction" class="cn.itcast.web.PersonManageAction" scope="prototype"/> </beans>
5.log4j.properties配置
# Set The RootLogger log4j.rootLogger=warn, console # Direct Log Messages To Console log4j.appender.console=org.apache.log4j.ConsoleAppender 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} %p %c:%L - %m%n # Log Hibernate log4j.logger.org.hibernate=error # Log Just The SQL log4j.logger.org.hibernate.SQL=debug # Log Schema Export Update log4j.logger.org.hibernate.tool.hbm2ddl=debug
6. interface层
package cn.itcast.service; import java.util.List; import cn.itcast.bean.Person; public interface PersonService { public void save(Person person); public void update(Person person); public Person getPerson(Integer personid); public void delete(Integer personid); public List<Person> getPersons(); }
7. service 层 (Hibernate4 )
package cn.itcast.service.impl; import java.util.List; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import cn.itcast.bean.Person; import cn.itcast.service.PersonService; @Transactional public class PersonServiceBean implements PersonService { @Resource private SessionFactory sessionFactory; public void save(Person person){ sessionFactory.getCurrentSession().persist(person); } public void update(Person person){ sessionFactory.getCurrentSession().merge(person); } @Transactional(propagation=Propagation.REQUIRED,readOnly=true) public Person getPerson(Integer personid){ return (Person)sessionFactory.getCurrentSession().get(Person.class, personid); } public void delete(Integer personid){ sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession().load(Person.class, personid)); } @Transactional(propagation=Propagation.REQUIRED,readOnly=true) @SuppressWarnings("unchecked") public List<Person> getPersons(){ return sessionFactory.getCurrentSession().createQuery("from Person").list(); } }
上面的代码是Hibernate4 的结构,不注意的写成下面这样的代码,就会出现异常的:
org.hibernate.HibernateException: No Session found for current thread
8. service 层 (Hibernate3 )
@Transactional public class PersonServiceBean implements PersonService { @Resource private SessionFactory sessionFactory; public void save(Person person){ sessionFactory.getCurrentSession().persist(person); } public void update(Person person){ sessionFactory.getCurrentSession().merge(person); } @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true) public Person getPerson(Integer personid){ return (Person)sessionFactory.getCurrentSession().get(Person.class, personid); } public void delete(Integer personid){ sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession().load(Person.class, personid)); } @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true) @SuppressWarnings("unchecked") public List<Person> getPersons(){ return sessionFactory.getCurrentSession().createQuery("from Person").list(); } }
而Hibernate4 的写法,有些改变,不明显,而在service 实现类的写法是这样的:
@Transactional(propagation=Propagation.REQUIRED ,readOnly=true)
而Hibernate3 在service 实现类的写法是这样的:
@Transactional(propagation=Propagation.NOT_SUPPORTED ,readOnly=true)
其实大家都在Hibernate3 在对数据库查询操作时,是不需要开事务的。但是在Hibernate4 就不行了,我也没有想明天是为什么,可能是Hibernate4 必须 要这样的。
9. action层
package cn.itcast.web; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import cn.itcast.bean.Person; import cn.itcast.service.PersonService; @Controller("loginAction") public class PersonManageAction extends ActionSupport{ /** * @Controller("loginAction") */ private static final long serialVersionUID = -1625398401024059186L; @Resource PersonService personService; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); personService.save(new Person(getName())); request.setAttribute("message", "添加成功"); return "message"; } }
10. Bean层
package cn.itcast.bean; import java.io.Serializable; import javax.persistence.Entity; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; @Entity @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class Person implements Serializable { /** * */ private static final long serialVersionUID = 4518832830369121138L; private Integer id; private String name; public Person(){} public Person(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
11. Person xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.bean"> <class name="Person" table="person"> <cache usage="read-write" region="cn.itcast.bean.Person"/> <id name="id"> <generator class="native"/> </id> <property name="name" length="10" not-null="true"/> </class> </hibernate-mapping>