关于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" name="code">dataSource.password=rootdataSource.username=rootdataSource.databaseName=DemodataSource.driverClassName=com.mysql.jdbc.DriverdataSource.dialect=org.hibernate.dialect.MySQLDialectdataSource.serverName=localhost:3306dataSource.url=jdbc:mysql://localhost:3306/Demo?autoReconnect=true&characterEncoding=utf-8dataSource.auto=falsedataSource.show_sql=falsedataSource.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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/aophttp://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" 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" 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" ref="sessionFactory"/></bean><tx:annotation-driven transaction-manager="txManager"/><bean id="personService" scope="prototype"/></beans>
?5.log4j.properties配置
# Set The RootLoggerlog4j.rootLogger=warn, console# Direct Log Messages To Consolelog4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.Target=System.outlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %c:%L - %m%n# Log Hibernatelog4j.logger.org.hibernate=error# Log Just The SQLlog4j.logger.org.hibernate.SQL=debug# Log Schema Export Updatelog4j.logger.org.hibernate.tool.hbm2ddl=debug
?6. interface层
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)
@Transactionalpublic 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)
@Transactionalpublic 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层
@Controller("loginAction")public class PersonManageAction extends ActionSupport{/** * */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;}@Overridepublic String execute() throws Exception {HttpServletRequest request = ServletActionContext.getRequest();personService.save(new Person(getName()));request.setAttribute("message", "添加成功");return SUCCESS;}}