hibernate指定jdbc配置文件

hibernatge如何指定jdbc配置文件呢?

jdbc配置文件型如:

Java代码  hibernate指定jdbc配置文件
  1. jdbc.driverClassName=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc:mysql://182.92.94.71:3306/test  
  3. jdbc.username=root  
  4. jdbc.password=123456  

 

hibernate的配置文件中有两种方式指定jdbc配置文件

方式一:

Xml代码  hibernate指定jdbc配置文件
  1. <bean  
  2.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.         <property name="locations">  
  4.             <value>classpath:jdbc.properties</value>  
  5.         </property>  
  6.     </bean>  

 

方式二:

Java代码  hibernate指定jdbc配置文件
  1. <context:property-placeholder location="classpath:jdbc.properties" />  

 

beans.xml的完整内容如下:

Xml代码  hibernate指定jdbc配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.             http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  9.            http://www.springframework.org/schema/aop  
  10.            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
  11.            http://www.springframework.org/schema/tx   
  12.            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"  
  13.     default-lazy-init="false">  
  14.   
  15.     <context:annotation-config />   
  16.     <context:component-scan base-package="com,oa"   
  17.         ></context:component-scan>  
  18.     <bean id="dataSource" destroy-method="close"  
  19.         class="org.apache.commons.dbcp.BasicDataSource">  
  20.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  21.         <property name="url" value="${jdbc.url}" />  
  22.         <property name="username" value="${jdbc.username}" />  
  23.         <property name="password" value="${jdbc.password}" />  
  24.         <!--initialSize: 初始化连接-->    
  25.         <property name="initialSize" value="1"/>  
  26.         <!--maxActive: 最大连接数量-->    
  27.         <property name="maxActive" value="2"/>      
  28.     </bean>  
  29.     <!-- <bean  
  30.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  31.         <property name="locations">  
  32.             <value>classpath:jdbc.properties</value>  
  33.         </property>  
  34.     </bean> -->  
  35.     <context:property-placeholder location="classpath:jdbc.properties" />  
  36.     <bean id="sessionFactory"  
  37.         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  38.         <property name="dataSource" ref="dataSource" />  
  39.         <!--<property name="packagesToScan"> <list> <value>com.pass.bean</value>   
  40.             </list> </property> -->  
  41.         <property name="hibernateProperties">  
  42.             <props>  
  43.                 <prop key="hibernate.dialect">  
  44.                 org.hibernate.dialect.MySQL5Dialect  
  45.                 </prop>  
  46.                 <!-- org.hibernate.dialect.PostgreSQLDialect 
  47.                  
  48.                  -->  
  49.                 <prop key="hibernate.show_sql">true</prop>  
  50.                 <prop key="hibernate.format_sql">true</prop>  
  51.                 <prop key="hibernate.hbm2ddl.auto">none</prop>  
  52.                 <prop key="hibernate.use_sql_comments">true</prop>  
  53.                 <prop key="current_session_context_class">thread</prop>  
  54.                 <prop key="javax.persistence.validation.mode">none</prop>  
  55.             </props>  
  56.         </property>  
  57.           
  58.         <property name="packagesToScan">  
  59.             <list>  
  60.                 <value>com.entity</value>  
  61.                 <value>oa.entity</value>  
  62.             </list>  
  63.         </property>  
  64.     </bean>  
  65.   
  66.   
  67.     <bean id="txManager"  
  68.         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  69.         <property name="sessionFactory" ref="sessionFactory"></property>  
  70.     </bean>  
  71.     <!-- 事务的注解,如 @Transactional(readOnly=true)  
  72.      <tx:annotation-driven transaction-manager="txManager" />  -->  
  73.   
  74.   
  75.     <aop:config>  
  76.         <aop:pointcut id="bussinessService"  
  77.             expression="execution(public   
  78.         * oa.dao..*.*(..)) ||execution(public   
  79.         * com.dao..*.*(..)) || execution(public   
  80.         * com.common.dao.generic..*.*(..))" />  
  81.         <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />  
  82.   
  83.   
  84.     </aop:config>  
  85.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  86.         <tx:attributes>  
  87.             <tx:method name="get*" read-only="true" />  
  88.             <tx:method name="count*" read-only="true" />  
  89.             <tx:method name="find*" read-only="true" />  
  90.             <tx:method name="test*" read-only="true" />  
  91.             <tx:method name="is*" read-only="true" />  
  92.             <tx:method name="show*" read-only="true" />  
  93.             <tx:method name="delete*" propagation="REQUIRED" />  
  94.             <tx:method name="update*" propagation="REQUIRED" />  
  95.             <tx:method name="save*" propagation="REQUIRED" />  
  96.             <tx:method name="add*" propagation="REQUIRED" />  
  97.             <tx:method name="edit*" propagation="REQUIRED" />  
  98.             <tx:method name="set*" propagation="REQUIRED" />  
  99.             <tx:method name="change*" propagation="REQUIRED" />  
  100.             <tx:method name="to*" propagation="REQUIRED" />  
  101.             <tx:method name="modify*" propagation="REQUIRED" />  
  102.             <tx:method name="verify*" read-only="true" />  
  103.             <tx:method name="list*" read-only="true" />  
  104.         </tx:attributes>  
  105.     </tx:advice>  
  106.       
  107.       
  108.       
  109. <!-- 支持上传文件 -->  
  110.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>  
  111.       
  112.       
  113.   
  114. </beans>  

 

  

上一篇:5G的要求和主要性能指标 | 《5G移动无线通信技术》之六


下一篇:windows下64位MySQL8的安装