struts2_hibernate3_spring2配置文件简单化

struts2_hibernate3_spring2配置文件简化
1. 以前使用的 applicationContext.xml与action-servlet.xml,struts.xml与实体映射文件.hbm.xml都可删除

applicationContext文件代码:

<?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/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
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
   
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        <property name="configLocation"
                  value="classpath:hibernate.cfg.xml">
        </property>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
   
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config/>
    <context:component-scan base-package="com.cw"/>
</beans>



action-servlet.xml 中的代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <!--库房信息维护-->
    <!--<bean id="storeHouse" class="com.cw.operation.action.StoreHouseAction" scope="prototype" autowire="byName">-->
        <!--<property name="storeHouseService">-->
            <!--<ref bean="storeHouseService"/>-->
        <!--</property>-->
         <!--<property name="archiveTankGridService">-->
            <!--<ref bean="archiveTankGridService"/>-->
        <!--</property>-->
        <!--<property name="archiveTankService">-->
            <!--<ref bean="archiveTankService"/>-->
        <!--</property>-->
        <!--<property name="archiveTankLineService">-->
            <!--<ref bean="archiveTankLineService"/>-->
        <!--</property>-->
    <!--</bean>-->

    <!--<bean id="storeHouseBean" class="com.cw.operation.bean.StoreHouseBean">-->
        <!--<property name="storeHouseService">-->
            <!--<ref bean="storeHouseService"/>-->
        <!--</property>-->
    <!--</bean>-->
</beans>



struts.xml中的代码:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
        "http://struts.apache.org/dtds/struts-2.1.dtd">

<!--定义返回结果?-->       
<struts>
    <!--<package name="operation" extends="struts-default">-->
        <!--库房信息维护-->
        <!--<action name="storeHouse" class="storeHouse">-->
            <!--<result name="add">/operation/storeHouseAdd.jsp</result>-->
            <!--<result name="edit">/operation/storeHouseEdit.jsp</result>-->
            <!--<result name="list">/operation/storeHouseList.jsp</result>-->
            <!--<result name="storeList">/operation/archiveTankInitialize.jsp</result>-->
            <!--<result name="initialize">/operation/initialize.jsp</result>-->
            <!--<result name="InitializeList">/operation/InitializeList.jsp</result>-->
            <!--<result name="editTank">/operation/archiveTankEdit.jsp</result>-->
            <!--<result name="editLine">/operation/archiveTankLineEdit.jsp</result>-->
            <!--<result name="showGrid">/operation/archiveTankGridList.jsp</result>-->
            <!--<result name="editGrid">/operation/archiveTankGridEdit.jsp</result>-->
        <!--</action>-->
       <!---->
    <!--</package>-->
</struts>


以上三个文件都可删除不用


实体映射可在hibernate.cfg.xml中完成:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.sybase.jdbc2.jdbc.SybXADataSource</property>
        <property name="connection.url">jdbc:sybase:Tds:172.22.80.234:7220/aicdal?charset=cp936</property>
        <property name="connection.username">aicbiz</property>
        <property name="connection.password">supper</property>
        <property name="dialect">org.hibernate.dialect.SybaseDialect</property>
        <property name="show_sql">true</property>
<!--实体类文件-->
<mapping class="com.cw.operation.entity.T_StoreHouse"/>  
<mapping class="com.cw.operation.entity.V_StoreHouse"/>
</session-factory>
</hibernate-configuration>



action配置可在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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <display-name>struts2_hibernate3_spring2_project</display-name>
<context-param>
        <param-name>servletmapping</param-name>
        <param-value>*.do</param-value>
    </context-param>
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>
                <!--action文件包-->
                com.cw.operation.action
            </param-value>
        </init-param>
    </filter>
</web-app>



StoreHouseAction类文件:

package com.cw.operation.action;

import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.config.ParentPackage;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;

@Controller
@Scope("prototype")
@ParentPackage(value="struts-default")
@Results({
    @Result(name="add",value="/operation/storeHouseAdd.jsp"),
    @Result(name="edit",value="/operation/storeHouseEdit.jsp"),
    @Result(name="list",value="/operation/storeHouseList.jsp")
})
public class StoreHouseAction implements Action {
    @Resource(name="storeHouseService")
    private IStoreHouseService storeHouseService;
}
配置action映射,其他文件一样。。如果需要调用多个service,则继续用
@Resource(name="service名")
private 需定义service



StoreHouseBean文件:

package com.cw.operation.bean;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;


@Service("storeHouseBean")
public class StoreHouseBean {
    @Resource(name="storeHouseService")
    private IStoreHouseService storeHouseService;
}



StoreHouseDaoImpl文件:

package com.cw.operation.dao;

import org.springframework.stereotype.Repository;

@Repository("storeHouseDao")
public class StoreHouseDaoImpl extends BaseDaoImpl<T_StoreHouse,String> implements IStoreHouseDao{
    public StoreHouseDaoImpl(){}
}



T_StoreHouse实体类映射:

package com.cw.operation.entity;

import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;

@Entity
public class T_StoreHouse implements Serializable{
    @Id
    private String storeIndex; //一般主键列

//自动增长主键列设置
    @Id
   @GeneratedValue(generator = "paymentableGenerator")
   @GenericGenerator(name = "paymentableGenerator", strategy = "uuid.hex")

    private String bizSequence;

}



StoreHouseServiceImpl方法配置:

package com.cw.operation.service;



import org.apache.commons.beanutils.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;


@Service("storeHouseService")
public class StoreHouseServiceImpl implements IStoreHouseService{
    @Resource(name="storeHouseDao") //dao映射
    private IStoreHouseDao storeHouseDao;
}