Spring整合Hibernate,Druid(Maven)

来源:互联网 发布:倍投软件下载 编辑:程序博客网 时间:2024/05/18 03:40

首先,说一下spring,spring有很好的整合能力,Web应用的各个层次都能整合起来,以前比较流行的SSH

,现在比较流行的SSM,数据库连接池可以是c3p0,也可以是dbcp等。


不过最近比较火的数据路连接池是国内阿里巴巴自己写的Druid.

项目地址:https://github.com/alibaba/druid

主页上有相关的文档: https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。

Druid 0.1.18 之后版本都发布到maven中央仓库中,所以你只需要在项目的pom.xml中加上dependency就可以了。例如:

    <dependency>        <groupId>com.alibaba</groupId>        <artifactId>druid</artifactId>        <version>${druid-version}</version>    </dependency>
也可以选择 Maven仓库查找公共的仓库地址:http://www.mvnrepository.com/artifact/com.alibaba/druid
这里我直接贴出applicationContext.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"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "><!--可以将数据库连接池的配置文件单独放在另一个properties中--><context:property-placeholder location="classpath:db.properties"/>
<!-- 配置hibernate属性 --><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><!-- 注入连接池 ,是druid连接池--><property name="dataSource" ref="druidDataSource"></property><!-- 配置其他属性 --><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop><prop key="hibernate.formate_sql">true</prop><prop key="hibernate.hbm2ddl">update</prop><prop key="hibernate.connection.autocommit">false</prop></props></property></bean>
<!-- 阿里数据库连接池 --><bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close"><!-- 基本属性 url、user、password --><property name="url" value="jdbc:mysql://localhost:3306/test" /><property name="username" value="用户名" /><property name="password" value="密码" /><!-- 配置初始化大小、最小、最大 --><property name="initialSize" value="10" /><property name="minIdle" value="50" /><property name="maxActive" value="50" /><!-- 配置获取连接等待超时的时间 --><property name="maxWait" value="60000" /><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name="minEvictableIdleTimeMillis" value="300000" /><property name="validationQuery" value="SELECT 'x'" /><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><!-- 打开PSCache,并且指定每个连接上PSCache的大小 --><property name="poolPreparedStatements" value="false" /><property name="maxPoolPreparedStatementPerConnectionSize"value="20" />
<!-- 配置监控统计拦截的filters --><property name="filters" value="stat" /></bean></beans>
将依赖加入到pom.xml文件中,我用的是1.0.10,其他的依赖包自行加入,注意要加入spring-orm包,他整合spring和hibernate
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.10</version></dependency>




0 0