DataSource(数据源)提供了一个标准化的取得数据库连接的方式,通过getConnection()方法即可取得数据库的连接,Spring也提供了数据库连接池(DataBase connection pool),配置数据源的模板如下:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
     <!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
  </bean>

示例:

通过测试可以很清晰的知道需要加入:org.apache.commons.dbcp.jar与org.apache.commons.pool.jar与数据库驱动。

配置文件:

 
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
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" >
<context:component-scan base-package="net.zmcheng"/>
<bean id=" logInterceptor" class="net.zmcheng.aop.LogInterceptor"/>
<aop:config>
<aop:pointcut expression="execution(public * net.zmcheng.serviceImpl.UserServiceImpl.add())" id="servicePointCut"/>
<aop:aspect id="logAspect" ref=" logInterceptor">
<aop:before method="before" pointcut-ref="servicePointCut"/>
<aop:around method="aroundMethod" pointcut-ref="servicePointCut"/>
</aop:aspect>
</aop:config>
<!-- 设置数据源:提供了一个标准化的取得数据库连接的方式 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/BSWS?useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
</beans>

daoImpl:

 
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
package net.zmcheng.daoImpl;
 
import java.sql.Connection;
 
import javax.annotation.Resource;
import javax.sql.DataSource;
 
import org.springframework.stereotype.Component;
 
import net.zmcheng.dao.UserDao;
@Component
public class UserDaoImpl implements UserDao {
  private DataSource dataSource;
   public DataSource getDataSource() {
return dataSource;
}
   @Resource
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
   public void add() {
   try{
   Connection conn = dataSource.getConnection();
   conn.createStatement().executeUpdate("insert into Z values('zhangsan',5)");
   System.out.println("添加员工成功");
   conn.close();
   }
   catch (Exception e){
   e.printStackTrace();
   }
    }
}

经测试成功加入,其它的接口与类在上篇博客中都贴了,本篇只贴了代码改变的daoImpl类。

属性占位符方式配置数据源

另外还可以通过属性占位符方式配置数据源,这时数据源的属性放在了一个属性配置文件中,如:jdbc.properties,模板如下:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<context:property-placeholder location=“jdbc.properties”/>
<!--也可以使用XML的方式-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations">
      <value>classpath:jdbc.properties</value>
   </property>
</bean>  
 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
       destroy-method="close">
      <property name="driverClassName" value="${driverClassName}"/>
      <property name="url" value="${url}"/>
      <property name="username" value="${username}"/>
      <property name="password" value="${password}"/>
</bean>

PS:使用占位符的方式配置数据源,需要在类路径或绝对路径下建一个属性配置文件,如jdbc.properties。

 

Spring第十一篇——–Spring整合Hibernate之配置数据源的更多相关文章

  1. Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能

    由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...

  2. Spring第13篇—–Spring整合Hibernate之声明式事务管理

    不容置疑的我们可以知道Spring的事务管理是通过AOP(AOP把我们的事务管理织入到我们的业务逻辑里面了)的方式来实现的,因为事务方面的代码与spring的绑定并以一种样板式结构使用.(面向切面编程 ...

  3. spring 整合 hibernate xml配置

    spring 整合 hibernate: hibernate :对数据库交互 spring: ioc   aop 整合点: 1.sessionFactory对象不再由hibernate生成,交由spr ...

  4. Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览

    ​ ​本文是Spring Cloud专栏的第一篇文章,了解本篇文章内容有助于更好的理解后面文章 ​ 一.网站架构演变过程 1-1.传统架构 传统的SSH架构,分为三层架构 web控制层.业务逻辑层.数 ...

  5. 死磕Spring之AOP篇 - Spring AOP常见面试题

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  6. 死磕Spring之AOP篇 - Spring AOP总览

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  7. 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  8. 死磕Spring之AOP篇 - Spring 事务详解

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  9. SpringBoot第二十一篇:整合ActiveMQ

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11190048.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   前一章节中 ...

随机推荐

  1. gnuplotx轴的logscale显示

    假设数据是这样子的: gnuplot脚本如下: set terminal postscript eps color enhanced set log x set log y set format x ...

  2. json格式转换成Map的应用

    jsp 1.引用json.js(将json格式转换成字符串) 2. var name = document.getElementById("name").value; var re ...

  3. jQuery 图片剪裁插件初探之 Jcrop

    主页:http://deepliquid.com/content/Jcrop.html 官方下载地址:http://deepliquid.com/content/Jcrop_Download.html ...

  4. [SHELL实例] (转)最牛B的 Linux Shell 命令 (一)

    本文编译自commandlinefu.com ( 应该是 Catonmat ) 的系列文章 Top Ten One-Liners from CommandLineFu Explained .作为一个由 ...

  5. PHP Error 和 Logging 函数

    PHP Error 和 Logging 函数 PHP Error 和 Logging 简介 Error 和 Logging 函数允许您对错误进行处理和记录. Error 函数允许用户定义错误处理规则, ...

  6. nginx gzip 模块配置

    #gzip模块设置 gzip on; #开启gzip压缩输出 gzip_min_length 1k; #最小压缩文件大小 gzip_buffers 4 16k; #压缩缓冲区 gzip_http_ve ...

  7. maven run as(debug as)没有运行的选项时

    run as - run configration -maven build- goal目录下填上:tomcat:run即可

  8. php7安装

    # 配置参数 ./configure --prefix=/usr/local/php7 \ --with-config-file-path=/usr/local/php7/etc \ --with-m ...

  9. communicate with other processes, regardless of where they are running

    Advanced Programming in the UNIX Environment Third Edition   In the previous chapter, we looked at p ...

  10. n0_n1

    #include<stdio.h>int a[10];void quanpailie(int i){ if(i==10)  {for(i=0;i<10;i++)  {   print ...