ssh框架有关数据源的配置

本文介绍了一个使用Struts2与Spring框架整合的示例应用,详细配置了web.xml、hibernate.cfg.xml及applicationContext.xml文件。示例包括了过滤器设置、数据源配置、SessionFactory初始化、TransactionManager定义以及DAO和服务层实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. web.xml中

 

  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
 <display-name>books</display-name>

<!-- log4j配置 -->
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>/WEB-INF/classes/log4j.properties</param-value>
 </context-param>
 <!-- ContextConfigLocation -->
 <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/spring-context/applicationContext.xml</param-value>
   </context-param>

 
 <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>com.sterning.commons.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
  <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
    </filter>
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        <init-param>
         <param-name>config</param-name>
         <param-value>struts-default.xml,struts-plugin.xml,struts.xml,struts_books.xml</param-value>
        </init-param>
    </filter>
 

 <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
 </filter-mapping>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
 
 <!-- Listener contextConfigLocation -->
   <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
 <!-- Listener log4jConfigLocation -->
   <listener>
     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
   </listener>

 
 <!-- The Welcome File List -->
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/mingoe</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

 

 

2.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">
<hibernate-configuration>
<session-factory>
 <property name="show_sql">true</property><!--控制台中显示执行的sql语句 -->
 <property name="hibernate.cglib.use_reflection_optimizer">
  true
 </property>
 <property name="connection.datasource">
  java:comp/env/jdbc/mingoe
 </property>
 <property name="dialect">
  org.hibernate.dialect.MySQL5Dialect
 </property>
 <mapping resource="com/sterning/books/model/books.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

 

 

3.applicationContext.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 
<beans>
 <!-- 数据源配置1 -->
 <bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://192.168.18.100:3306/mingoe2" />
  <property name="username" value="root" />
  <property name="password" value="bjhwlc"/>
 </bean>
<!-- 数据源配置2 -->
 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>java:comp/env/jdbc/mingoe</value>
    </property>
 </bean>

 


 <!-- SessionFactory -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  <property name="dataSource">
   <ref bean="dataSource"/>
  </property>
  <property name="configLocation">
   <value>classpath:com/sterning/bean/hibernate/hibernate.cfg.xml</value>
  </property>  
 </bean>
 
 <!-- TransactionManager  不过这里暂时没注入-->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
 </bean>
 
 <!-- DAO -->
 <bean id="booksDao" class="com.sterning.books.dao.hibernate.BooksMapDao">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>
 
 <!-- Services -->
 <bean id="booksService" class="com.sterning.books.services.BooksService">
  <property name="booksDao">
   <ref bean="booksDao"/>
  </property>
 </bean>
 
 <bean id="pagerService" class="com.sterning.commons.PagerService"/>
 
 <!-- view -->
 <bean id="bookAction" class="com.sterning.books.web.actions.BooksAction" singleton="false">
  <property name="booksService">
   <ref bean="booksService"/>
  </property>
  <property name="pagerService">
   <ref bean="pagerService"/>
  </property>
 </bean> 
 
</beans>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值