SSM中Druid数据源配置

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_38105115/article/details/90764355

1,引入依赖

<!--数据源-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.13</version>
    </dependency>

2,在application.xml配置文件中

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="locations">
        <list>
            <value>classpath:db.properties</value>
        </list>
    </property>
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="driverClassName" value="${db.driverClassName}" />
    <property name="url" value="${db.url}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
    <!--初始化值-->
    <property name="initialSize" value="3" />
    <!--最小空余的数量-->
    <property name="minIdle" value="3" />
    <!--最大连接数量-->
    <property name="maxActive" value="20" />
    <!--最长等待时间-->
    <property name="maxWait" value="60000" />
    <!--过滤器-->
    <property name="filters" value="stat,wall" />
</bean>

<!--监控数据库druid-->
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
    <property name="slowSqlMillis" value="3000" />
    <!--记录sql-->
    <property name="logSlowSql" value="true" />
    <!--记录sql的时候可以合并-->
    <property name="mergeSql" value="true" />
</bean>


<!--防火墙过滤-->
<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
    <property name="dbType" value="mysql" />
</bean>

3,在web.xml中设置拦截

<filter>
  <filter-name>DruidWebStatFilter</filter-name>
  <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
  <init-param>
    <param-name>exclusions</param-name>
    <param-value>*.js,*.css,*.jpg,*.png,*.ico,*.gif,/sys/druid/*</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>DruidWebStatFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<!--druid,对全局进行设计拦截-->
<servlet>
  <servlet-name>DruidStatServlet</servlet-name>
  <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
  <init-param>
    <param-name>loginUsername</param-name>
    <!--druid,设置默认登录名-->
    <param-value>druid</param-value>
  </init-param>
  <init-param>
    <param-name>loginPassword</param-name>
    <!--druid,设置默认登录密码-->
    <param-value>123456</param-value>
  </init-param>
</servlet>
<!--druid,对全局进行设计拦截-->
<servlet-mapping>
  <servlet-name>DruidStatServlet</servlet-name>
  <url-pattern>/sys/druid/*</url-pattern>
</servlet-mapping>

注意:我的路径是/sys/druid
在这里插入图片描述

展开阅读全文

没有更多推荐了,返回首页