第一:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- spring的web启动方式 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 延长session生命周期 -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 过滤器 -->
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.hsg.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第二:struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans /> <global-exceptions /> <global-forwards /> <action-mappings> <action path="/sale" parameter="p" type="org.springframework.web.struts.DelegatingActionProxy" ></action> <action path="/detail" parameter="p" type="org.springframework.web.struts.DelegatingActionProxy" ></action> </action-mappings> <message-resources parameter="com.yourcompany.struts.ApplicationResources" /> </struts-config>
第三:applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
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/util http://www.springframework.org/schema/util/spring-util-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
">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="TSaleformDetailDAO"
class="com.hsg.dao.TSaleformDetailDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="TSaleformDAO" class="com.hsg.dao.TSaleformDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 注入service -->
<bean id="SaleformService" class="com.hsg.service.SaleformService">
<property name="detailDao"><ref bean="TSaleformDetailDAO"></ref></property>
<property name="saleformDao"><ref bean="TSaleformDAO"></ref></property>
</bean>
<!-- 注入service -->
<bean id="SaleformDetailService" class="com.hsg.service.SaleformDetailService">
<property name="detailDao"><ref bean="TSaleformDetailDAO"></ref></property>
<property name="saleformDao"><ref bean="TSaleformDAO"></ref></property>
</bean>
<!-- 注入action -->
<bean name="/sale" class="com.hsg.action.SaleformAction">
<property name="detailService"><ref bean="SaleformDetailService"></ref></property>
<property name="saleService"><ref bean="SaleformService"></ref></property>
</bean>
<!-- 注入action -->
<bean name="/detail" class="com.hsg.action.SaleformDetailAction">
<property name="detailService"><ref bean="SaleformDetailService"></ref></property>
<property name="saleService"><ref bean="SaleformService"></ref></property>
</bean>
<!-- 声明事物管理器 -->
<bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 通知器 -->
<tx:advice id="adv" transaction-manager="htm">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- aop切入事务拦截器 -->
<aop:config>
<aop:advisor advice-ref="adv"
pointcut="execution(* com.hsg.iservice.*.*(..))"/>
</aop:config>
</beans>
第四:hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- 数据库对应的方言 -->
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<!-- 定义数据库服务器地址 -->
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<!-- 数据库用户名 -->
<property name="connection.username">hhgz</property>
<!-- 对应密码-->
<property name="connection.password">hhgz</property>
<!-- 数据库连接驱动-->
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">
softfactory
</property>
<!-- 操作数据库时是否打印sql语句 -->
<property name="hibernate.show_sql"></property>
<!-- 配置ORM映射文件 -->
<mapping resource="com/hsg/pojos/TSaleformDetail.hbm.xml" />
<mapping resource="com/hsg/pojos/TSaleform.hbm.xml" />
</session-factory>
</hibernate-configuration>
第五:*.hbm.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="com.hsg.pojos.TSaleform" table="T_SALEFORM" schema="HHGZ" > <id name="FSaleformNo" type="java.lang.String" > <column name="F_SALEFORM_NO" length="20" /> <generator class="assigned" /> </id> <property name="FSaleformDate" type="java.util.Date" > <column name="F_SALEFORM_DATE" length="7" not-null="true" /> </property> <property name="FCustomer" type="java.lang.String"> <column name="F_CUSTOMER" length="20" not-null="true" /> </property> <property name="FSales" type="java.lang.String"> <column name="F_SALES" length="20" not-null="true" /> </property> <property name="FRemark" type="java.lang.String"> <column name="F_REMARK" length="2000" /> </property> <set name="TSaleformDetails" inverse="true"> <!-- 指定外键 --> <key> <column name="F_SALEFORM_NO" length="20" not-null="true" unique="true" /> </key> <!-- 指定引用类 --> <one-to-many class="com.hsg.pojos.TSaleformDetail" /> </set> </class> </hibernate-mapping>
附一:SSH架构
附二:baseDao和service及action三个类的实现方式
1,baseDao
package com.hsg.dao;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BaseDao extends HibernateDaoSupport{
/**
* 根据hql 查询
* @param hql
* @param objects
* @return
*/
public List queryByHql(final String hql,final Object...objects){
Query query=this.getSession().createQuery(hql);
try {
if(objects!=null){
for (int i = 0; i < objects.length; i++) {
query.setParameter(i, objects[i]);
}
}
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return query.list();
}
/**
* 根据hql 查询
* @param hql
* @param objects
* @return
*/
public Object queryByUnique(final String hql,final Object...objects){
Query query=this.getSession().createQuery(hql);
try {
if(objects!=null){
for (int i = 0; i < objects.length; i++) {
query.setParameter(i, objects[i]);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return query.uniqueResult();
}
}
2.service实现类
/**
* 订单服务类
* @author hushiguo
*
*/
public class SaleformService implements ISaleformService {
private TSaleformDAO saleformDao;
private TSaleformDetailDAO detailDao;
public void setSaleformDao(TSaleformDAO saleformDao) {
this.saleformDao = saleformDao;
}
public void setDetailDao(TSaleformDetailDAO detailDao) {
this.detailDao = detailDao;
}
/**
* 添加订单
* @param saleForm 订单信息
* @param detail 详单信息
* @return
*/
public void addSale(TSaleform saleForm){
try {
if(saleForm!=null){
saleformDao.save(saleForm);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获得最大编号
* @param saleDate 日期
* @return 单号
*/
public String getBigSaleId(String saleDate){
String saleId="";
SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
Date nowDate=new Date();
String hql="";
if(saleDate==null||saleDate.equals("")){
hql="select max(s.FSaleformNo) from TSaleform s where to_char(s.FSaleformDate,'yyyyMMdd')=?";
saleId=(String) saleformDao.queryByUnique(hql, df.format(nowDate));
if(saleId==null){
saleId="SL"+df.format(nowDate)+"001";
}else{
String id=saleId.substring(2);
saleId="SL"+(Long.parseLong(id)+1);
}
}else{
hql="select max(s.FSaleformNo) from TSaleform s where to_char(s.FSaleformDate,'yyyyMMdd')=?";
saleId=(String) saleformDao.queryByUnique(hql,saleDate);
if(saleId==null){
saleId="SL"+saleDate+"001";
}else{
//SL20110909001
String id=saleId.substring(2);
saleId="SL"+(Long.parseLong(id)+1);
}
}
return saleId;
}
}
3.action实现类
/**
* 订单action
* @author hushiguo
*
*/
public class SaleformAction extends DispatchAction {
private ISaleformService saleService;
private ISaleformDetailService detailService;
public void setSaleService(ISaleformService saleService) {
this.saleService = saleService;
}
public void setDetailService(ISaleformDetailService detailService) {
this.detailService = detailService;
}
/*
*
* 添加订单
*/
public ActionForward addSale(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
String saleId=request.getParameter("saleId"); //订单编号
String rq=request.getParameter("rq"); //日期
String kh=request.getParameter("kh"); //客户
String ywy=request.getParameter("ywy"); //业务员
String bz=request.getParameter("bz"); //备注
String [] jsonArray=request.getParameterValues("jsonArray"); //详单信息
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date d=null;
Boolean bol=true;
if(rq!=null&&!rq.equals("")){
d=df.parse(rq);
}
TSaleformDetail detail=null;
Integer lineId=null;
try {
TSaleform saleForm=new TSaleform(saleId,
d,
kh,
ywy,
bz);
//saleService.addSale(saleForm); //调用添加订单业务方法
//循环json对象数组 将它转换为对应类的对象
for (int i = 0; i < jsonArray.length; i++) {
//将对象转换为JSONObject
JSONObject json=JSONObject.fromObject(jsonArray[i]);
//将JSONObject转换为对应类的对象
detail= (TSaleformDetail) json.toBean(json,TSaleformDetail.class);
System.out.println(detail.getFGoods());
lineId=detailService.addSale(saleForm, detail); //调用添加详单方法
}
} catch (Exception e) {
bol=false;
// TODO Auto-generated catch block
e.printStackTrace();
}
out.print("{result:"+bol+",saleId:"+lineId+"}");
/**
* JSONObject
* 将对象转换成 JSONObject
* object 可以是json字符串, 也可以是对象
* JSONObject.fromObject(object)
*
* 将一个JsonObject对象转换成对应的类的对象
* obj: JsonObject 对象
* JSONObject.toBean(obj,Emp.class)
*
* 将一个JsonArray对象转换成List<Emp>
* JSONArray.toList(jsonArray,Emp.class)
*
* 通过键获得值
* obj.get(key)
*
*
*/
return null;
}
/*
* 获得订单编号
*/
public ActionForward getBigSaleId(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
try {
String saleDate=request.getParameter("saleDate");
SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
String d2="";
if(saleDate!=null&&!saleDate.equals("")){
//d2=df.format(df.parse(saleDate));
if(saleDate.contains("-")){
String [] dd=saleDate.split("-");
for (int i = 0; i <dd.length; i++) {
d2+=dd[i];
}
}else if(saleDate.contains("/")){
String [] dd=saleDate.split("/");
for (int i = 0; i <dd.length; i++) {
d2+=dd[i];
}
}
System.out.println(d2);
String saleId=saleService.getBigSaleId(d2);
out.print(saleId);
}else{
String saleId=saleService.getBigSaleId(saleDate);
out.print(saleId);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
附3:建表SQL
----------------=========创建订单表
create table t_saleform(
f_saleform_no VARCHAR2(20) primary key not null,--N Y 单号assigned
f_saleform_date DATE not null,--N 单据日期 要建索引idx_form_date
f_customer VARCHAR2(20) not null,--N 客户
f_sales VARCHAR2(20)not null,-- N 业务员
f_remark VARCHAR2(2000) -- 备注
);
--建日期索引
create index idx_date on t_saleform(f_saleform_date);
----------------=========创建详单表
create table t_saleform_detail(
f_saleform_no VARCHAR(20) not null,--N Y Y 出库单号,引用t_sd_saleform. f_saleform_no
f_line_id SMALLINT not null,--N Y 行号
f_goods VARCHAR(50) not null,--N 商品名称
f_qty NUMERIC(19,4) not null,--N 数量
f_price NUMERIC(19,4) not null,--N 单价
f_money_amt NUMERIC(19,4)not null,-- N 金额
constraint pk_f_saleform_no primary key (f_saleform_no),
constraint fk_f_saleform_no foreign key (f_saleform_no) references t_saleform(f_saleform_no)
);