druid在spring中的配置已经maven中的配置



        <dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>druid</artifactId>
	    <version>1.0.18</version>
	</dependency>
	<dependency>
	    <groupId>org.wisdom-framework</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.34_1</version>
	</dependency>
        <dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jdbc</artifactId>
		<version>${version.spring-framework}</version>
	</dependency>

这里不用mysql中,是因为mysl中的mysql-connector-java要出问题

spring.xml文件


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"  
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:task="http://www.springframework.org/schema/task" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"
           default-autowire="byName">

	<bean id="app" class="com.nb.store.App"></bean>
	<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"  
       p:host="10.xx.xx.xx" 
       p:username="xxxxxxx" 
       p:password="xxxxxxxxx"/> 
     
    <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage" 
       p:from="noreply@nubia.cn"/>
    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    	<property name="resourceLoaderPath" value="/" />
		<property name="velocityProperties">
		<props>
		<prop key="input.encoding">UTF-8</prop>
		<prop key="output.encoding">UTF-8</prop>
		<prop key="default.contentType">text/html; charset=UTF-8</prop>
		</props>
		</property>
    </bean>
    <context:property-placeholder location="classpath:xxxxx"/>  这里注意,如果没有xxxx文件,会报错
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
	    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
	    <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?zeroDateTimeBehavior=convertToNull&autoReconnect=true&useUnicode=true&characterEncoding=utf-8" />
	    <property name="username" value="root" />
	    <property name="password" value="root" />
	     
	    <property name="filters" value="stat" />
	 
	    <property name="maxActive" value="20" />
	    <property name="initialSize" value="1" />
	    <property name="maxWait" value="60000" />
	    <property name="minIdle" value="1" />
	 
	    <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" />
	     
	    <property name="poolPreparedStatements" value="true" />
	    <property name="maxPoolPreparedStatementPerConnectionSize" value="50" />
	</bean>
	<!--JDBC模板定义 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>
</beans>



 
要使用 `druid-spring-boot-starter`,你需要在你的 Maven 项目添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> ``` 然后,你可以在你的 `application.properties` 或 `application.yml` 文件配置 `druid` 数据源。以下是一个示例: ```yaml spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC username: root password: yourpassword type: com.alibaba.druid.pool.DruidDataSource # 下面是 druid 配置 # 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 initial-size: 5 # 最小连接池数量 min-idle: 5 # 最大连接池数量 max-active: 20 # 获取连接时最大等待时间,单位毫秒 max-wait: 60000 # 是否开启 PSCache pool-prepared-statements: true # 指定每个连接上 PSCache 的大小 max-pool-prepared-statement-per-connection-size: 20 # 打开removeAbandoned功能 remove-abandoned: true # 180秒,也就是3分钟 remove-abandoned-timeout: 180 # 关闭abanded连接时输出错误日志 log-abandoned: true # 监控配置 filter: # 开启监控统计功能 stat: enabled: true # 是否打印 SQL 语句 log-slow-sql: true # 慢 SQL 记录时间阈值,单位毫秒 slow-sql-millis: 5000 # 配置监控统计拦截的 URI,多个用逗号隔开 web-stat-filter: exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" # 配置 Druid 的 StatViewServlet stat-view-servlet: url-pattern: /druid/* # IP 白名单 allow: 127.0.0.1 # IP 黑名单(共同存在时,deny优先于allow) deny: 192.168.0.1 # 登录用户名 login-username: admin # 登录密码 login-password: admin123 ``` 以上是一个基本的 `druid` 配置,你可以根据自己的实际需求进行调整。配置完成后,你就可以在代码使用 `DataSource` 了,例如: ```java @Autowired DataSource dataSource; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值