`
yuanfen860913
  • 浏览: 119898 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

struts2+spring3.0+hibernate注解方式

阅读更多

最近用这三个东西开发了一个后台管理的程序,现在写出来希望能够讨论谈论,因为我刚开始弄有很多东西都还不知道,在这里写出来望大家发现到问题后能够给我留言,以便完善,呵呵~

第一步:导入工程需要的包,具体需要哪些包,如果有需要的话我可以提供,当然最好的就是去官网下载了,呵呵~

第二步:配置web.xml文件。我的web.xml文件如下:

view plaincopy to clipboardprint?
<?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">
<!-- 默认i18n资源文件 -->
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>i18n/messages</param-value>
</context-param>

<!--
Location of the Log4J config file, for initialization and refresh checks.
Applied by Log4jConfigListener.
-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.cssweb.tradeadmin.action.trade</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 著名 Character 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>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<session-config>
<session-timeout>30</session-timeout>
</session-config>

<!-- Error Page定义 -->
<error-page>
<error-code>500</error-code>
<location>/commons/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/commons/404.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/commons/403.jsp</location>
</error-page>

<welcome-file-list>
<!-- Redirects to "welcome.htm" for dispatcher handling -->
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>
<?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">
<!-- 默认i18n资源文件 -->
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>i18n/messages</param-value>
</context-param>

<!--
Location of the Log4J config file, for initialization and refresh checks.
Applied by Log4jConfigListener.
-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.cssweb.tradeadmin.action.trade</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 著名 Character 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>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<session-config>
<session-timeout>30</session-timeout>
</session-config>

<!-- Error Page定义 -->
<error-page>
<error-code>500</error-code>
<location>/commons/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/commons/404.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/commons/403.jsp</location>
</error-page>

<welcome-file-list>
<!-- Redirects to "welcome.htm" for dispatcher handling -->
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

第三步:配置strust.xml文件(由于我用的是注解驱动方式,所以只有这一个文件)

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="globalMessages" />
<constant name="struts.convention.package.locators" value="action" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- when you used to real please set false -->
<constant name="struts.devMode" value="true" />
<!-- 将struts2委托Spring管理 -->
<constant name="struts.objectFactory" value="spring" />

<package name="mydefault" extends="struts-default">

<global-results>
<!-- 下面定义的结果对所有的Action都有效 -->
<result name="exception">/commons/error.jsp</result>
<result name="login">/login/login.jsp</result>
</global-results>

<global-exception-mappings>
<!-- 指Action抛出Exception异常时,转入名为exception的结果。 -->
<exception-mapping exception="java.lang.Exception" result="exception"/>
</global-exception-mappings>
</package>

</struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="globalMessages" />
<constant name="struts.convention.package.locators" value="action" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- when you used to real please set false -->
<constant name="struts.devMode" value="true" />
<!-- 将struts2委托Spring管理 -->
<constant name="struts.objectFactory" value="spring" />

<package name="mydefault" extends="struts-default">

<global-results>
<!-- 下面定义的结果对所有的Action都有效 -->
<result name="exception">/commons/error.jsp</result>
<result name="login">/login/login.jsp</result>
</global-results>

<global-exception-mappings>
<!-- 指Action抛出Exception异常时,转入名为exception的结果。 -->
<exception-mapping exception="java.lang.Exception" result="exception"/>
</global-exception-mappings>
</package>

</struts>

第四步:配置spring的xml文件包含hibernate的引入

view plaincopy to clipboardprint?
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/jee http://www.springframework.org/schema/jee/spring-jee-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">

<context:component-scan base-package="com.cssweb.tradeadmin.action"/>

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />

<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/cssweb/tradeadmin/model/UserBasicInfo.hbm.xml</value>
<value>com/cssweb/tradeadmin/model/LogLoginToday.hbm.xml</value>
<value>com/cssweb/tradeadmin/model/CustHostInfo.hbm.xml</value>
</list>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>

<!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
<!--
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
-->


<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->

<!--
Activates various annotations to be detected in bean classes:
Spring's @Required and @Autowired, as well as JSR 250's @Resource.
-->
<context:annotation-config/>

<!--
Instruct Spring to perform declarative transaction management
automatically on annotated classes.
-->
<tx:annotation-driven/>

<!--
Exporter that exposes the Hibernate statistics service via JMX. Autodetects the
service MBean, using its bean name as JMX object name.
-->
<context:mbean-export/>


<aop:aspectj-autoproxy/>

<!-- PetClinic's central data access object: Hibernate implementation -->
<bean id="clinic" class="com.cssweb.tradeadmin.dao.hibernate.HibernateClinic"/>
</beans>
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/jee http://www.springframework.org/schema/jee/spring-jee-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">

<context:component-scan base-package="com.cssweb.tradeadmin.action"/>

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />

<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/cssweb/tradeadmin/model/UserBasicInfo.hbm.xml</value>
<value>com/cssweb/tradeadmin/model/LogLoginToday.hbm.xml</value>
<value>com/cssweb/tradeadmin/model/CustHostInfo.hbm.xml</value>
</list>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>

<!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
<!--
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
-->


<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->

<!--
Activates various annotations to be detected in bean classes:
Spring's @Required and @Autowired, as well as JSR 250's @Resource.
-->
<context:annotation-config/>

<!--
Instruct Spring to perform declarative transaction management
automatically on annotated classes.
-->
<tx:annotation-driven/>

<!--
Exporter that exposes the Hibernate statistics service via JMX. Autodetects the
service MBean, using its bean name as JMX object name.
-->
<context:mbean-export/>


<aop:aspectj-autoproxy/>

<!-- PetClinic's central data access object: Hibernate implementation -->
<bean id="clinic" class="com.cssweb.tradeadmin.dao.hibernate.HibernateClinic"/>
</beans>


第五步:在action包里面建action类:

view plaincopy to clipboardprint?
/**
* Copyright 2010 CssWeb Microsystems, Inc. All rights reserved.
* CssWeb PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
* @(#)LoginAction.java 1:58:01 PM Apr 15, 2010
*/
package com.cssweb.tradeadmin.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cssweb.common.exception.BussinessException;
import com.cssweb.common.util.MD5;
import com.cssweb.tradeadmin.dao.Clinic;
import com.cssweb.tradeadmin.model.UserBasicInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
* 交易系统后台登录
*
* @author hujun
* @version 1.0
* @see
* @since 1.0
*/
@Controller
@ParentPackage("mydefault")
@Namespace("/user")
@Scope("prototype")
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 7454734463309586123L;

private Log log = LogFactory.getLog(LoginAction.class);

private final Clinic clinic;

private String loginName;
private String loginPwd;
private String incode;

@Autowired
public LoginAction(Clinic clinic) {
this.clinic = clinic;
}

@Action(value="login",
results={@Result(name="success", location="/index.jsp", type="redirect"),
@Result(name="input", location="/login/login.jsp")})
public String login() {
System.out.println(">>>>>>>>>>>>>>>>>>>>" + loginName);
System.out.println(">>>>>>>>>>>>>>>>>>>>" + loginPwd);
System.out.println(">>>>>>>>>>>>>>>>>>>>" + incode);
String msg = "";
try {
Map session = ActionContext.getContext().getSession();
String vnum = (String)session.get("vnum");
if(vnum!=null&&!vnum.toLowerCase().equals(incode.toLowerCase())) {
msg="验证码不正确!";
}
else {
MD5 md5 = new MD5();
UserBasicInfo user = new UserBasicInfo();
user.setUserName(loginName);
user.setPassword(md5.getMD5ofStr(loginPwd));
List<?> list = this.clinic.queryList(user, 0, 1, null, false);
if(list!=null&&list.size()>0) {
session.put("userName", loginName);
session.put("password", loginPwd);
}
else {
msg="用户和密码不正确!";
}
}
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("msg", msg);
} catch (BussinessException e) {
log.error(e);
}
System.out.println(">>>>>>>>>>>>##########" + msg);
return msg.equals("") ? SUCCESS : "input";
}

public String getLoginName() {
return loginName;
}

public void setLoginName(String loginName) {
this.loginName = loginName;
}

public String getLoginPwd() {
return loginPwd;
}

public void setLoginPwd(String loginPwd) {
this.loginPwd = loginPwd;
}

public String getIncode() {
return incode;
}

public void setIncode(String incode) {
this.incode = incode;
}
}
/**
* Copyright 2010 CssWeb Microsystems, Inc. All rights reserved.
* CssWeb PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
* @(#)LoginAction.java 1:58:01 PM Apr 15, 2010
*/
package com.cssweb.tradeadmin.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cssweb.common.exception.BussinessException;
import com.cssweb.common.util.MD5;
import com.cssweb.tradeadmin.dao.Clinic;
import com.cssweb.tradeadmin.model.UserBasicInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
* 交易系统后台登录
*
* @author hujun
* @version 1.0
* @see
* @since 1.0
*/
@Controller
@ParentPackage("mydefault")
@Namespace("/user")
@Scope("prototype")
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 7454734463309586123L;

private Log log = LogFactory.getLog(LoginAction.class);

private final Clinic clinic;

private String loginName;
private String loginPwd;
private String incode;

@Autowired
public LoginAction(Clinic clinic) {
this.clinic = clinic;
}

@Action(value="login",
results={@Result(name="success", location="/index.jsp", type="redirect"),
@Result(name="input", location="/login/login.jsp")})
public String login() {
System.out.println(">>>>>>>>>>>>>>>>>>>>" + loginName);
System.out.println(">>>>>>>>>>>>>>>>>>>>" + loginPwd);
System.out.println(">>>>>>>>>>>>>>>>>>>>" + incode);
String msg = "";
try {
Map session = ActionContext.getContext().getSession();
String vnum = (String)session.get("vnum");
if(vnum!=null&&!vnum.toLowerCase().equals(incode.toLowerCase())) {
msg="验证码不正确!";
}
else {
MD5 md5 = new MD5();
UserBasicInfo user = new UserBasicInfo();
user.setUserName(loginName);
user.setPassword(md5.getMD5ofStr(loginPwd));
List<?> list = this.clinic.queryList(user, 0, 1, null, false);
if(list!=null&&list.size()>0) {
session.put("userName", loginName);
session.put("password", loginPwd);
}
else {
msg="用户和密码不正确!";
}
}
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("msg", msg);
} catch (BussinessException e) {
log.error(e);
}
System.out.println(">>>>>>>>>>>>##########" + msg);
return msg.equals("") ? SUCCESS : "input";
}

public String getLoginName() {
return loginName;
}

public void setLoginName(String loginName) {
this.loginName = loginName;
}

public String getLoginPwd() {
return loginPwd;
}

public void setLoginPwd(String loginPwd) {
this.loginPwd = loginPwd;
}

public String getIncode() {
return incode;
}

public void setIncode(String incode) {
this.incode = incode;
}
}

第六步:hibernate类,在这里action中的Clinic是接口,hibernate类集成这个接口

view plaincopy to clipboardprint?
@Autowired
private SessionFactory sessionFactory;

/**
* 保存(新建或者更新)
*/
public void merge(Object obj) throws BussinessException {
try {
Session session = sessionFactory.openSession();
Transaction t=session.beginTransaction();
session.merge(obj);
t.commit();
}catch(RuntimeException e){
log.error(e);
throw new BussinessException(e);
}
}

/**
* 保存(新建或者更新)
*/
public void save(Object obj) throws BussinessException {
try {
Session session = sessionFactory.openSession();
Transaction t=session.beginTransaction();
session.save(obj);
t.commit();
}catch(RuntimeException e){
log.error(e);
throw new BussinessException(e);
}
}

/**
* 保存(新建或者更新)
*/
public void update(Object obj) throws BussinessException {
try {
Session session = sessionFactory.openSession();
Transaction t=session.beginTransaction();
session.update(obj);
t.commit();
}catch(RuntimeException e){
log.error(e);
throw new BussinessException(e);
}
}
/**
* 保存(新建或者更新)
*/
public void saveOrupdate(Object obj) throws BussinessException {
try {
Session session = sessionFactory.openSession();
Transaction t=session.beginTransaction();
session.saveOrUpdate(obj);
t.commit();
}catch(RuntimeException e){
log.error(e);
throw new BussinessException(e);
}
}
@Autowired
private SessionFactory sessionFactory;

/**
* 保存(新建或者更新)
*/
public void merge(Object obj) throws BussinessException {
try {
Session session = sessionFactory.openSession();
Transaction t=session.beginTransaction();
session.merge(obj);
t.commit();
}catch(RuntimeException e){
log.error(e);
throw new BussinessException(e);
}
}

/**
* 保存(新建或者更新)
*/
public void save(Object obj) throws BussinessException {
try {
Session session = sessionFactory.openSession();
Transaction t=session.beginTransaction();
session.save(obj);
t.commit();
}catch(RuntimeException e){
log.error(e);
throw new BussinessException(e);
}
}

/**
* 保存(新建或者更新)
*/
public void update(Object obj) throws BussinessException {
try {
Session session = sessionFactory.openSession();
Transaction t=session.beginTransaction();
session.update(obj);
t.commit();
}catch(RuntimeException e){
log.error(e);
throw new BussinessException(e);
}
}
/**
* 保存(新建或者更新)
*/
public void saveOrupdate(Object obj) throws BussinessException {
try {
Session session = sessionFactory.openSession();
Transaction t=session.beginTransaction();
session.saveOrUpdate(obj);
t.commit();
}catch(RuntimeException e){
log.error(e);
throw new BussinessException(e);
}
}

由于部分代码设计到公司正在运营的项目,所以不能贴出忘大家原谅!另外如果大家对这块比较感兴趣的话希望可以多交流交流,比如我这里的struts.xml文件的配置不完整,总之我把我会的都写出来希望能够给大家个参考,同时也希望高手能够指点下,呵呵~

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiaojunhu/archive/2010/04/19/5500365.aspx

分享到:
评论

相关推荐

    搭建Struts2.1+Spring3.0+Hibernate3.3框架

    搭建Struts2.1+Spring3.0+Hibernate3.3框架是开发Java Web应用程序时常见的一种技术组合,也被称为SSH框架。这三个框架分别负责不同的职责:Struts2处理前端控制器,Spring提供依赖注入和事务管理,Hibernate则作为...

    struts1.2 + spring2.5 + hibernate3.2框架demo

    2. **配置文件**:struts-config.xml定义Struts的配置,spring-beans.xml管理Spring的bean,hibernate.cfg.xml配置Hibernate的数据库连接,可能还有实体类的映射文件(hbm.xml或使用注解)。 3. **JSP页面**:展示...

    Struts2+Spring3.0+MyBatis3.0平台搭建spring+json+gson+mysql,经典版本spring3.0+,完整架包

    标题中的"Struts2+Spring3.0+MyBatis3.0平台搭建"涉及到的是一个常见的企业级Java Web开发框架组合。这个平台基于Struts2作为MVC框架,Spring3.0作为核心容器,负责依赖注入(DI)和面向切面编程(AOP),而MyBatis...

    struts2.1+spring3.0+hibernate3.3整合小例子

    - 学习更多关于Struts2拦截器、Spring注解以及Hibernate高级查询技术等,以提升项目性能和可维护性。 - 探索不同数据库(如MySQL、SQL Server)的配置差异,增强自己的适应能力。 通过上述步骤和建议,开发者能够更...

    struts2.2.3+spring3.0.1+hibernate3.6.5整合jar包

    Struts2、Spring和Hibernate是Java开发中三大主流框架,它们的组合被称为SSH(Struts2、Spring、Hibernate)集成,常用于构建企业级的Web应用程序。本篇将详细讲解这三个框架的核心概念、功能以及它们如何协同工作。...

    struts1.3+spring3.0+hibernate3.3简单项目示例

    Struts1.3、Spring3.0和Hibernate3.3是经典的Java企业级开发框架组合,常被用于构建Web应用程序。这个"SSH"项目示例旨在帮助开发者理解如何将这三个框架整合并应用于实际项目中。下面我们将深入探讨这三个组件以及...

    struts1.2+spring2.3+hibernate3.0

    Struts1.2、Spring2.3 和 Hibernate3.0 是经典的Java企业级开发框架组合,被称为SSH(Struts-Spring-Hibernate)集成框架。这个组合在2000年代末到2010年初非常流行,它们各自解决了不同的问题,并通过相互配合,...

    Struts2+Spring3.0+Hibernate开发问题总结+知识讲解

    Struts2、Spring3.0和Hibernate是Java Web开发中的三大框架,它们分别负责MVC模式中的Action层、业务逻辑层以及数据访问层。本文将深入探讨这三大框架的集成应用,常见问题以及解决策略,并提供相关的知识讲解。 1....

    OA办公自动化管理系统(Struts1.2+Hibernate3.0+Spring2+DWR)

    这个系统采用了经典的Java技术栈,包括Struts1.2、Hibernate3.0、Spring2以及DWR,这些都是Java开发中的核心框架。 Struts1.2是MVC(Model-View-Controller)设计模式的实现,它在Web应用中负责处理请求、控制业务...

    Struts2+Spring3.0+MyBatis3.0

    Struts2、Spring3.0和MyBatis3.0是经典的Java Web开发框架组合,它们各自承担着不同的职责,协同工作以构建高效、灵活的应用系统。Struts2作为MVC(模型-视图-控制器)框架,负责处理HTTP请求并转发到相应的业务逻辑...

    struts2.0+spring3.0+hibernate3.2框架整合Demo

    Struts2.0、Spring3.0和Hibernate3.2是Java Web开发中经典的三大框架,它们各自负责不同的职责,并通过整合实现高效、灵活的应用开发。这个Demo是开发者利用一个周末时间,结合网上找到的jar包,精心构建的SSH框架...

    Struts2.0+Spring3.0+hibernate3.2 API(参考手册)

    Struts2.0、Spring3.0和Hibernate3.2是经典的Java企业级开发框架组合,通常被称为SSH(Struts2、Spring、Hibernate)架构。这个API参考手册提供了这三个框架的核心概念、用法和功能的详细说明。 Struts2.0是MVC...

    MyEclipse8.6+Struts2.1+Spring3.0+Hibernate3.3环境搭建.doc

    - 通过Add Hibernate Capabilities,选择Hibernate3.3版本,可以选择是否启用注解(影响实体类的映射方式)。 - Hibernate库:默认选择Hibernate 3.3 Annotation & Entity Manager和Hibernate 3.3 Core Libraries...

    struts2+hibernate3.3+spring3.0整合实例

    Struts2、Hibernate3.3和Spring3.0是Java Web开发中著名的三大框架,它们分别负责MVC模式中的Action层、持久化层和依赖注入与业务逻辑管理。本实例将详细阐述如何将这三个框架整合在一起,实现一个完整的CRUD(创建...

    最新版本Struts2.1.8+Spring3.0+Hibernate3.3.2+DWR2.0.6配置

    ### 最新版本Struts2.1.8+Spring3.0+Hibernate3.3.2+DWR2.0.6配置详解 在本篇内容中,我们将详细探讨如何搭建一个基于Struts2.1.8、Spring3.0、Hibernate3.3.2以及DWR2.0.6的Web应用程序框架。这一组合被称为SSH+...

    struts2+spring2.5+hibernate3.2 + freemarker 全新功能实现的增删改查+freemarker 摸版

    struts2+spring2.5+hibernate3.2 + freemarker 全新功能实现的增删改查+freemarker 摸版 struts2 的方式自己去看简单。 spring2.5 是用注释来注入 hibernate3.2 是用ejb3注解映射关系 hibernate3 +个属性可以自动...

    新版SSH+DWR配置(Struts2.1.8+Spring3.0+Hibernate3.3.2+DWR2.0.6).doc

    本文将详细介绍如何配置新版SSH+DWR框架,即Struts2.1.8、Spring3.0和Hibernate3.3.2与Direct Web Remoting (DWR) 2.0.6的集成。这个配置示例是基于目前最新的技术栈,旨在提供一个高效、稳定的开发环境。 首先,...

    Struts2+Spring4+Hibernate5整合

    这种整合方式的优势在于,Struts2处理用户交互,Spring提供整体的依赖管理和事务控制,Hibernate则简化了数据库操作。通过它们的协作,可以构建出松散耦合、可测试性高且具有良好架构的Java Web应用。然而,随着...

    struts2.2.1+spring 3.0.3+hibernate3.6+dwr3.0全注解整合包

    这个"struts2.2.1+spring 3.0.3+hibernate3.6+dwr3.0全注解整合包"是将这些框架结合在一起,使用注解方式配置,简化了传统的XML配置文件,提高了开发效率和代码的可读性。以下是关于这四个框架及其整合的知识点: 1...

Global site tag (gtag.js) - Google Analytics