当前位置: 代码迷 >> Java Web开发 >> struts2+hibernate+spring项目action中无法更新和添加数据解决思路
  详细解决方案

struts2+hibernate+spring项目action中无法更新和添加数据解决思路

热度:8997   发布时间:2016-04-10 22:54:12.0
struts2+hibernate+spring项目action中无法更新和添加数据
更新数据库的action
package com.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.dao.ChanpinxinxiDao;
import com.dao.KehuxinxiDao;
import com.dao.ProducttypeDao;
import com.dao.TypeDao;
import com.entity.Chanpinxinxi;
import com.entity.Kehuxinxi;
import com.entity.Producttype;
import com.entity.Type;

/**
 * 修改产品信息
 * @author think
 *
 */
@Controller
public class ToupdateproductAction {
@Resource ChanpinxinxiDao cpxxdao;
@Resource TypeDao typedao;
@Resource ProducttypeDao protypedao;
@Resource KehuxinxiDao khxxdao;
//input
private int id;
private int pstate;
private int types;
private String pname;
private double price;
private String pspecs;

//output
private boolean ok=false;
public String execute(){
Producttype protype=new Producttype();
Type type=new Type();
Chanpinxinxi cpxx=new Chanpinxinxi();
Kehuxinxi khxx=new Kehuxinxi();
try {
protype=protypedao.findById(types);
type=typedao.findById(pstate);
khxx=khxxdao.findById(1);
cpxx=cpxxdao.findById(id);
cpxx.setChanpinmingcheng(pname);
cpxx.setProducttype(protype);
cpxx.setType(type);
cpxx.setJiage(price);
cpxx.setGuige(pspecs);
cpxx.setKehuxinxi(khxx);
cpxxdao.updateCPXX(cpxx);
ok=true;

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
ok=false;
}
return "success";
}
public int getPstate() {
return pstate;
}
public void setPstate(int pstate) {
this.pstate = pstate;
}
public int getTypes() {
return types;
}
public void setTypes(int types) {
this.types = types;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPspecs() {
return pspecs;
}
public void setPspecs(String pspecs) {
this.pspecs = pspecs;
}
public boolean isOk() {
return ok;
}
public void setOk(boolean ok) {
this.ok = ok;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}


}

dao的实现类的方法
package com.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dao.ChanpinxinxiDao;
import com.entity.Chanpinxinxi;

@Transactional
public class ChanpinxinxiDaoImpl implements ChanpinxinxiDao{
@Resource SessionFactory factory;
@Transactional(propagation=Propagation.REQUIRED)
public void savaCPXX(Chanpinxinxi cpxx) throws Exception {
factory.getCurrentSession().saveOrUpdate(cpxx);

}
}

beans.xml
<?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/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
        ">
    <context:annotation-config />
<context:component-scan base-package="com" />

<aop:aspectj-autoproxy />  
  <bean id="dataSource"

        class="com.mchange.v2.c3p0.ComboPooledDataSource"
  相关解决方案