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; // 用户名

	@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);
	}
}
时间: 2024-10-14 14:00:46

ssh2项目整合 struts2.1+hibernate3.3+spring3 基于hibernate注解和struts2注解的相关文章

SSH项目整合教学Eclipse搭建SSH(Struts2+Spring3+Hibernate3)

这篇博文的目的 尝试搭建一个完整的SSH框架项目. 给以后的自己,也给别人一个参考. 读博文前应该注意: 本文提纲:本文通过一个用户注册的实例讲解SSH的整合.创建Struts项目,整合Hibernate,整合Spring.最后总结如何熟练创建SSH项目. 仅是创建SSH项目,对于其他的扩展例如Struts的国际化,Hibernate的缓存优化,Spring的AOP等,本博文涉及不到.想学习更多的东西请搜索其他博文. 本项目的环境:Windows 8-64位,Eclipse Indigo Ser

Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合

java教程|Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合教程并测试成功一.创建项目二.搭建struts-2.3.4.11.struts2必须的Jar包(放到WEB-INF/... Struts2.3.4.1+Spring3.2.3+Hibernate4.1.9整合教程并测试成功 一. 创建项目 二. 搭建struts-2.3.4.1 1.struts2必须的Jar包(放到WEB-INF/lib目录下): 2.配置struts2.3的过滤器 web.xml

MyEclipse搭建SSH(Struts2+Spring2+Hibernate3)框架项目教程

对Struts.spring.hibernate大体上了解一遍后,就是针对这个几个框架的整合了.如何整合,请看下面: 第一:Struts2的jar和xml配置文件: jar包: commons-fileupload-1.2.1.jar:文件上传 commons-io-1.3.2.jar:文件读取工具类 freemarker-2.3.15.jar:模板引擎,基于模板生成文本输出的通用工具. ognl-2.7.3.jar:功能强大的表达式语言,替代EL表达式,进行数据绑定和显示 struts2-co

eclipse环境下基于已构建struts2项目整合spring+hibernate

本文是基于已构建的struts2项目基础上整合 spring+hibernate,若读者还不熟悉struts2项目,请先阅读 实现步骤: 第一步:引入spring所需jar包,如下图所示: 第二步:导入hibernate所需jar包,如下图中被选中jar文件: 第三步:导入struts-spring整合包,暂且就这么称呼吧 第四步:导入MySQL驱动包: 第五步:所有准备工作就绪后,接下来创建spring与hibernate配置文件,命名为applicationContext.xml,配置如下所

Maven - 构建ssh2项目

一.数据库准备 1.创建数据库maven create database maven character set utf8 collate utf8_general_ci; //use maven; 2.创建用户表 create table t_users( id int primary key auto_increment, username varchar(30) not null, password varchar(50) not null, constraint unq_users_us

SSh框架搭建(Struts2.1+Hibernate4.0+Spring3.1)

一.使用工具 开发工具:My Eclipse 10.7以及相应的java开发工具 框架版本:Struts2.1+Hibernate3+Spring3.0 数据库:MySql5.5 Mysql可视化工具:Navicat 11 所需软件的下载地址:Navicat 11:http://pan.baidu.com/s/1nthrWtb                                MyEclipse:http://www.xpgod.com/soft/7498.html         

在SSH2项目中添加JBPM4的开发环境

1.导入JBPM4开发所需的jar包,注意不要和原有的SSH2jar包冲突,启动tomcat,如果正常,则进行下一步的操作 2.将下面的JBPM4的映射文件代码,写入到hibernate.cfg.xml配置文件中: <!-- 导jpbm入映射文件 --> <mapping resource="jbpm.repository.hbm.xml" /> <mapping resource="jbpm.execution.hbm.xml" /&

ssh2框架整合详细版(使用较新jar包)

ssh2框架整合+Maven+详细配置.多种方式介绍,中间碰到问题与注意点,部分已有指出. 关于Eclipse中使用Maven详见http://blog.csdn.net/it_faquir/article/details/54562242 步骤一 导入必备junit.javax.servlet相关jar包,maven配置如下: <dependency> <groupId>junit</groupId> <artifactId>junit</artif

ExtJs、Struts2、Hibernate3.2登录页面的简单实现

1.思想的大致模型 2.建立数据库test和数据库表tb_user 1 CREATEDATABASE `test`;2  CREATETABLE `test`.`tb_user` (3 `username` varchar(20) NOTNULL,4 `password` varchar(20) NOTNULL,5 PRIMARYKEY (`username`)6 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 3.加入基本的struts包 3.1.加入struts2