将启动器,数据库驱动,数据源依赖添加到pom.xml文件中
<!-- jdbc启动器 -->
org.springframework.boot
spring-boot-starter-jdbc
<!-- 数据库驱动,根据自己的数据库版本决定-->
mysql
mysql-connector-java
8.0.13
>
>com.alibaba >
>druid >
>1.1.12 >
>
在resource目录创建jdbc.properties文件
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/miduoduo?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
创建一个类用于获取DataSource
@Configuration
@PropertySource("classpath:/jdbc.properties")//加载指定的properties文件
public class JDBCConfig {
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Bean
public DataSource getDataSource() {
DruidDataSource source=new DruidDataSource();
source.setUsername(username);
source.setPassword(password);
source.setUrl(url);
source.setDriverClassName(driverClassName);
return source;
}
}
通过属性:
直接在application配置文件中添加jdbc连接属性,注意除了前缀后面的部分必须与属性名相同
yaml格式:
jdbc:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/miduoduo?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
username: root
password: 123456
properties格式:
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/miduoduo?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
创建一个JDBC属性配置类(随便啥子名字都行)
@ConfigurationProperties(prefix = "jdbc")//springboot的注解,只能读取springboot的配置文件,prefix规定读取以什么为前缀的属性
public class JDBC {
private String username;
private String password;
private String url;
private String driverClassName;
public JDBC() {
}
public JDBC(String username, String password, String url, String driverClassName) {
super();
this.username = username;
this.password = password;
this.url = url;
this.driverClassName = driverClassName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
}
获取DataSource的类
@Configuration
//@PropertySource("classpath:/jdbc.properties")//加载指定的properties文件
@EnableConfigurationProperties(JDBC.class)
public class JDBCConfig {
@Autowired
private JDBC jdbc;
@Bean
public DataSource getDataSource() {
DruidDataSource source=new DruidDataSource();
source.setUsername(jdbc.getUsername());
source.setPassword(jdbc.getPassword());
source.setUrl(jdbc.getUrl());
source.setDriverClassName(jdbc.getDriverClassName());
return source;
}
}
通过构造
获取DataSource的类
@Configuration
//@PropertySource("classpath:/jdbc.properties")//加载指定的properties文件
@EnableConfigurationProperties(JDBC.class)
public class JDBCConfig {
private JDBC jdbc;
public JDBCConfig(JDBC jdbc){
this.jdbc=jdbc;
}
@Bean
public DataSource getDataSource() {
DruidDataSource source=new DruidDataSource();
source.setUsername(jdbc.getUsername());
source.setPassword(jdbc.getPassword());
source.setUrl(jdbc.getUrl());
source.setDriverClassName(jdbc.getDriverClassName());
return source;
}
}
通过入参
获取DataSource的类
@Configuration
//@PropertySource("classpath:/jdbc.properties")//加载指定的properties文件
@EnableConfigurationProperties(JDBC.class)
public class JDBCConfig {
@Bean
public DataSource getDataSource(JDBC jdbc) {
DruidDataSource source=new DruidDataSource();
source.setUsername(jdbc.getUsername());
source.setPassword(jdbc.getPassword());
source.setUrl(jdbc.getUrl());
source.setDriverClassName(jdbc.getDriverClassName());
return source;
}
}
没有JDBC配置属性类,直接在获取DataSource的类中装配DruidDataSource
直接在application配置文件中添加jdbc连接属性
yaml格式:
jdbc:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/miduoduo?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
username: root
password: 123456
properties格式:
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/miduoduo?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
获取DataSource的类
@Configuration
//@PropertySource("classpath:/jdbc.properties")//加载指定的properties文件
//@EnableConfigurationProperties(JDBC.class)
public class JDBCConfig {
@Bean
@ConfigurationProperties(prefix = "jdbc")//直接将注解标注在方法上,自动装配在配置文件中以jdbc为前缀的属性
public DataSource getDataSource() {
DruidDataSource source=new DruidDataSource();
return source;
}
}