ssh2项目整合 struts2.1+hibernate3.3+spring3 基于hibernate注解和struts2注解
Posted cxchanpin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh2项目整合 struts2.1+hibernate3.3+spring3 基于hibernate注解和struts2注解相关的知识,希望对你有一定的参考价值。
项目文件夹结构例如以下:
核心配置文件:
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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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>/*</url-pattern> </filter-mapping> </web-app>
applicationContext.xml
<?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: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-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 用注解方式注入bean --> <context:annotation-config /> <context:component-scan base-package="com.yza" /> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config/jdbc.properties</value> </list> </property> </bean> <!-- 数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.drive}"> </property> <property name="url" value="${jdbc.url}"> </property> <property name="username" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="defaultAutoCommit" value="false"></property> </bean> <!-- hibernate sessionFactory 创建 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--扫描包下的bean,不採用mapping开放性好--> <property name="packagesToScan"> <list> <value>com.yza.model.</value> <!-- 注意最后的".", 会转换成"classpath*:com/xxx/entity/**/*.class" --> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.mysqlDialect</prop> </props> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 这样的硬性写入太死。建议写一个主要的dao,再来继承,由于假设直接给HibernateDaoSupport或者不写都会报错 “sessionFactory' or 'hibernateTemplate' is required" --> <bean id="sysUserInfoDaoImpl" class="com.yza.sysuser.dao.SysUserInfoDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <!-- 事物配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(public * com.yza.service..*.*(..))" id="myPointcut" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" /> </aop:config> </beans>
jdbc:
jdbc.drive=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://10.6.0.56\:3306/jxproject?characterEncoding\=UTF-8&characterSetResults\=UTF-8&profileSQL\=true jdbc.user=admins jdbc.password=root
核心java代码
实体bean
/** * @Project: ssh2 * @Title: SysUserInfo.java * @Package com.yza.model * @author yongzhian * @date 2014-9-29 下午3:40:08 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */ package com.yza.model; import javax.persistence.Column; import javax.persistence.Entity;//一定要用这个包下的,不能是hibernate下的 import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; /** * @ClassName SysUserInfo * @Description 模块描写叙述 * @author yongzhian * @Date 2014-9-29 */ @Entity @Table(name = "user_baseinfo", catalog = "commonuser") public class SysUserInfo implements java.io.Serializable { /** * serialVersionUID */ private static final long serialVersionUID = 1L; @Id @Column(name = "info_id", unique = true, nullable = false) @GeneratedValue(strategy = GenerationType.AUTO) private Integer userId; // 主键 @Column(name = "info_name") private String infoName; // username @Column(name = "sex") private String sex; // 性别 /** * @return userId */ public Integer getUserId() { return userId; } /** * @param userId 要设置的 userId */ public void setUserId(Integer userId) { this.userId = userId; } /** * @return infoName */ public String getInfoName() { return infoName; } /** * @param infoName 要设置的 infoName */ public void setInfoName(String infoName) { this.infoName = infoName; } /** * @return sex */ public String getSex() { return sex; } /** * @param sex 要设置的 sex */ public void setSex(String sex) { this.sex = sex; } }action
/** * @Project: ssh2 * @Title: SysuserAction.java * @Package com.yza.sysuser.action * @author yongzhian * @date 2014-9-29 下午3:49:43 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */ package com.yza.sysuser.action; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionSupport; import com.yza.sysuser.dao.SysuserDao; import com.yza.sysuser.service.SysuserService; /** * @ClassName SysuserAction * @Description 模块描写叙述 * @author yongzhian * @Date 2014-9-29 */ @ParentPackage("struts-default") @Namespace("/sysuser") @Results({ @Result(name = "success" , location = "/pages/userinfo.jsp"), }) public class SysuserAction extends ActionSupport implements ServletRequestAware{ /** * serialVersionUID */ private static final long serialVersionUID = 1L; @Resource private SysuserService sysuserService; protected HttpServletRequest request; @Action("getUserInfo") public String getUserInfo(){ String userIdStr = this.request.getParameter("userId"); if(userIdStr != null){ request.setAttribute("user", sysuserService.getSysUserInfo(Integer.parseInt(userIdStr))); } request.setAttribute("tt", "12"); return "success"; } /* (non-Javadoc) * <p>Title: setServletRequest</p> * <p>Description: 方法描写叙述</p> * @param request * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest) */ @Override public void setServletRequest(HttpServletRequest request) { this.request = request; } }
service
/** * @Project: ssh2 * @Title: SysuserDao.java * @Package com.yza.sysuser.dao * @author yongzhian * @date 2014-9-29 下午4:19:27 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */ package com.yza.sysuser.service; import com.yza.model.SysUserInfo; /** * @ClassName SysuserDao * @Description 模块描写叙述 * @author yongzhian * @Date 2014-9-29 */ public interface SysuserService { public SysUserInfo getSysUserInfo(Integer userId); }
/** * @Project: ssh2 * @Title: SysuserServiceImpl.java * @Package com.yza.sysuser.service * @author yongzhian * @date 2014-9-29 下午4:24:16 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */ package com.yza.sysuser.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.yza.model.SysUserInfo; import com.yza.sysuser.dao.SysuserDao; /** * @ClassName SysuserServiceImpl * @Description 模块描写叙述 * @author yongzhian * @Date 2014-9-29 */ @Service public class SysuserServiceImpl implements SysuserService { @Resource private SysuserDao sysuserDao; /* (non-Javadoc) * <p>Title: getSysUserInfo</p> * <p>Description: 方法描写叙述</p> * @param userId * @return * @see com.yza.sysuser.service.SysuserService#getSysUserInfo(java.lang.Integer) */ @Override public SysUserInfo getSysUserInfo(Integer userId) { return sysuserDao.getSysUserInfo(userId); } }dao
/** * @Project: ssh2 * @Title: SysuserDao.java * @Package com.yza.sysuser.dao * @author yongzhian * @date 2014-9-29 下午4:19:27 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */ package com.yza.sysuser.dao; import com.yza.model.SysUserInfo; /** * @ClassName SysuserDao * @Description 模块描写叙述 * @author yongzhian * @Date 2014-9-29 */ public interface SysuserDao { public SysUserInfo getSysUserInfo(Integer userId); }
/** * @Project: ssh2 * @Title: SysUserInfoDaoImpl.java * @Package com.yza.sysuser.dao * @author yongzhian * @date 2014-9-29 下午4:21:16 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */ package com.yza.sysuser.dao; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.yza.model.SysUserInfo; /** * @ClassName SysUserInfoDaoImpl * @Description 模块描写叙述 * @author yongzhian * @Date 2014-9-29 */ @Repository public class SysUserInfoDaoImpl extends HibernateDaoSupport implements SysuserDao{ /* (non-Javadoc) * <p>Title: getSysUserInfo</p> * <p>Description: 方法描写叙述</p> * @param userId * @return * @see com.yza.sysuser.dao.SysuserDao#getSysUserInfo(java.lang.Integer) */ @Override public SysUserInfo getSysUserInfo(Integer userId) { return this.getHibernateTemplate().get(SysUserInfo.class, userId); } }
以上是关于ssh2项目整合 struts2.1+hibernate3.3+spring3 基于hibernate注解和struts2注解的主要内容,如果未能解决你的问题,请参考以下文章
Struts2.1.6 + Spring2.5+Hibernate3.2整合
Struts2.1.6+Spring2.0+Hibernate3.1整合图解教程(ssh223)
spring3.0整合异常,我的框架为struts2.1+hibernate3.0+spring3.0+mysql5.0+tomcat6.0
ssh2整合: No bean named 'sessionFactory' is defined(求大神指点)