首先声明下可能引起的版权问题,我做这个小系统用到的很多配置文件是从尚学堂的培训教程中的直接改过来的.希望尚学堂各位老师不要追究责任哈.嘎嘎感谢上个帖子大家的支持.有个问题我解释下,用SSH是因为是毕业设计,想把技术表现的全一点,把hibernate去掉感觉最难的少了空荡荡的.现在遇到的问题我贴下,请各位指教:
1.服务器正常启动,没有任何异常.
单元测试过程可以无异常跑完.
单元测试完成之后结果不能插入到数据库.
但是手动往数据库插入数据,可以发现每次插入都占用了id(自动递增的id).
前面遇到的一些框架版本等方面的问题已经解决,这次遇到这个数据库插入的问题实在怪异!!!
2.关于OrderItem类的设计
OrderItem我认为没有必要保存到数据库中,只是想在保存订单的时候保存为卖出商品id,数量,不知道这样是否显示.
另外,如果设计上让OrderItem存储到数据库,又该怎么样进行和Commodity(商品), Order(订单)之间的映射???下面是详细设计:
命名规范
前台页面:
首页:index.jsp
购物车:shopCar.jsp
删除购物项:del_item.jsp
订单提交成功:sub_success.jsp
销售管理:
弹出页面:index.jsp
入库成功:add_order_success.jsp
财务管理:
首页(销量统计):index.jsp
修改库存:update_storage.jsp
修改商品:update_commodity.jsp
增加商品:add_commodity.jsp
增加类别:add_category.jsp
删除商品:del_commodity_success.jsp
删除类别:del_commodity_success.jsp
查询库存:find_storage.jsp
更新库存:update_storage.jsp
类
User:
id
name:用户姓名
worked:工号
role:角色
Commodity:
id
name:商品名
description:描述
storage:库存
category:类别 many-to-one
unitPrice:单价
Category:
id
name:类别名
commodities:类别下商品 Set: one-to-many
OrderItem:
id:
commodity:商品 one-to-many
amount:数量
totalPrice:订单项总价
order:所属订单 many-to-one
Order:
id
time:订单产生时间
totalPrice:订单总价
orderItems:包含的订单项 Set: one-to-many
status:订单状态: -2,失效. -1,未提交. 0,已提交. 1,已入库.
环境配置:
准备使用的开发框架
Spring-framework-2.0.8 + struts-1.2.9 + hibernate 3.0 + xdoclet-plugins 1.0.3 + ant 1.6.2 + Tomcat 6 + Junit 3.8.2 + MySql 5 + pager-taglib + log4j 1.2.1
准备使用的IDE MyEclipse 6.0.1
准备使用的版本管理工具: Subversion
1.服务器正常启动,没有任何异常.
单元测试过程可以无异常跑完.
单元测试完成之后结果不能插入到数据库.
但是手动往数据库插入数据,可以发现每次插入都占用了id(自动递增的id).
前面遇到的一些框架版本等方面的问题已经解决,这次遇到这个数据库插入的问题实在怪异!!!
2.关于OrderItem类的设计
OrderItem我认为没有必要保存到数据库中,只是想在保存订单的时候保存为卖出商品id,数量,不知道这样是否显示.
另外,如果设计上让OrderItem存储到数据库,又该怎么样进行和Commodity(商品), Order(订单)之间的映射???下面是详细设计:
命名规范
前台页面:
首页:index.jsp
购物车:shopCar.jsp
删除购物项:del_item.jsp
订单提交成功:sub_success.jsp
销售管理:
弹出页面:index.jsp
入库成功:add_order_success.jsp
财务管理:
首页(销量统计):index.jsp
修改库存:update_storage.jsp
修改商品:update_commodity.jsp
增加商品:add_commodity.jsp
增加类别:add_category.jsp
删除商品:del_commodity_success.jsp
删除类别:del_commodity_success.jsp
查询库存:find_storage.jsp
更新库存:update_storage.jsp
类
User:
id
name:用户姓名
worked:工号
role:角色
Commodity:
id
name:商品名
description:描述
storage:库存
category:类别 many-to-one
unitPrice:单价
Category:
id
name:类别名
commodities:类别下商品 Set: one-to-many
OrderItem:
id:
commodity:商品 one-to-many
amount:数量
totalPrice:订单项总价
order:所属订单 many-to-one
Order:
id
time:订单产生时间
totalPrice:订单总价
orderItems:包含的订单项 Set: one-to-many
status:订单状态: -2,失效. -1,未提交. 0,已提交. 1,已入库.
环境配置:
准备使用的开发框架
Spring-framework-2.0.8 + struts-1.2.9 + hibernate 3.0 + xdoclet-plugins 1.0.3 + ant 1.6.2 + Tomcat 6 + Junit 3.8.2 + MySql 5 + pager-taglib + log4j 1.2.1
准备使用的IDE MyEclipse 6.0.1
准备使用的版本管理工具: Subversion
解决方案 »
- 在 myeclipse 中启动 tomcat 时出现错误
- java模拟用户登录问题!高手大哥请留步小弟被这个问题缠很久了。快发疯了帮帮我!请看内容
- WebService的发布问题
- select optiion 联动
- 求帮助
- 在Servlet里如何获取select标签里的值
- 將js文件放在WEB-INF文件夾下,JSP中的路径应该如何写?
- 如何调用远程的DB2数据库,请帮忙!?谢谢
- 如何在jsp页面中把一个相对地址付值给realplay控件,请看其中部分代码?
- 怎么在JSP中把session.getAttribute("参数")赋值给一个变量。
- 请问这段修改数据库代码对吗?
- 奇怪!三种环境下测试,只有tomcat6.0下有问题!关于出现问号
商品类别管理接口:package selfImpr.retail.manager;import java.util.List;import selfImpr.retail.model.Category;/**
* 商品类别管理接口
* @author Administrator
*
*/
public interface CategoryManager {
/**
* 增加一个商品类别
* @param category
*/
public void add(Category category);
/**
* 更新一个商品类别
* @param category
*/
public void update(Category category);
/**
* 删除一个商品类别
* @param category
*/
public void del(Category category); /**
* 查询所有类别(由于网吧商品数目单一,固定,预备设计一级类别)
* @param ids
* @return
*/
public List<Category> findAll();
}
package selfImpr.retail.manager;import java.util.List;import selfImpr.retail.model.Commodity;/**
* 商品管理类
* @author Administrator
*
*/
public interface CommodityManager {
/**
* 增加一个商品
* @param commodity
*/
public void add(Commodity commodity);
/**
* 删除一个商品
* @param commodity
*/
public void del(Commodity commodity);
/**
* 更新一个商品
* @param commodity
*/
public void update(Commodity commodity);
/**
* 根据ID查询一个商品
* @param id
* @return
*/
public Commodity findById(int id);
/**
* 根据类别ID,关键字查询一组商品
* @param categoryId
* @param queryStr
* @return
*/
public List<Commodity> findCommodity(int categoryId, String queryStr);
}
package selfImpr.retail.manager.impl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import selfImpr.retail.manager.CommodityManager;
import selfImpr.retail.model.Commodity;/**
* 商品管理实现类(基于spring+hibernate)
* @author Administrator
*
*/
public class CommodityManagerImpl extends HibernateDaoSupport implements
CommodityManager { /**
* 增加一个商品
*/
public void add(Commodity commodity) {
getHibernateTemplate().save(commodity);
} /**
* 删除一个商品
*/
public void del(Commodity commodity) {
getHibernateTemplate().delete(commodity);
} /**
* 根据id查询一个商品
*/
public Commodity findById(int id) {
Commodity commodity = null;
commodity = (Commodity)getHibernateTemplate().get(Commodity.class, id);
return commodity;
} public List<Commodity> findCommodity(int categoryId, String queryStr) {
return null;
} public void update(Commodity commodity) { }}
* @hibernate.class table="category"
* @author Administrator
* 商品类别实体类
*/
public class Category {
/**
* @hibernate.id generator-class="native"
* 类别id
*/
private int id;
/**
* @hibernate.property
* 类别名
*/
private String name;
/**
* @hibernate.set inverse="true" lazy="extra"
* @hibernate.key column="categoryId"
* @hibernate.one-to-many class="selfImpr.retail.model.Commodity"
* 类别下所有商品
*/
private Set<Commodity> commoditys; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Set<Commodity> getCommoditys() {
return commoditys;
} public void setCommoditys(Set<Commodity> commoditys) {
this.commoditys = commoditys;
}
}
* @hibernate.class table="commodity"
* @author Administrator
* 商品实体类
*/
public class Commodity {
/**
* @hibernate.id generator-class="native"
* 商品id
*/
private int id;
/**
* @hibernate.property
* 商品名
*/
private String name;
/**
* @hibernate.property
* 商品描述
*/
private String description;
/**
* @hibernate.many-to-one
* column="categoryId"
* 商品所属类别
*/
private Category category;
/**
* @hibernate.property
* 商品单价
*/
private double unitPrice;
/**
* @hibernate.property
* 商品库存量
*/
private int storage; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Category getCategory() {
return category;
} public void setCategory(Category category) {
this.category = category;
} public double getUnitPrice() {
return unitPrice;
} public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} public int getStorage() {
return storage;
} public void setStorage(int storage) {
this.storage = storage;
}
}
* @hibernate.class table="user"
* @author Administrator
* 用户实体类
*/
public class User {
/**
* @hibernate.id generator-class="native"
* 用户id
*/
private int id;
/**
* @hibernate.property
* 用户姓名
*/
private String name;
/**
* @hibernate.property
* 用户工号
*/
private String workId;
/**
* @hibernate.property
* 用户角色,由于系统简单,权限系统准备用字符串标识.
*/
private String role; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getRole() {
return role;
} public void setRole(String role) {
this.role = role;
} public String getWorkId() {
return workId;
} public void setWorkId(String workId) {
this.workId = workId;
}
}
<?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: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/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">
<!-- 注入commodityManager,同时为实例注入sessionFactory -->
<bean id="commodityManager" class="selfImpr.retail.manager.impl.CommodityManagerImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
applicationContext-common.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: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/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">
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean >
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置使用事务的类和方法 -->
<aop:config>
<aop:pointcut id="allManager" expression="execution(* selfImpr.spring.manager.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManager" />
</aop:config>
</beans>
<project name="零售系统构建脚本" default="生成Hibernate配置文件" basedir=".">
<property name="src.dir" value="${basedir}/src" />
<property name="build.dir" value="${basedir}/bin" />
<property name="webapp.dir" value="${basedir}/WebRoot" />
<property name="xdoclet.home" value="E:\Plugin-Frame\xdoclet-plugins-1.0.3" />
<!-- Build classpath -->
<path id="xdoclet.task.classpath">
<fileset dir="${xdoclet.home}/lib">
<include name="**/*.jar" />
</fileset>
<fileset dir="${xdoclet.home}/plugins">
<include name="**/*.jar" />
</fileset>
</path>
<taskdef name="xdoclet"
classname="org.xdoclet.ant.XDocletTask"
classpathref="xdoclet.task.classpath"
/>
<target name="生成Hibernate配置文件">
<xdoclet>
<fileset dir="${src.dir}/selfImpr/retail/model">
<include name="**/*.java" />
</fileset>
<component classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin"
destdir="${src.dir}"
version="3.0"
hbm2ddlauto="create"
jdbcurl="jdbc:mysql://localhost/retail"
jdbcdriver="com.mysql.jdbc.Driver"
jdbcusername="root"
jdbcpassword="root"
dialect="org.hibernate.dialect.MySQLDialect"
showsql="true"
/>
</xdoclet>
</target>
<target name="生成Hibernate映射文件">
<xdoclet>
<fileset dir="${src.dir}/selfImpr/retail/model">
<include name="**/*.java" />
</fileset>
<component version="3.0"
destdir="${src.dir}"
classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
/>
</xdoclet>
</target></project>
import org.springframework.context.support.ClassPathXmlApplicationContext;import selfImpr.retail.model.Category;
import selfImpr.retail.model.Commodity;/**
* 商品管理类功能测试
* @author Administrator
*
*/
public class CommodityManagerTest extends TestCase {
/**
* 获取BeanFactory
*/
private static BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
/**
* 测试添加商品功能
*/
public void testAdd() {
CommodityManager cm = (CommodityManager)factory.getBean("commodityManager");
Commodity commodity = new Commodity();
commodity.setName("方便面");
Category category = new Category();
category.setId(1);
commodity.setCategory(category);
cm.add(commodity);
}
/**
* 测试查询商品功能
*/
public void testFindById() {
CommodityManager cm = (CommodityManager)factory.getBean("commodityManager");
Object o = cm.findById(1);
Commodity c = (Commodity)o;
System.out.println(c.getName());
}
}
用不着每个 DAO 中写一遍的。详见:http://topic.csdn.net/u/20080407/09/dabcc399-4460-47ef-966f-26bcb800bd39.html 第 5 问,解答在 134、135 楼。
本人才开始学struts