zoukankan      html  css  js  c++  java
  • spring+hibernate+jpa+Druid的配置文件,spring整合Druid

    spring+hibernate+jpa+Druid的配置文件

    spring+hibernate+jpa+Druid的完整配置

    spring+hibernate+jpa+Druid的数据源配置

    spring整合Druid,SpringMvc整合Druid,hibernate整合druid

     

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    蕃薯耀 2016年3月30日 15:46:15 星期三

    http://fanshuyao.iteye.com/

     

    全部整合到spring.xml文件,不需要jpa的persistence.xml文件

     

    spring.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:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:task="http://www.springframework.org/schema/task"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    
    	<context:property-placeholder location="classpath:jdbc.properties"/>
    	
    	<context:component-scan base-package="com.lqy.spring.iwx.**">
    		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    	</context:component-scan>
    	
    	<!-- mysql数据源配置 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
            <!-- 数据库用户名称 -->
            <property name="username" value="${jdbc.username}"/>
            <!-- 数据库密码 -->
            <property name="password" value="${jdbc.password}"/>
            <!-- 驱动名称 -->
            <property name="driverClassName" value="${jdbc.driverClassName}" />
            <!-- JDBC连接串 -->
            <property name="url" value="${jdbc.url}" />
            <!-- 连接池最大使用连接数量 -->
            <property name="maxActive" value="${jdbc.maxActive}" />
            <!-- 初始化大小 -->
            <property name="initialSize" value="${jdbc.initialSize}" />
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="${jdbc.maxWait}" />
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="${jdbc.minIdle}" />
            <!-- 逐出连接的检测时间间隔 -->
            <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
            <!-- 最小逐出时间 -->
            <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
            <!-- 测试有效用的SQL Query -->
            <property name="validationQuery" value="SELECT 'x'" />
            <!-- 连接空闲时测试是否有效 -->
            <property name="testWhileIdle" value="true" />
            <!-- 获取连接时测试是否有效 -->
            <property name="testOnBorrow" value="false" />
            <!-- 归还连接时是否测试有效 -->
            <property name="testOnReturn" value="false" />
        </bean>
    	
    	<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    		<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
    	</bean>
                
    	<!-- 配置Spring管理Jpa的工厂Bean,需要加入spring-orm-4.1.7.RELEASE.jar(LocalEntityManagerFactoryBean类在里面) -->
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource"></property>
    		<!--待扫描的实体类包,不再需要persistence.xml了-->
    		<property name="packagesToScan" value="com.lqy.spring.iwx.bean.**"></property>
            <property name="jpaVendorAdapter" ref="jpaVendorAdapter"></property>
            <property name="jpaProperties">
    			<props>
    				<!--设置外连接抓取树的最大深度 -->
    	            <prop key="hibernate.max_fetch_depth">3</prop>
                  	<prop key="hibernate.jdbc.fetch_size">18</prop>
                  	<prop key="hibernate.jdbc.batch_size">10</prop>
                  	
                  	<!-- 自动建表类型 validate|create|create-drop|update -->
                  	<prop key="hibernate.hbm2ddl.auto">update</prop>
                  	<!-- 是否显示SQL -->
                  	<prop key="hibernate.show_sql">false</prop>
                  	<!-- 显示SQL是否格式化 -->
                  	<prop key="hibernate.format_sql">false</prop>
                  	<!-- 关闭二级缓存 -->
                  	<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                  	<!-- 关闭实体字段映射校验 -->
                  	<prop key="javax.persistence.validation.mode">none</prop>
    			</props>
    		</property>
    	</bean>
    	
    	<!-- 配置事务管理 -->
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory"></property>
    	</bean>
    	
    	
    	<!-- 启用事务注解 -->
    	<!-- 
    		Spring事务默认只能对运行时异常(RuntimeException)进行回滚,
    		不会对Exception进行回滚。
    		如果需要指定其他异常,则需要配置:rollbackFor=Exception.class
    	 -->
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    	
    	<task:annotation-driven scheduler="taskScheduler" mode="proxy"/>    
        <task:scheduler id="taskScheduler" pool-size="10"/>
    
    </beans>
    

     

    springMvc.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:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    
    	<context:component-scan base-package="com.lqy.spring.iwx.controller"></context:component-scan>
    	
    	<mvc:annotation-driven>
    		<mvc:message-converters>
    			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
    				<property name="supportedMediaTypes">
    					<list>
    						<value>text/html;charset=UTF-8</value>
    						<value>application/json;charset=UTF-8</value>
    					</list>
    				</property>
    			</bean>
    		</mvc:message-converters>
    	</mvc:annotation-driven>
    	
    	
    	<mvc:default-servlet-handler/>
    	
    	<!-- spring3使用-->
    	<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/WEB-INF/jsp/"></property>
    		<property name="suffix" value=".jsp"></property>
    	</bean> -->
    	
    	<!-- spring4使用 -->
    	<mvc:view-resolvers>
    		<mvc:jsp prefix="/WEB-INF/jsp/" suffix=".jsp"/>
    	</mvc:view-resolvers>
    	
    </beans>
    

     

    jdbc.properties配置文件:

    #数据源连接配置
    jdbc.username=root
    jdbc.password=xxx
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/databaseName?useUnicode=true&amp;characterEncoding=UTF-8
    
    #<!-- 连接池最大使用连接数量 -->
    jdbc.maxActive=20
    #<!-- 初始化大小 -->
    jdbc.initialSize=2
    #<!-- 获取连接最大等待时间 -->
    jdbc.maxWait=60000
    #<!-- 连接池最小空闲 -->
    jdbc.minIdle=0
    #<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    jdbc.timeBetweenEvictionRunsMillis=3000
    #<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    jdbc.minEvictableIdleTimeMillis=300000

     

     

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    蕃薯耀 2016年3月30日 15:46:15 星期三

    http://fanshuyao.iteye.com/

  • 相关阅读:
    c#下实现ping操作
    DevExpress 经典常用功能代码收集
    WPF – 使用触发器
    XtraGrid使用方法
    [读书笔记]STL源码剖析
    [微软面试100题]6170
    [读书笔记]Thinking in C++
    [转][算法]google page rank算法
    [微软面试100题]7180
    [转][算法]链接分析算法之:HITS算法
  • 原文地址:https://www.cnblogs.com/fanshuyao/p/6227143.html
Copyright © 2011-2022 走看看