Spring数据库原理 之 DataSource

DataSource

引言

用Spring进行Web应用开发时,我们经常会做datasource的配置。而且datasource的配法风格各异。那么他们到底有哪些异同点呢?

在这里插入图片描述
在这里插入图片描述

DataSource作用

DataSource是javax.sql包中的类,是Java原生rt.jar包中的类。

public interface DataSource  extends CommonDataSource, Wrapper {
   

  Connection getConnection() throws SQLException;

  Connection getConnection(String username, String password)
    throws SQLException;
}

javax.sql.DataSource定义的是抽象方法,通过Java JNDI的方式将具体实现开放给各个厂商、组织自己、个人自己实现。

在Spring框架中,通过DataSource + 配置的方式,来定义具体的数据库源。并向Spring框架提供数据源的Connection服务。

在Spring中若想实现多数据源,那么就需要在DataSource下手。

javax.sql.DataSource源码注释说明

/**
 * <p>A factory for connections to the physical data source that this
 * {@code DataSource} object represents.  An alternative to the
 * {@code DriverManager} facility, a {@code DataSource} object
 * is the preferred means of getting a connection. An object that implements
 * the {@code DataSource} interface will typically be
 * registered with a naming service based on the
 * Java&trade; Naming and Directory (JNDI) API.
 * <P>
 * The {@code DataSource} interface is implemented by a driver vendor.
 * There are three types of implementations:
 * <OL>
 *   <LI>Basic implementation -- produces a standard {@code Connection}
 *       object
 *   <LI>Connection pooling implementation -- produces a {@code Connection}
 *       object that will automatically participate in connection pooling.  This
 *       implementation works with a middle-tier connection pooling manager.
 *   <LI>Distributed transaction implementation -- produces a
 *       {@code Connection} object that may be used for distributed
 *       transactions and almost always participates in connection pooling.
 *       This implementation works with a middle-tier
 *       transaction manager and almost always with a connection
 *       pooling manager.
 * </OL>
 * <P>
 * A {@code DataSource} object has properties that can be modified
 * when necessary.  For example, if the data source is moved to a different
 * server, the property for the server can be changed.  The benefit is that
 * because the data source's properties can be changed, any code accessing
 * that data source does not need to be changed.
 * <P>
 * A driver that is accessed via a {@code DataSource} object does not
 * register itself with the {@code DriverManager}.  Rather, a
 * {&#
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring框架中,spring.datasource是一个配置属性,用于配置数据源的相关信息。在application.properties文件中,可以通过设置spring.datasource.url来指定MySQL数据库的连接URL,包括主机名、端口号和数据库名称等信息。\[1\] 在Spring JDBC中,DataSource是用于获取物理数据源连接的工厂类。它是AbstractDataSource类的子类,实现了DataSource的所有父接口方法,但保留了getConnection()方法由子类实现。\[2\]通过配置spring.datasource.username和spring.datasource.password,可以指定连接数据库所需的用户名和密码。 总结来说,spring.datasource是用于配置数据源的相关信息,包括连接URL、用户名和密码等。DataSource是获取物理数据源连接的工厂类,用于在Spring框架中获取数据库连接。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [解析mysql的spring.datasource.url获取host,端口,数据库名,获取表里的 字段名,字段类型,字段注释](https://blog.csdn.net/dayonglove2018/article/details/111245219)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Spring数据库原理DataSource](https://blog.csdn.net/weixin_43318367/article/details/108745784)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值