首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2_hibernate3_spring2配置文件简单化

2012-10-25 
struts2_hibernate3_spring2配置文件简化1. 以前使用的 applicationContext.xml与action-servlet.xml,stru

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"
          value="org.hibernate.cfg.AnnotationConfiguration" />
        <property name="configLocation"
                  value="classpath:hibernate.cfg.xml">
        </property>
    </bean>

    <bean id="transactionManager"
          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" 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" 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" 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;
}

热点排行