2 qq 35580552 qq_35580552 于 2017.01.12 18:12 提问

this.getHibernateTemplate().update(user);不执行修改操作

this.getHibernateTemplate().update(user);执行了新增操作却没有执行修改操作

重点就是一行代码“this.getHibernateTemplate().update(user);” 为什么执行不了修改操作而执行的是新增操作 而且我以前同样的写法却可以执行修改操作

//Dao层的实现类
 @Override
    public boolean update(User user) {
        // TODO Auto-generated method stub
        this.getHibernateTemplate().update(user);
        return true;
    }

//Service层实现类
@Override
    public boolean update(User user) {
        // TODO Auto-generated method stub
        return this.dao.update(user);
    }


//Action层
public String update(){
        boolean a = this.service.update(user);
        if (a) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }


//User实体类
package com.bochy.vo;

/**
 * User entity. @author MyEclipse Persistence Tools
 */

public class User implements java.io.Serializable {

    // Fields

    private Integer UId;
    private String UName;
    private Integer UAge;

    // Constructors

    /** default constructor */
    public User() {
    }

    /** minimal constructor */
    public User(Integer UId) {
        this.UId = UId;
    }

    /** full constructor */
    public User(Integer UId, String UName, Integer UAge) {
        this.UId = UId;
        this.UName = UName;
        this.UAge = UAge;
    }

    // Property accessors

    public Integer getUId() {
        return this.UId;
    }

    public void setUId(Integer UId) {
        this.UId = UId;
    }

    public String getUName() {
        return this.UName;
    }

    public void setUName(String UName) {
        this.UName = UName;
    }

    public Integer getUAge() {
        return this.UAge;
    }

    public void setUAge(Integer UAge) {
        this.UAge = UAge;
    }

}


//实体类映射文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.bochy.vo.User" table="user">
        <id name="UId" type="java.lang.Integer">
            <column name="u_id" />
            <generator class="increment" />
        </id>
        <property name="UName" type="java.lang.String">
            <column name="u_name" />
        </property>
        <property name="UAge" type="java.lang.Integer">
            <column name="u_age" />
        </property>
    </class>
</hibernate-mapping>


//Spring总配置文件
<!-- 导入本地的jdbc.properties -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:spring/jdbc.properties</value>
    </property>
</bean>
<!-- 导入dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 导入sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <!-- 导入dataSource -->
    <property name="dataSource">
        <ref bean="dataSource"></ref>
    </property>
    <!-- 配置文件 -->
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        </props>
    </property>
    <!-- 映射文件  -->
    <property name="mappingDirectoryLocations">
        <list>
            <value>classpath:com/bochy/vo</value>
        </list>
    </property>
</bean>
<import resource="applicationContext-user.xml"/>
<import resource="applicationContext-transaction.xml"/>

//Spring-user配置文件
<?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 id="userDao" class="com.bochy.dao.impl.UserDaoImpl">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>
<bean id="userService" class="com.bochy.service.impl.UserServiceImpl">
    <property name="dao">
        <ref bean="userDao"/>
    </property>
</bean>
<bean id="userAction" class="com.bochy.action.UserAction">
    <property name="service">
        <ref bean="userService"/>
    </property>
</bean>
</beans>

//spring事务管理配置文件
<?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 id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>
<!-- 生命策略 -->
<tx:advice id="tx" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>
<!-- AOP配置 -->
<aop:config>
    <aop:pointcut expression="execution(* com.bochy.service.impl.*.*(..))" id="p"/>
    <aop:advisor advice-ref="tx" pointcut-ref="p"/>
</aop:config>
</beans>


//Struts配置文件
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

    <constant name="struts.devMode" value="true"></constant>
    <package name="user" extends="struts-default" namespace="/user">
        <action name="find" class="userAction" method="find">
            <result name="success">/User_add_success.jsp</result>
        </action>
        <action name="add" class="userAction" method="add">
            <result name="success" type="redirectAction">find</result>
            <result name="error">/index.jsp</result>
        </action>
        <action name="findbyid" class="userAction" method="findbyid">
            <result name="success">/User_update.jsp</result>
        </action>
        <action name="update" class="userAction" method="update">
            <result name="success" type="redirectAction">find</result>
            <result name="error">/index.jsp</result>
        </action>
    </package>
</struts>

2个回答

JE_GE
JE_GE   2017.01.12 19:31

多半是 事务的问题了

qq_35580552
qq_35580552 怎样改能让修改操作正确执行 能看得出来么?
11 个月之前 回复
zzhao114
zzhao114   2017.02.10 16:00

http://blog.csdn.net/zzhao114/article/details/54913879 你试试用注释的方式能行么?如果行的话,就是你用aop管理事务那一块错了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!