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. .NET生成静态页面的方案总结

    转载自:http://www.cnblogs.com/cuihongyu3503319/archive/2012/12/06/2804233.html 方法一:在服务器上指定aspx网页,生成html ...

  2. 仿5173游戏交易平台系统SQL注入(可直接脱裤)+Getshell

    最近没事登登好几年前玩过的游戏看看,发现有人喊高价收号,这一看就是骗子,这等骗子还想骗我?我就来看看这逗逼是怎么骗人的,结果发现这人给了一个说是 5173平台交易的网站,叫我直接把号的信息填上去然后填 ...

  3. 在Windows上启用LDAPs

    公司的环境比较特殊, Windows server + Linux desktop, 所以我们希望在server端启用LDAP over SSL功能. 当中走了不少弯路, 网上文章也搜了一大堆, 千辛 ...

  4. 禁止用户自己停止SEP - 飞舞的菜刀 - 51CTO技术博客

    员工在自己的工作站上,右键点击状态栏SEP图标,停止SEP服务,导致管理员定制的策略失效,针对上述情况,请安装下述方法操作. 1. 打开SEPM. 2. 在[策略]里选中你所使用的[防病毒和防间谍软件 ...

  5. [转]在VS2010 VC++项目中引用Lib静态库(以Openssl为例)

    本文转自:http://kb.cnblogs.com/page/94467/ Openssl是个为网络通信提供安全及数据完整性的一种安全协议,囊括了主要的密码算法.常用的密钥和证书封装管理功能以及SS ...

  6. jedis操作

    Jedis jedis = RedisUtil.getJedis(); try { // 向key-->name中放入了value-->minxr jedis.set("name ...

  7. QQ群笔记

     uuid就好比你的名字,类似到了班级里,你的名字会被学号替代.同样的连接之后,uuid会被handle句柄替代.   问下CC2541串口用DMA接收的时候,调试程序时候发现,串口发一帧数据,进入两 ...

  8. ①spirngMVC框架运行原理图

  9. BeanNameViewResolver

    As described in the documentation, BeanNameViewResolver resolves Views declared as beans. Usually yo ...

  10. Qt 之 使用 https发送 HTTP请求(使用OPENSSL库)

    一.简述 在使用Qt发送HTTP请求中一般使用的链接都是http://前缀,而有的服务器支持 https://前缀的链接,而Qt本身是支持https的,但是https访问需要用到SSL认证,而QT默认 ...