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

坚持2012 2014-01-16 12:23:30
更新数据库的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"

destroy-method="close">

<property name="driverClass">

<value>com.mysql.jdbc.Driver</value>

</property>

<property name="jdbcUrl">

<value>jdbc:mysql://localhost:3306/jxcgl?useUnicode=true&characterEncoding=UTF-8</value>

</property>

<property name="user">

<value>root</value>

</property>

<property name="password">

<value>19880222</value>

</property>

<property name="minPoolSize">

<value>3</value>

</property>

<property name="maxPoolSize">

<value>20</value>

</property>

<property name="initialPoolSize">

<value>10</value>

</property>
<property name="maxIdleTime">

<value>60</value>

</property>
<property name="acquireIncrement">

<value>5</value>

</property>
<property name="maxStatements">

<value>0</value>
</property>
<property name="idleConnectionTestPeriod">

<value>60</value>

</property>

<property name="acquireRetryAttempts">

<value>30</value>

</property>

<property name="breakAfterAcquireFailure">

<value>true</value>

</property>

<property name="testConnectionOnCheckout">

<value>false</value>

</property>

</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/entity/User.hbm.xml</value>
<value>com/entity/Dept.hbm.xml</value>
<value>com/entity/Chanpinxinxi.hbm.xml</value>
<value>com/entity/Kehuxinxi.hbm.xml</value>
<value>com/entity/Type.hbm.xml</value>
<value>com/entity/Producttype.hbm.xml</value>
</list>

</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>
<!-- Create/update the database tables automatically when the JVM starts up
<prop key="hibernate.hbm2ddl.auto">update</prop> -->
<!-- Turn batching off for better error messages under PostgreSQL
<prop key="hibernate.jdbc.batch_size">100</prop> -->
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="connection.autocommit">true</prop>
</props>
</property>


</bean>
<!-- 以下为事务配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 使用基于注释式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>


<bean id="userDaoImpl" class="com.dao.impl.UserDaoImpl"></bean>
<bean id="chanpinxinxiDaoImpl" class="com.dao.impl.ChanpinxinxiDaoImpl"></bean>
<bean id="producttypeDaoImpl" class="com.dao.impl.ProducttypeDaoImpl"></bean>
<bean id="kehuxinxiDaoImpl" class="com.dao.impl.KehuxinxiDaoImpl"></bean>
<bean id="typeDaoImpl" class="com.dao.impl.TypeDaoImpl"></bean>

</beans>

struts-main.xml
<!-- 添加产品信息addProduct.jsp中的内容 -->
<action name="addchanpinxinxi" class="addchanpinxinxiAction">
<result name="success" type="json">
</result>
</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 AddchanpinxinxiAction extends XinGaoAction{
@Resource ProducttypeDao protypedao;
@Resource ChanpinxinxiDao cpxxdao;
@Resource KehuxinxiDao khxxdao;
@Resource TypeDao typedao;
private String pname;//名称
private int types;//类型
private String pspecs;//规格
private double price;//价格
private int pstate;//状态
private int kehus;//厂商

private boolean ok=false;
public String execute(){
Producttype protype=new Producttype();
Type type=new Type();
Kehuxinxi khxx=new Kehuxinxi();

Chanpinxinxi cpxx=new Chanpinxinxi();
try {
protype=protypedao.findById(types);
type=typedao.findById(pstate);
khxx=khxxdao.findById(kehus);
cpxx.setChanpinmingcheng(pname);
cpxx.setChanpinmobanbianhao(0);//产品编号
cpxx.setDingdanbianhao(0);//订单编号
cpxx.setGuige(pspecs);
cpxx.setJiage(price);
cpxx.setJine("");
cpxx.setKehuxinxi(khxx);
cpxx.setProducttype(protype);
cpxx.setType(type);
//保存产品信息
cpxxdao.savaCPXX(cpxx);
ok=true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
ok=false;
}
return "success";

}

public String getPname() {
return pname;
}

public void setPname(String pname) {
this.pname = pname;
}

public int getTypes() {
return types;
}

public void setTypes(int types) {
this.types = types;
}

public String getPspecs() {
return pspecs;
}

public void setPspecs(String pspecs) {
this.pspecs = pspecs;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public int getPstate() {
return pstate;
}

public void setPstate(int pstate) {
this.pstate = pstate;
}
public int getKehus() {
return kehus;
}
public void setKehus(int kehus) {
this.kehus = kehus;
}
public boolean isOk() {
return ok;
}
public void setOk(boolean ok) {
this.ok = ok;
}

}

说明:debug模式下,到action中的cpxxdao.savaCPXX(cpxx);这一步时跳到defaultinvoke.class,控制台只有select的查询没有insert的查询,用junit测试类测试dao的savaCPXX(cpxx)是正常的,并且数据库里也有数据,这是什么问题?
...全文
592 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
坚持2012 2014-01-22
  • 打赏
  • 举报
回复
以上没有正确答案,问题我自己解决了,是hibernate-validator.jar这个包的版本太低与hibernate3.jar没有hibernate-validator.jar包所调用的一个方法,所以导致程序无法执行,谢谢各位热心帮忙。
Gemerl 2014-01-18
  • 打赏
  • 举报
回复
他这spring完全不起作用呢?很多对象都自己new出来的。。。不是配置文件中已经注入好了吗。。怎么又去new呢?
坚持2012 2014-01-18
  • 打赏
  • 举报
回复
引用 20 楼 fw347969680 的回复:
[quote=引用 19 楼 fw347969680 的回复:] 你还是直接把你的项目打包,传到新浪爱问共享资料,然后给个链接吧,我去下载下来看下。
数据库建表语句不要忘了。[/quote] 哥么,623562021是我的qq,加我,我把项目发给你,你帮我看看,还好是自己做着玩的,不赶时间,不然我真不知道问题处在哪了,先谢了
fw347969680 2014-01-17
  • 打赏
  • 举报
回复
引用 13 楼 caochuankui 的回复:
[quote=引用 11 楼 fw347969680 的回复:] [quote=引用 10 楼 fw347969680 的回复:] [quote=引用 8 楼 caochuankui 的回复:] [quote=引用 2 楼 defonds 的回复:] 数据库里也有数据 是指数据持久化成功了?
是的持久化没问题,通过junit的@test测试了dao方法也没问题,就是放到action中调用就不能执行添加和更新操作了[/quote] 你是指test7这个测试方法运行后,可以插入的?但是放在Action中就不行了?[/quote] factory.getCurrentSession().saveOrUpdate(cpxx); 这个factory的值是从哪里来的?配置文件?注解?[/quote] 是的我在spring加上了事物的配置,启用了注解[/quote] 但是你的注解好像是错的, @Resource SessionFactory factory; factory应该改为sessionFactory,和配置文件里对应起来。
fw347969680 2014-01-17
  • 打赏
  • 举报
回复
引用 12 楼 licip 的回复:
你最好用hibernateTemplate这个模板来实现你的操作。
我也想让楼主这样改,但是他就是不改。
fw347969680 2014-01-17
  • 打赏
  • 举报
回复
引用 19 楼 fw347969680 的回复:
你还是直接把你的项目打包,传到新浪爱问共享资料,然后给个链接吧,我去下载下来看下。
数据库建表语句不要忘了。
fw347969680 2014-01-17
  • 打赏
  • 举报
回复
你还是直接把你的项目打包,传到新浪爱问共享资料,然后给个链接吧,我去下载下来看下。
fw347969680 2014-01-17
  • 打赏
  • 举报
回复
引用 17 楼 caochuankui 的回复:
[quote=引用 16 楼 fw347969680 的回复:] [quote=引用 13 楼 caochuankui 的回复:] [quote=引用 11 楼 fw347969680 的回复:] [quote=引用 10 楼 fw347969680 的回复:] [quote=引用 8 楼 caochuankui 的回复:] [quote=引用 2 楼 defonds 的回复:] 数据库里也有数据 是指数据持久化成功了?
是的持久化没问题,通过junit的@test测试了dao方法也没问题,就是放到action中调用就不能执行添加和更新操作了[/quote] 你是指test7这个测试方法运行后,可以插入的?但是放在Action中就不行了?[/quote] factory.getCurrentSession().saveOrUpdate(cpxx); 这个factory的值是从哪里来的?配置文件?注解?[/quote] 是的我在spring加上了事物的配置,启用了注解[/quote] 但是你的注解好像是错的, @Resource SessionFactory factory; factory应该改为sessionFactory,和配置文件里对应起来。[/quote] 如果真是factory的问题,那为什么查询语句可以?[/quote] 你查询语句的代码又没贴。所以我说好像,还不是很确定。
坚持2012 2014-01-17
  • 打赏
  • 举报
回复
引用 16 楼 fw347969680 的回复:
[quote=引用 13 楼 caochuankui 的回复:] [quote=引用 11 楼 fw347969680 的回复:] [quote=引用 10 楼 fw347969680 的回复:] [quote=引用 8 楼 caochuankui 的回复:] [quote=引用 2 楼 defonds 的回复:] 数据库里也有数据 是指数据持久化成功了?
是的持久化没问题,通过junit的@test测试了dao方法也没问题,就是放到action中调用就不能执行添加和更新操作了[/quote] 你是指test7这个测试方法运行后,可以插入的?但是放在Action中就不行了?[/quote] factory.getCurrentSession().saveOrUpdate(cpxx); 这个factory的值是从哪里来的?配置文件?注解?[/quote] 是的我在spring加上了事物的配置,启用了注解[/quote] 但是你的注解好像是错的, @Resource SessionFactory factory; factory应该改为sessionFactory,和配置文件里对应起来。[/quote] 如果真是factory的问题,那为什么查询语句可以?
坚持2012 2014-01-16
  • 打赏
  • 举报
回复
package test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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;

public class Testdao {
//	private static UserDao userDao;
//	private static MyFlowDao myflowdao;
//	private static TemplateDao templateDao;
	private static KehuxinxiDao khxxdao;
	private static ProducttypeDao protypedao;
	private static TypeDao typedao;
	private static ChanpinxinxiDao chanpinxinxidao;

	@BeforeClass
	public static void setUpBeforeClass()throws Exception {
		try{
			ApplicationContext act=new ClassPathXmlApplicationContext("beans.xml");
			chanpinxinxidao=(ChanpinxinxiDao) act.getBean("chanpinxinxiDaoImpl");
			khxxdao=(KehuxinxiDao) act.getBean("kehuxinxiDaoImpl");
			protypedao=(ProducttypeDao) act.getBean("producttypeDaoImpl");
			typedao=(TypeDao) act.getBean("typeDaoImpl");
			 System.out.println("加载成功!");
		}catch(RuntimeException e){
			e.printStackTrace();
		}

	}

	@Test
	public void test7(){
		Type type=new Type();
		Producttype protype=new Producttype();
		Kehuxinxi khxx=new Kehuxinxi();
		Chanpinxinxi cpxx=new Chanpinxinxi();
		try {
			type=typedao.findById(1);
			protype=protypedao.findById(2);
			khxx=khxxdao.findById(1);
			//cpxx=chanpinxinxidao.findById(13);
			cpxx.setGuige("套");
			cpxx.setChanpinmingcheng("万能机");
			cpxx.setChanpinmobanbianhao(0);
			cpxx.setDingdanbianhao(0);
			cpxx.setJiage(10000.0);
			cpxx.setKehuxinxi(khxx);
			cpxx.setType(type);
			cpxx.setProducttype(protype);
			chanpinxinxidao.savaCPXX(cpxx);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
这个是测试类的代码,运行时正常的,但是在action中就无法插入了?
坚持2012 2014-01-16
  • 打赏
  • 举报
回复
引用 9 楼 magi1201 的回复:
引用 7 楼 caochuankui 的回复:
亲,是不一样的,我在junit测试的时候都是可以的,就是放到action中就出问题了,在action不能插入和更新
楼主方便打断点吗,debug调试下,看具体哪一步出问题了
dao方法执行到返回值得时候就跳到defaultinvoke.class,然后就是无法回到程序中了
坚持2012 2014-01-16
  • 打赏
  • 举报
回复
引用 11 楼 fw347969680 的回复:
[quote=引用 10 楼 fw347969680 的回复:] [quote=引用 8 楼 caochuankui 的回复:] [quote=引用 2 楼 defonds 的回复:] 数据库里也有数据 是指数据持久化成功了?
是的持久化没问题,通过junit的@test测试了dao方法也没问题,就是放到action中调用就不能执行添加和更新操作了[/quote] 你是指test7这个测试方法运行后,可以插入的?但是放在Action中就不行了?[/quote] factory.getCurrentSession().saveOrUpdate(cpxx); 这个factory的值是从哪里来的?配置文件?注解?[/quote] 是的我在spring加上了事物的配置,启用了注解
licip 2014-01-16
  • 打赏
  • 举报
回复
你最好用hibernateTemplate这个模板来实现你的操作。
fw347969680 2014-01-16
  • 打赏
  • 举报
回复
引用 10 楼 fw347969680 的回复:
[quote=引用 8 楼 caochuankui 的回复:] [quote=引用 2 楼 defonds 的回复:] 数据库里也有数据 是指数据持久化成功了?
是的持久化没问题,通过junit的@test测试了dao方法也没问题,就是放到action中调用就不能执行添加和更新操作了[/quote] 你是指test7这个测试方法运行后,可以插入的?但是放在Action中就不行了?[/quote] factory.getCurrentSession().saveOrUpdate(cpxx); 这个factory的值是从哪里来的?配置文件?注解?
fw347969680 2014-01-16
  • 打赏
  • 举报
回复
引用 8 楼 caochuankui 的回复:
[quote=引用 2 楼 defonds 的回复:] 数据库里也有数据 是指数据持久化成功了?
是的持久化没问题,通过junit的@test测试了dao方法也没问题,就是放到action中调用就不能执行添加和更新操作了[/quote] 你是指test7这个测试方法运行后,可以插入的?但是放在Action中就不行了?
姜小白- 2014-01-16
  • 打赏
  • 举报
回复
引用 7 楼 caochuankui 的回复:
亲,是不一样的,我在junit测试的时候都是可以的,就是放到action中就出问题了,在action不能插入和更新
楼主方便打断点吗,debug调试下,看具体哪一步出问题了
坚持2012 2014-01-16
  • 打赏
  • 举报
回复
引用 2 楼 defonds 的回复:
数据库里也有数据 是指数据持久化成功了?
是的持久化没问题,通过junit的@test测试了dao方法也没问题,就是放到action中调用就不能执行添加和更新操作了
坚持2012 2014-01-16
  • 打赏
  • 举报
回复
引用 6 楼 fw347969680 的回复:
和你问题差不多的帖子:http://bbs.csdn.net/topics/390654972?page=1#post-396204410
亲,是不一样的,我在junit测试的时候都是可以的,就是放到action中就出问题了,在action不能插入和更新
fw347969680 2014-01-16
  • 打赏
  • 举报
回复
和你问题差不多的帖子:http://bbs.csdn.net/topics/390654972?page=1#post-396204410
fw347969680 2014-01-16
  • 打赏
  • 举报
回复
factory.getCurrentSession().saveOrUpdate(cpxx); 这句话错了。我只想说,你的代码根本没有把SSH整合好,SSH整合好的代码例子:http://blog.csdn.net/cai5/article/details/6565523。factory.getCurrentSession()用法:http://liusu.iteye.com/blog/380397。
加载更多回复(2)
SSH整合示例(详情见我博客专栏)之前的博客我们总结了spring基础、spring分别整合struts2hibernate、mybatis等,今天我们来同时整合下 struts、springhibernate,也就是所谓的 ssh 。 整合流程: 1 首先整合springhibernate,这次我们在spring 配置bean使用注解的方式 ,hibernate实体映射关系也使用注解的方式,配置完毕后用简单方法测试下hibernate是否整合成功。 a 加入支持:添加 spring核心包、hibernate 3.6 包、 spring整合hibernate包 , 在src下建立applicationContext.xml (先建立src下便于测试hibernate)。 b 编写实体类,加入hibernate注解,编写方法类测试类,在applicationContext.xml添加hibernate模板类配置以及包扫描语句 。在类添加spring bean注解。 c 测试类 主动解析applicationContext.xml ,获取bean 执行dao层方法进行测试 2 将struts2 整合进去, 这次在struts.xml我们使用通配符的方式配置action。 a 加入支持 : 添加struts2.3.15 必需包 以及 struts json包(ajax要用到),spring整合struts2包,spring web 包,在src目录下建立struts.xml,复制头文件进去。将applicationContext.xml移到WEB-INF目录下。web容器(web.xml)添加struts2 filter以及spring 监听器。 b 在struts.xml添加action,使用通配符的方式 , 注意这里和单独struts2不同的地方: class属性指向的是bean 的 id ,这里我们配置bean采用spring ioc注解的方式, 所以默认的bean的id 为 类名(首字母小写) c 编写action类、页面进行测试

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧