chapter10_通过Spring和JDBC征服数据库_2_配置数据源
在声明模板和Repository之前, 我们需要先在Spring中配置一个数据源用于数据库连接
Spring提供了多种配置数据源bean的方式
(1) JDBC驱动程序定义的数据源
(2) JNDI查找的数据源
(3) 连接池的数据源
(4) 嵌入式数据源
使用JNDI数据源 P294
使用连接池数据源 P295
基于JDBC驱动的数据源
(1) 种类
1° DriverManagerDateSource: 每次有连接请求时都返回一个新建的连接
2° SimpleDriverDataSource: 和DriverManagerDateSource的工作方式类似, 但是直接使用JDBC驱动来解决特定环境下的类加载问题, 例如OSGI容器
3° SingleConnectionDataSource: 只有一个Connection的数据库连接池
(2) 示例
@Configurationpublic class JdbcConfig {@Beanpublic DataSource jdbcDataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/spitt");dataSource.setUsername("haha");dataSource.setPassword("123456");return dataSource;}...}
使用嵌入式的数据源
(1) 嵌入式数据库作为应用的一部分运行, 每次重启应用时都会__重新填充__测试数据
(2) 示例: 使用嵌入式的H2数据库
@Configurationpublic class JdbcConfig {@Beanpublic DataSource dataSource() {return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScripts("classpath:spittr/db/jdbc/schema.sql", "classpath:spittr/db/jdbc/test-data.sql").build();}...}
EmbeddedDatabaseBuilder使用了生成器模式, 它包含了各种addXXX,setXXX方式用于添加参数, 最后调用build会返回一个EmbeddedDatabase对象, 而EmbeddedDatabase继承于DataSource;
schema.sql是创建数据库的脚本, test-data是向数据库中添加测试数据的脚本
schema.sql
drop table if exists spittle;drop table if exists spitter;create table spitter (id identity,username varchar(25) not null,password varchar(25) not null,fullName varchar(100) not null,email varchar(50) not null,updateByEmail boolean not null);create table spittle (id integer identity primary key,spitter integer not null,message varchar(2000) not null,postedTime datetime not null,foreign key (spitter) references spitter(id) );
test-data.sql
insert into Spitter (username, password, fullname, email, updateByEmail) values ('habuma', 'password', 'Craig Walls', 'craig@habuma.com', false);insert into Spitter (username, password, fullname, email, updateByEmail) values ('mwalls', 'password', 'Michael Walls', 'mwalls@habuma.com', true);...insert into Spittle (spitter, message, postedTime) values (1, 'This is a test spittle message', '2012-06-09 22:00:00Z');insert into Spittle (spitter, message, postedTime) values (1, 'This is another test spittle message', '2012-06-09 22:10:00Z');...
使用__@profile__可以根据环境在运行时选择哪种数据库
chapter10_通过Spring和JDBC征服数据库_2_配置数据源相关推荐
- spring(10)通过spring 和 JDBC征服数据库
[0]README 1)本文部分文字描述转自:"Spring In Action(中/英文版)",旨在review "spring(10)通过spring 和 JDBC ...
- Java程序员从笨鸟到菜鸟之(七十八)细谈Spring(七)spring之JDBC访问数据库及配置详解
利用spring访问数据库是我们ssh程序中必不可少的步骤,在没有hibernate之前,我们一般都用jdbc访问数据库,所以用jdbc访问数据库必不可少的要进行一些配置,spring中为我们提供了访 ...
- Spring实战6-利用Spring和JDBC访问数据库
主要内容 定义Spring的数据访问支持 配置数据库资源 使用Spring提供的JDBC模板 写在前面:经过上一篇文章的学习,我们掌握了如何写web应用的控制器层,不过由于只定义了SpitterRep ...
- Spring Session Management – Spring Session JDBC
Spring Session Module provides APIs and implementation for managing user session in a web applicatio ...
- Spring - Spring配置文件-Spring配置数据源详解
文章目录 一般方式配置数据源 数据源(连接池)的作用 数据源开发的步骤 数据源创建演示(c3p0) 创建maven-web骨架项目 导入mysql.c3p0.druid.junit坐标 创建编写测试类 ...
- Java EE---通过Spring JDBC实现数据库的增、删、改、查
Spring JDBC之update.query方法 1.项目清单 2.全部代码 2.1.student 2.2.stuDao 2.3.applicationContext.xml 2.4.test ...
- Spring使用JDBC访问MySQL数据库
在Java应用程序开发中,使用JDBC访问MySQL数据库是Java开发者常用的技术.在Spring框架中,Spring对JDBC又进行了封装,简化了程序访问数据库的复杂度.本课主要讨论在Spring ...
- Spring JDBC 访问数据库
Spring JDBC是Spring所提供的持久层技术,它以一种更直接.更简单的方式使用JDBC API.在Spring JDBC里,用户仅需要做那些必不可杀的事儿,而将资源获取.Statement创 ...
- Spring DAO(2):Spring JDBC 访问数据库
以下完整示例代码地址:https://gitee.com/assad/springframework-test-daohttps://gitee.com/assad/springframework-t ...
最新文章
- 适用于WIFI Hacking的无线网卡推荐
- 算法----删除链表中的节点(Java)
- 基于DDD的.NET开发框架 - ABP模块设计
- IOS15仿地铁我的页面
- php 任意字符串_php 生成任意长度字符串的类(只含有数字 只含有字母 混合数字和字母)...
- epoll 浅析以及 nio 中的 Selector
- 深度学习分类类别不平衡_「图像分类」 关于图像分类中类别不平衡那些事
- 教你实现图片的惰性加载
- 何修改基于ArcGIS Server .NET ADF确良9.3的WEB SERVER端口使ArcGIS Server Manager来创建WEB应用的注意事项...
- ListT的RemoveAll方法
- 如何让Mac在 Finder 顶部显示完整的文件路径
- 统计项目代码行数工具,如何统计代码行数。
- mysql varbinary 长度_mysql8 参考手册--BINARY和VARBINARY类型
- 解构荣耀销量奇迹背后的化学反应:技术+品质+产品力
- 什么叫结构化程序设计,他的主要内容是什么?
- win7黑屏,提示副本不是正版的激活解决办法
- excel查找出不来了_Excel技巧:明明看到1了,为什么查找不到?
- JAVA对接发送SMS短信服务
- 安装AmaterasUML插件和GEF插件的详细步骤
- 多线程面试题_线程魔术技巧:使用Java线程可以做的5件事