1.pom.xml文件引入druid和数据库连接jar包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< properties >
< druid.version >1.0.18</ druid.version >
</ properties >
< dependencies >
<!-- connection pool -->
< dependency >
< groupId >com.alibaba</ groupId >
< artifactId >druid</ artifactId >
< version >${druid.version}</ version >
</ dependency >
< dependency >
< groupId >mysql</ groupId >
< artifactId >mysql-connector-java</ artifactId >
< version >5.1.38</ version >
</ dependency >
</ dependencies >
|
2.jdbc.properties配置
1
2
3
4
5
6
7
8
9
10
|
datasource.mysql.driverClassName=com.mysql.jdbc.Driver
datasource.mysql.url=jdbc:mysql://localhost:3306/ganlandoudata?useUnicode=true&characterEncoding=UTF-8&useSSL=false
datasource.mysql.username=root
datasource.mysql.password=
jdbc.pool.init=1 连接池初始化的连接数
jdbc.pool.minIdle=3 连接池连接最小空闲数
jdbc.pool.maxActive=20 连接池最大活跃连接数
datasource.validationQuery=select 1 from dual 连接测试
datasource.testOnBorrow=true <span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(254, 254, 242);">申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。</span>
datasource.testOnReturn=false <span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; background-color: rgb(254, 254, 242);">归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能</span>
|
3.ibatis-config.xml关于mybatis的参数配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<!-- 全局参数 -->
< settings >
<!-- 使全局的映射器启用或禁用缓存。 -->
< setting name = "cacheEnabled" value = "true" />
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
< setting name = "lazyLoadingEnabled" value = "true" />
<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
< setting name = "aggressiveLazyLoading" value = "true" />
<!-- 是否允许单条sql 返回多个数据集 (取决于驱动的兼容性) default:true -->
< setting name = "multipleResultSetsEnabled" value = "true" />
<!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true -->
< setting name = "useColumnLabel" value = "true" />
<!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false -->
< setting name = "useGeneratedKeys" value = "true" />
<!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分 FULL:全部 -->
< setting name = "autoMappingBehavior" value = "FULL" />
<!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) -->
< setting name = "defaultExecutorType" value = "BATCH" />
<!-- 数据库超过25000秒仍未响应则超时 -->
< setting name = "defaultStatementTimeout" value = "1000" />
<!-- 使用驼峰命名法转换字段。 -->
< setting name = "mapUnderscoreToCamelCase" value = "true" />
<!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session -->
< setting name = "localCacheScope" value = "SESSION" />
<!-- 设置但JDBC类型为空时,某些驱动程序 要指定值,default:OTHER,插入空值时不需要指定类型 -->
< setting name = "jdbcTypeForNull" value = "NULL" />
</ settings >
<!-- 类型别名 -->
<!-- <typeAliases>
<typeAlias alias="Page" type="com.thinkgem.jeesite.common.persistence.Page" />分页
</typeAliases>
-->
<!-- 插件配置 -->
< plugins >
< plugin interceptor = "com.hl.core.mybatis.plugin.OffsetLimitInterceptor" >
< property name = "dialectClass" value = "com.hl.core.mybatis.dialect.MySQLDialect" />
</ plugin >
</ plugins >
</ configuration >
|
4.spring-mybatis.xml整合文件配置
1
2
|
<!-- 自动扫描 ,扫描指定包下的mapper-->
<context:component-scan base- package = "com.hl" />
|
1
2
3
4
5
6
7
|
<!--引入jdbc.properties配置文件-->
<bean id= "propertyConfigurer"
class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "location" value= "classpath:jdbc.properties" />
</bean>
<!-- MyBatis begin -->
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<!--sqlsession配置-->
<bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" >
<!-- 注入dataSource,引用已定义的id为 dataSource的bean-->
<property name= "dataSource" ref= "dataSource" />
<!-- 实体类 -->
<property name= "typeAliasesPackage" value= "com.hl.manage.*.entity.*" />
<!--实体类继承的父类-->
<property name= "typeAliasesSuperType" value= "com.hl.core.base.BaseEntity" />
<!--mapper.xml配置文件-->
<property name= "mapperLocations" value= "classpath:com/hl/**/mapper/*Mapper.xml" />
<!--myBatis配置文件-->
<property name= "configLocation" value= "classpath:ibatis-config.xml" ></property></bean><!-- 扫描basePackage下所有Dao的接口 --><bean id= "mapperScannerConfigurer" class = "org.mybatis.spring.mapper.MapperScannerConfigurer" ><property name= "sqlSessionFactoryBeanName"
value= "sqlSessionFactory" /><property name= "basePackage" value= "com.hl" /></bean><!-- 定义事务 --><bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" ><property name= "dataSource" ref= "dataSource" /></bean><!-- 配置
Annotation 驱动,扫描 @Transactional 注解的类定义事务 --><tx:annotation-driven transaction-manager= "transactionManager" proxy-target- class = "true" /><!-- MyBatis end --><!-- 数据源配置, 使用 druid数据库连接池 --><bean id= "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" init-method= "init"
destroy-method= "close" ><!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass --><property name= "driverClassName" value= "${datasource.mysql.driverClassName}" /><!-- 基本属性 url、user、password --><property name= "url" value= "${datasource.mysql.url}" /><property name= "username"
value= "${datasource.mysql.username}" /><property name= "password" value= "${datasource.mysql.password}" /><!-- 配置初始化大小、最小、最大 --><property name= "initialSize" value= "${jdbc.pool.init}" /><property name= "minIdle" value= "${jdbc.pool.minIdle}" /><property name= "maxActive"
value= "${jdbc.pool.maxActive}" /><!-- 配置获取连接等待超时的时间 --><property name= "maxWait" value= "60000" /><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name= "timeBetweenEvictionRunsMillis" value= "60000" /><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name= "minEvictableIdleTimeMillis"
value= "300000" /><!-- <property name= "validationQuery" value= "${jdbc.testSql}" /> --><property name= "testWhileIdle" value= "true" /><property name= "testOnBorrow" value= "false" /><property name= "testOnReturn" value= "false" /><!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用)
<property name= "poolPreparedStatements" value= "true" /> <property name= "maxPoolPreparedStatementPerConnectionSize" value= "20" /> --><!-- 配置监控统计拦截的filters ,注意此配置根据需要选择--><property name= "filters" value= "stat,wall,config" />
##若只需要监控请配置成<property name= "filters" value= "stat" />
<!--配置config时加此配置-->
<property name= "ConnectionProperties" value= "config.decrypt=true;config.decrypt.key=${publickey}" />
<!-- 防御SQL注入攻击 -->
<!-- <property name= "filters" value= "wall" /> -->
<!-- 数据库密码解密配置 -->
<!-- <property name= "filters" value= "config" /> -->
</bean>
<!-- 数据源配置, 不使用连接池 <bean id= "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "driverClassName" value= "${jdbc.driver}" /> <property name= "url"
value= "${jdbc.url}" /> <property name= "username" value= "${jdbc.username}" />
<property name= "password" value= "${jdbc.password}" /> </bean> -->
</beans>
|
5.web.xml配置检测访问
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!-- DruidStatView -->
< servlet >
< servlet-name >DruidStatView</ servlet-name >
< servlet-class >com.alibaba.druid.support.http.StatViewServlet</ servlet-class >
< init-param >
<!-- 允许清空统计数据 -->
< param-name >resetEnable</ param-name >
< param-value >true</ param-value >
</ init-param >
< init-param >
<!-- 用户名 -->
< param-name >loginUsername</ param-name >
< param-value >admin</ param-value >
</ init-param >
< init-param >
<!-- 密码,用于访问druid监测界面 -->
< param-name >loginPassword</ param-name >
< param-value >ganlandou123456</ param-value >
</ init-param >
<!-- 允许访问的ip
|
1
2
3
4
|
< init-param >
< param-name >allow</ param-name >
< param-value >128.242.127.1/24,128.242.128.1</ param-value >
</ init-param >
|
禁止访问的ip
1
2
3
4
5
6
7
8
9
|
< init-param >
< param-name >deny</ param-name >
< param-value >128.242.127.4</ param-value >
</ init-param > -->
</ servlet >
< servlet-mapping >
< servlet-name >DruidStatView</ servlet-name >
< url-pattern >/druid/*</ url-pattern >
</ servlet-mapping >
|
6.根据需要配置各类监控Spring-mvc.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<!-- druid web监控 -->
< 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,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</ param-value >
</ init-param >
<!-- 监控单个url调用的sql列表 -->
< init-param >
< param-name >profileEnable</ param-name >
< param-value >true</ param-value >
</ init-param >
<!-- 获取的session的name,根据name拿到存储的值 -->
< init-param >< param-name >principalSessionName</ param-name >
< param-value >user</ param-value >
< param-value >agtCompany</ param-value >
< param-value >cusUser</ param-value ></ init-param >
<!--是否开启session监控,若开启能获取服务传递的session信息 -->
< init-param >< param-name >sessionStatEnable</ param-name >
< param-value >true</ param-value ></ init-param ></ filter >
< filter-mapping >< filter-name >DruidWebStatFilter</ filter-name >< url-pattern >/*</ url-pattern ></ filter-mapping >
<!-- 关联Spring监测 -->
</ bean >
< bean id = "druid-stat-interceptor"
class = "com.alibaba.druid.support.spring.stat.DruidStatInterceptor" >
</ bean >
< bean id = "druid-stat-pointcut" class = "org.springframework.aop.support.JdkRegexpMethodPointcut"
scope = "prototype" >
< property name = "patterns" >
< list >
<!--基于方法进行拦截监测-->
< value >com.hl.manage.*.service.*.*</ value >
</ list >
</ property >
</ bean >
< aop:config >
< aop:advisor advice-ref = "druid-stat-interceptor"
pointcut-ref = "druid-stat-pointcut" />
</ aop:config >
|
7.可选安全的加密操作
数据库加密
1
2
3
4
5
6
|
数据库密码加密指令,cmd控制台执行
java -cp F:\server\MavenRepository\maven_jar\com\alibaba\druid\1.0.18\druid-1.0.18.jar com.alibaba.druid.filter.config.ConfigTools reiang1227
// java -cp后指向jar包,再指向com.alibaba.druid.filter.config.ConfigTools,最后加上数据库原始密码,会生成加密后的password及生成的秘钥publickey
将jdbc。properties中的数据库密码换掉,然后增加publickey参数配置
datasource.mysql.password=Je0i/vizWGnbgeYaXfuy/js/ZWeSMqnGRtjXbx/NK5pVhyUesUs91oih5W2qZpx6WzXB2ffQzByI/7K7tFlUvg==
publickey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIliWZYPXkbi6x7hAN7y4zIGqDD3aRSJgmDhWGtrU9cbH3DExXz/BvGoA9CmEkdCGzHAOXKaz1iHw5v4EqJ/hosCAwEAAQ==
|
8.访问方式
Ip:端口号/项目名/druid
以上就是Java开发druid数据连接池maven方式简易配置流程示例的详细内容,更多关于druid数据连接池maven配置的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/sinat_37464123/article/details/78732562