struts2_hibernate3_spring2配置文件简化

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

applicationContext文件代码:

[color=darkred]<?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>[/color]


action-servlet.xml 中的代码:
[color=darkred]<?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>[/color]


struts.xml中的代码:

[color=darkred]<?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>[/color]

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


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

[color=darkred]<!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>[/color]


action配置可在web.xml中进行:

[color=darkred]<?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>[/color]


StoreHouseAction类文件:

[color=darkred]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[/color]


StoreHouseBean文件:

[color=darkred]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;
}[/color]


StoreHouseDaoImpl文件:

[color=darkred]package com.cw.operation.dao;

import org.springframework.stereotype.Repository;

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


T_StoreHouse实体类映射:

[color=darkred]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;

}[/color]


StoreHouseServiceImpl方法配置:

[color=darkred]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;
}[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值