szz0429 2011-03-19 15:11
浏览 227
已采纳

急(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条回答 默认 最新

  • lxbccsu 2011-03-20 11:44
    关注

    在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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?