1. 程式人生 > >ssh2項目整合 struts2.1+hibernate3.3+spring3 基於hibernate註解和struts2註解

ssh2項目整合 struts2.1+hibernate3.3+spring3 基於hibernate註解和struts2註解

模塊 port aware -- ids tle des 項目 @service

項目文件夾結構例如以下:技術分享

核心配置文件:

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註解