首页 新闻 会员 周边

ssh存储过程无法改变数据库数据

0
悬赏园豆:15 [已解决问题] 解决于 2018-08-28 18:50

使用ssh存储过程实现购物车添加和商品列表的数据减少,程序执行成功但是数据库数据没变

java调用

public int add(int sid,int num,double total,int uid) {
        try {
            this.getSession().getNamedQuery("update").setParameter(0, sid).setParameter(1, num)
            .setParameter(2, total).setParameter(3, uid);
            return 1;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return 0;
        }
    }

调用mysql存储过程代码

 <sql-query name="update" callable="true">
        {call zcxg(?,?,?,?)}
    </sql-query>

mysql的存储过程详细代码

create procedure `zcxg`(in sid int,in num int,in total double,in uid int)
begin
update shop as s set s.`number`=(s.`number`-`num`) where s.`id`=`sid`;
insert into cart(number,sid,total,uid) values(`num`,`sid`,`total`,`uid`);
commit;
end
问题补充:

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:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    ">
    <bean id="propertyPlaceholderConfigurer" 
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>    
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation"
            value="classpath:hibernate.cfg.xml">
        </property>
    </bean>
    <bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:annotation-driven transaction-manager="htm"/>    
    <bean id="sd" class="dao.impl.ShopDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="ss" class="service.impl.ShopServiceImpl">
        <property name="sd" ref="sd"></property>
    </bean>
    <bean id="sa" class="action.ShopAction">
        <property name="ss" ref="ss"></property>
    </bean>
    
    <bean id="cd" class="dao.impl.CartDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="cs" class="service.impl.CartServiceImpl">
        <property name="cd" ref="cd"></property>
    </bean>
    <bean id="ca" class="action.CartAction">
        <property name="cs" ref="cs"></property>
    </bean>
</beans>

struts2配置

<?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.objectFactory" value="spring"></constant>
    <package name="t" extends="struts-default">
        <action name="cartadd" class="action.CartAction" method="add">
            <interceptor-ref name="paramsPrepareParamsStack"></interceptor-ref>
            <result name="success">index.jsp</result>
        </action>
    </package>
</struts>    

封装类对应的xml内容

<?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="model.Cart" table="cart" catalog="shop">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <many-to-one name="users" class="model.Users" fetch="select">
            <column name="uid" not-null="true" />
        </many-to-one>
        <many-to-one name="shop" class="model.Shop" fetch="select">
            <column name="sid" not-null="true" />
        </many-to-one>
        <property name="number" type="java.lang.Integer">
            <column name="number" not-null="true" />
        </property>
        <property name="total" type="java.lang.Double">
            <column name="total" precision="22" scale="0" not-null="true" />
        </property>
    </class>
     <sql-query name="update" callable="true">
        {call cl(?,?,?,?)}
    </sql-query>
</hibernate-mapping>
tpy的主页 tpy | 初学一级 | 园豆:13
提问于:2018-08-13 21:16
< >
分享
最佳答案
0

  this.getSession().getNamedQuery("update").setParameter(0, sid).setParameter(1, num)
            .setParameter(2, total).setParameter(3, uid);

换成

    String procdure = "{call cl(?,?,?,?)}";
            CallableStatement cs = this.getHibernateTemplate().getSessionFactory().getCurrentSession().connection().prepareCall(procdure);
            cs.setInt(1, sid);
            cs.setInt(2, num);
            cs.setDouble(3, total);
            cs.setInt(4, uid);
            cs.execute();
tpy | 初学一级 |园豆:13 | 2018-08-13 23:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册