springboot通过bean连接MySQL数据库
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
@Configuration
@PropertySource("classpath:application-dev.properties")
public class DataConfig {@Value("${spring.datasource.driver-class-name}")
private String driver;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.mima}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setValidationQuery("SELECT 1");
dataSource.setTestOnBorrow(true);
return dataSource;
}}
【#springboot|springboot通过bean连接MySQL数据库】注意:如果是多模块的话,把该类放在公共类中
推荐阅读
- CS246: Mining Massive Datasets
- 避免重复造轮子,Java 程序员必备
- C#数据库|C#实现学生信息管理系统---登录实现
- 编程语言|20天拿到美团快手小米搜狐跟谁学offer
- Java|面试官问(说说你对ZooKeeper集群与Leader选举的理解())
- ORACLE|oracle利用函数实现oracle表生成mysql建表DDL
- VMware|搭建云平台 7 Day(上)________**控制节点网络配置**
- mysql|python数据库存 和 取 数据 ~~~~别磨叽了,拿来直接用吧
- java|springcloud-alibaba springSecurity整合oauth2 授权码模式