博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ssh2项目整合 struts2.1+hibernate3.3+spring3 基于hibernate注解和struts2注解
阅读量:6849 次
发布时间:2019-06-26

本文共 10231 字,大约阅读时间需要 34 分钟。

项目文件夹结构例如以下:

核心配置文件:

web.xml

index.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
openSessionInViewFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
openSessionInViewFilter
/*
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
applicationContext.xml

> <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.Driverjdbc.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)	 * 

Title: setServletRequest

*

Description: 方法描写叙述

* @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 */@Servicepublic class SysuserServiceImpl implements SysuserService {	@Resource	private SysuserDao sysuserDao;	/* (non-Javadoc)	 * 

Title: getSysUserInfo

*

Description: 方法描写叙述

* @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 */@Repositorypublic class SysUserInfoDaoImpl extends HibernateDaoSupport implements SysuserDao{	/* (non-Javadoc)	 * 

Title: getSysUserInfo

*

Description: 方法描写叙述

* @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); } }

你可能感兴趣的文章
中国已成为亚太区企业首选的外包基地
查看>>
IPSEC主模式分析三
查看>>
2011.11.04刚通过vcp认证,谈下感受
查看>>
gitlab部署、配置更改、备份及恢复
查看>>
CentOS7.2最小安装的虚拟机上安装MySQL 5.6.32
查看>>
saltstack之virt模块创建虚拟机分析之四,创建虚拟机
查看>>
beego中文文档完成+部分新功能和bugfix
查看>>
Emmet的一些新的用法【转】
查看>>
面试常备题-----jsp基础知识
查看>>
网线水晶头接法
查看>>
哈希-------开放寻址法-------暴雪哈希
查看>>
Oracle 存储过程
查看>>
基于语义网的自动问答技术研究项目源代码
查看>>
sql help cs
查看>>
关于COUNT STOPKEY的工作机制(转载)
查看>>
如何理解VB窗体中的scale类属性及width height属性之间的关系
查看>>
大叔也说Xamarin~Android篇~日志的记录
查看>>
(原創) 為什麼要學[計算機組織]? (日記)
查看>>
JS实现Trim()
查看>>
Linux信号说明列表
查看>>