spring boot datasource配置

		<!--引入jdbc starter -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
        
		<!--引入mysql驱动包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
#application.properties
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root

如果不指定spring.datasource.type,默认为org.apache.tomcat.jdbc.pool.DataSource,tomcat连接池的具体设置为:spring.datasource.tomcat.xxx=xxx,如maxActive属性:spring.datasource.tomcat.max-active=10或者spring.datasource.tomcat.maxActive,两种写法都可以,这是因为@ConfigurationProperties注解支持松散绑定

spring boot 连接池自动配置仅支持tomca、dbcp、dbcp2、Hikari,具体可以查看org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

spring.datasource的具体参数设置参考https://docs.spring.io/spring-boot/docs/1.5.21.RELEASE/reference/html/common-application-properties.html(页面搜索spring.datasource)

//启用spring-boot-starter-jdbc后,自动配置了jdbcTemplate,我们可以直接拿来使用
    @Autowired
	JdbcTemplate jdbcTemplate;

	@Test
	public void contextLoads() throws SQLException {
		
		jdbcTemplate.queryForList("select * from student");
	
	}

自定义数据源

#spring.datasource.type:指定数据源的实现类,这里我们使用Druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#下面配置为Druid的配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
@Configuration
public class DruidConfig {
	
	//只需要创建一个DataSource的实现托管到ioc容器中,指定properties的哪些前缀的属性与连接池的属性绑定,spring boot 根据spring.datasource.type利用反射为我们进行自动配置
	@ConfigurationProperties(prefix = "spring.datasource")
	@Bean
	public DataSource druid() {
		return new DruidDataSource();
	}
}

下图为spring boot为自定义数据源创建原理 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java Spring Boot中,YAML(Yet Another Markup Language)是一种简洁的配置文件格式,用于替代XML配置Spring Boot通过Spring Boot Actuator提供对YAML的支持,使得配置管理更加直观和易读。 以下是使用Spring Boot配置YAML的基本步骤: 1. **创建YAML配置文件**: 在`src/main/resources`目录下创建一个名为`application.yml`或`application.properties`(默认为properties格式,但Spring Boot也支持YAML)的文件。例如,对于YAML配置: ```yaml server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: user password: pass ``` 2. **引用配置**: 在主应用类上添加`@SpringBootApplication`注解,并启用YAML加载,可以通过`spring.config.location`属性指定配置文件位置,如`spring.config.location=classpath:application.yml`。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ConfigurationProperties; @SpringBootApplication @ConfigurationProperties(prefix = "spring.datasource") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. **使用配置属性**: 使用@ConfigurationProperties注解可以自动将YAML中的键值对映射到Java类的属性上,使得配置更易于管理和使用。 4. **使用Profile(环境变量)**: Spring Boot支持基于环境的配置,可以在YAML中定义不同的环境模式,如`application-dev.yml`、`application-prod.yml`等,通过`spring.profiles.active`属性切换环境。 相关问题-- 1. Spring Boot如何区分YAML和properties配置? 2. 如何在Spring Boot中启用YAML配置并指定文件路径? 3. 什么情况下会使用Spring Boot的Profile功能?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值