81,092
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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);
}
}
<?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>
<!-- 添加产品信息addProduct.jsp中的内容 -->
<action name="addchanpinxinxi" class="addchanpinxinxiAction">
<result name="success" type="json">
</result>
</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;
}
}
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中就无法插入了?[quote=引用 10 楼 fw347969680 的回复:] [quote=引用 8 楼 caochuankui 的回复:] [quote=引用 2 楼 defonds 的回复:] 数据库里也有数据 是指数据持久化成功了?
[quote=引用 8 楼 caochuankui 的回复:] [quote=引用 2 楼 defonds 的回复:] 数据库里也有数据 是指数据持久化成功了?
[quote=引用 2 楼 defonds 的回复:] 数据库里也有数据 是指数据持久化成功了?
亲,是不一样的,我在junit测试的时候都是可以的,就是放到action中就出问题了,在action不能插入和更新
数据库里也有数据 是指数据持久化成功了?
和你问题差不多的帖子:http://bbs.csdn.net/topics/390654972?page=1#post-396204410