Spring第十一篇——–Spring整合Hibernate之配置数据源
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&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&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之配置数据源的更多相关文章
- Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能
由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...
- Spring第13篇—–Spring整合Hibernate之声明式事务管理
不容置疑的我们可以知道Spring的事务管理是通过AOP(AOP把我们的事务管理织入到我们的业务逻辑里面了)的方式来实现的,因为事务方面的代码与spring的绑定并以一种样板式结构使用.(面向切面编程 ...
- spring 整合 hibernate xml配置
spring 整合 hibernate: hibernate :对数据库交互 spring: ioc aop 整合点: 1.sessionFactory对象不再由hibernate生成,交由spr ...
- Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览
本文是Spring Cloud专栏的第一篇文章,了解本篇文章内容有助于更好的理解后面文章 一.网站架构演变过程 1-1.传统架构 传统的SSH架构,分为三层架构 web控制层.业务逻辑层.数 ...
- 死磕Spring之AOP篇 - Spring AOP常见面试题
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring AOP总览
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring 事务详解
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- SpringBoot第二十一篇:整合ActiveMQ
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11190048.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 前一章节中 ...
随机推荐
- iOS开发之--UITextField属性
UITextField属性 0. enablesReturnKeyAutomatically 默认为No,如果设置为Yes,文本框中没有输入任何字符的话,右下角的返回按钮是disabled的. ...
- spark Mllib SVM实例
Mllib SVM实例 1.数据 数据格式为:标签, 特征1 特征2 特征3…… 0 128:51 129:159 130:253 131:159 132:50 155:48 156:238 157: ...
- css教程如何修改留言板程序
error_reporting(0);$conn = new com("adodb.connection"); $conn->open("driver={micro ...
- netbeans使用技巧总结+快捷键大全
部分经常用的快捷键: 使用快捷键Alt+Enter显示修复错误的方法. 4.导航 a)Alt+Shift+O :转到类. b)Ctrl+Tab :在打开的源文件中进行切换. c)Alt+Right ...
- oracle sql日期比较
oracle sql日期比较:在今天之前: select * from up_date where update < to_date('2007-09-07 00:00:00','yyyy-mm ...
- maven资源文件的相关配置
构建Maven项目的时候,如果没有进行特殊的配置,Maven会按照标准的目录结构查找和处理各种类型文件. src/main/java和src/test/java 这两个目录中的所有*.java文件会 ...
- Markdown基本用法
Markdown基本用法 不同的编辑器对本文中的功能可能显示情况不一样,马克飞象可以完美支持和显示. 一.标题 h1标题 #h1标题 h1标题 h1标题 == h2标题 ##h2标题 h2标题 h2标 ...
- PHP 命名空间总结
PHP 5.3 及以上版本中引入了命名空间 的概念. notes: 1. 在 PHP 中,命名空间用来解决在编写 类库 或 应用程序 时创建 可重用 的 代码如 类 或 函数 时碰到的两类问题: ① ...
- SQL查询(二)
常用查询技巧 1.获取数据的前3(n)行 ; 2.SQL语句中if语句 在SQL语句中没有直接的if语句,但是有两个函数:DECODE和CASE,他们能够实现if语句的功能 2.1)decode -- ...
- Lazarus -Pascal常量
2.常量 2.1.普通常量 仅仅下面类型可以被定义为常量Ordinal类型Set类型指针类型 (but the only allowed value is Nil). real类型 Char, Str ...