急(online wait!求解!!):S2SH为什么无法更新数据,帮我看看哪里有问题.

[color=red]struts.xml[/color]
[code="java"]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<!--

<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
    <default-action-ref name="index" />
    <action name="index">
        <result type="redirectAction">
            <param name="actionName">HelloWorld</param>
            <param name="namespace">/example</param>
        </result>
    </action>
</package>

-->

    <package name="default" namespace="/" extends="json-default">
    <action name="save" class="com.hyit.ttmsoge.action.SaveAction" method="saveMethod">
        <result type="json">
            <param name="defaultEncoding">utf-8</param>
        </result>
    </action>
</package>


<!-- Add packages here 

     <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" 
        value="applicationContext.xml,
            com/hyit/ttmsoge/action/actionContext.xml,
            com/hyit/ttmsoge/dao/daoContext.xml,com/hyit/ttmsoge/service/serviceContext.xml">
    </set-property>
 </plug-in>
-->


[/code]

[color=red]applicationContext.xml[/color]
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver">
    </property>
    <property name="url" value="jdbc:mysql://localhost/ttmsoge">
    </property>
    <property name="username" value="root"></property>
    <property name="password" value="0429"></property>
</bean>


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>com/hyit/ttmsoge/model/Exam.hbm.xml</value>
        </list>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置Advice(事务的传播特性) -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="del*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="search*" propagation="SUPPORTS" read-only="true" />
    </tx:attributes>
</tx:advice>

<import resource="com/hyit/ttmsoge/action/actionContext.xml" />
<import resource="com/hyit/ttmsoge/dao/daoContext.xml" />
<import resource="com/hyit/ttmsoge/service/serviceContext.xml" />


[/code]

[color=red]SaveAction:[/color]
[code="java"]
package com.hyit.ttmsoge.action;

import com.hyit.ttmsoge.model.Exam;
import com.hyit.ttmsoge.service.SaveService;
import com.opensymphony.xwork2.ActionSupport;

public class SaveAction extends ActionSupport{
private static final long serialVersionUID = 1L;

private String username;
private String game;
private SaveService saveService;
private boolean result;
public boolean isResult() {
    return result;
}

public void setResult(boolean result) {
    this.result = result;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getGame() {
    return game;
}

public void setGame(String game) {
    this.game = game;
}

public void setSaveService(SaveService saveService) {
    this.saveService = saveService;
}

public String saveMethod() throws Exception {
    System.out.println("进入saveMethod..."+username+"..."+game);
    this.saveService.saveMessage(username, game);
    this.setResult(true);
    return SUCCESS;
}

}
[/code]
[color=red]SaveService:[/color]
[code="java"]
package com.hyit.ttmsoge.service;

import com.hyit.ttmsoge.model.Exam;

public interface SaveService {
void saveMessage(String username,String game)throws Exception;
}
[/code]

[color=red]SaveServiceImpl :[/color]
[code="java"]
package com.hyit.ttmsoge.serviceImpl;

import com.hyit.ttmsoge.dao.SaveDao;
import com.hyit.ttmsoge.model.Exam;
import com.hyit.ttmsoge.service.SaveService;

public class SaveServiceImpl implements SaveService{
private SaveDao saveDao;

public SaveDao getSaveDao() {
    return saveDao;
}

public void setSaveDao(SaveDao saveDao) {
    this.saveDao = saveDao;
}

public void saveMessage(String username, String game) throws Exception {
    int id=1;
    Exam exam=this.saveDao.findExamById(id);
    if(exam!=null){
        exam.setUsername(username);
        exam.setGame(game);
        this.saveDao.save(exam);
    }
}

}

[/code]

[color=red]SaveDao :[/color]
[code="java"]
package com.hyit.ttmsoge.dao;

import java.util.ArrayList;

import com.hyit.ttmsoge.model.Exam;

public interface SaveDao {
Exam findExamById(int id)throws Exception;
void save(Exam exam)throws Exception;
}
[/code]

[color=red]SaveDaoImpl :[/color]
[code="java"]
package com.hyit.ttmsoge.daoImpl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hyit.ttmsoge.dao.SaveDao;
import com.hyit.ttmsoge.model.Exam;

public class SaveDaoImpl extends HibernateDaoSupport implements SaveDao{

public Exam findExamById(int id) throws Exception {
    List<Exam> examList=this.getHibernateTemplate().find("from Exam e where e.id=?",new Object[]{id});
    if(examList!=null&&examList.size()!=0){
        Exam exam=examList.get(0);
        return exam;
    }
    return null;
}

public void save(Exam exam) throws Exception {
    System.out.println("进入exam:"+exam.getUsername());
    this.getHibernateTemplate().merge(exam);
}

}
[/code]
[color=red]Exam:[/color]
[code="java"]
package com.hyit.ttmsoge.model;

public class Exam {
private int id;
private String username;
private String game;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getGame() {
return game;
}
public void setGame(String game) {
this.game = game;
}

}
[/code]
[color=red]Exam.hbm.xml:[/color]
[code="java"]
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">









[/code]
相关配置:
[color=red]actionContext.xml:[/color]
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<bean id="saveAction" class="com.hyit.ttmsoge.action.SaveAction" scope="prototype">
    <property name="saveService" ref="saveService" />
</bean>


[/code]
[color=red]serviceContext.xml[/color]
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<bean id="saveService" class="com.hyit.ttmsoge.serviceImpl.SaveServiceImpl">
    <property name="saveDao" ref="saveDao" ></property>
</bean>


[/code]
[color=red]daoContext.xml[/color]
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<bean id="saveDao" class="com.hyit.ttmsoge.daoImpl.SaveDaoImpl" >
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>


[/code]

总是无法保存修改后的数据.这是一个测试的例子,为了方便起见,我在其中自定义了一个id=1,为了找到id=1的数据,在修改它的其他内容.可是不管是update,还是merge都无法执行(无sql语句打印).很急,麻烦大家快点帮我看一下!!!

3个回答

在applicationContext.xml 文件中加上

[code="java"]



<!-- 切入点 -->

expression="execution(* com.hyit.ttmsoge.serviceImpl.*.*(..))" />

pointcut-ref="saveServicePointcut" />
/aop:config

[/code]

DAO中save方法中的merge()改为saveOrUpdate()方法。

下面是原因说明:
[url]
http://forum.springsource.org/showthread.php?t=14938
[/url]

[url]
http://wind6266.blog.163.com/blog/static/67109072011229532760/
[/url]

你web.xml里配置了事务了吗?要是没有配置事务,你的数据是没有办法保存的。

在applicationContext.xml 文件中加一句

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐