`
临听岗岗
  • 浏览: 9520 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

spring + hibernate 最佳配置(转载)

 
阅读更多

applicationContext-config.xml  //用于加载  .properties 属性文件 该文件放于classpath路径下 eg: jdbc.properties

 

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>	
	
</beans>

 

applicationContext-dataAccess.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: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/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd
	http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

   <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${connection.driver_class}"/>
    <property name="jdbcUrl" value="${jdbc.connection.url}"/>
    <property name="idleConnectionTestPeriod" value="${jdbc.pool.c3p0.idle_connection_test_period}" />
    <property name="properties">
			<props>
				<prop key="user">${jdbc.connection.username}</prop>
				<prop key="password">${jdbc.connection.password}</prop>
				<prop key="c3p0.acquire_increment">${jdbc.pool.c3p0.acquire_increment}</prop>
				<prop key="c3p0.max_size">${jdbc.pool.c3p0.max_size}</prop>
				<prop key="c3p0.min_size">${jdbc.pool.c3p0.min_size}</prop>
			</props>
    </property>
  </bean>

  <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="mappingDirectoryLocations">
      <list>
        <value>classpath*:persist/user</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
		<!--常用数据库方言 MySQL5Dialect,SQLServerDialect,OracleDialect,SybaseDialect,DB2Dialect,HSQLDialect -->
	    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
	    
		<prop key="hibernate.show_sql">true</prop>
		<!-- <prop key="hibernate.query.substitutions">true 1, false 0</prop>
		<prop key="hibernate.default_batch_fetch_size">4</prop> -->
		<prop key="hibernate.hbm2ddl.auto">update</prop>
		<prop key="hibernate.cache.use_second_level_cache">true</prop>
		<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
		
		
	  </props>
    </property>
  </bean>
  
  <bean id="transactionManager" 
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory"/>
  </bean>
  
  <aop:config>
    <aop:pointcut id="productServiceMethods" 
            expression="execution(* com.dx.dao.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
  </aop:config>

  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="save" propagation="REQUIRED"/>
      <tx:method name="update" propagation="REQUIRED"/>
      <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
  </tx:advice>

  
  

<bean id="baseDao" class="com.dx.dao.impl.BaseDaoImpl">
  <property name="sessionFactory" ref="mySessionFactory"></property>
</bean>

	
</beans>

 

applicatiionContext-user 用于service层业务bean的配置 目前没有东西。

 

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	
</beans>

 

hibernate 实体类的映射文件  .hbm.xml  eg:User.hbm.xml 文件 放于conf文件夹下的persist/user下

 

文件结构图如下

 

web.xml配置

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>web_Services</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
    
  </welcome-file-list>
 
  <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>
    classpath:context/applicationContext-*.xml
    /WEB-INF/applicationContext-security.xml
 </param-value>
 </context-param>

 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener
 </listener-class>
 </listener>
  
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</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-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>*</url-pattern>
  </filter-mapping>
  
  <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
      <filter-name>springSecurityFilterChain</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
     <!-- 
  <servlet>
   <servlet-name>dispatcherServlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-servlet.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>dispatcherServlet</servlet-name>
   <url-pattern>*.html</url-pattern>
  </servlet-mapping>
-->
</web-app>

 

ok就酱紫了

 

 

  • 大小: 8.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics