springboot整合jdbc(1)

1.在项目的pom.xml中添加相关依赖

<!--jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--Druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
2.配置数据源:
方法1:在resource下创建jdbc.properties文件 存储数据库的配置信息
jdbc.driver-class-name=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.password=xxxxxxx
jdbc.username=root
然后就是spring的基于注解配置数据源的那一套了
创建配置类
package com.mc_74120.springbootjdbc.config;

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 javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

@Configuration
@PropertySource("classpath:jdbc.properties")
public class SpringjdbcConfig {
@Value("${jdbc.username}")
private String userName;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.driver-class-name}")
private String className;
@Bean
public DataSource getDataSource(){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName(className);
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(password);
return dataSource;
}
}
当springboot扫描到该配置类后 会将数据源加入容器
方法2:将
jdbc.driver-class-name=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.password=xxxxxxx
jdbc.username=root
写入application.properties文件中
并创建配置类:
package com.mc_74120.springbootjdbc.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

//@ConfigurationProperties(prefix = "jdbc")
//是springboot的注解 不能读取其他配置文件 只能读取springboot的application配置文件
public class JbdcConfiguration {
private String driver;
private String url;
private String password;
private String userName;

public String getDriver() {
return driver;
}

public String getUrl() {
return url;
}

public String getPassword() {
return password;
}

public String getUserName() {
return userName;
}

public void setDriver(String driver) {
this.driver = driver;
}

public void setUrl(String url) {
this.url = url;
}

public void setPassword(String password) {
this.password = password;
}

public void setUserName(String userName) {
this.userName = userName;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.print.DocFlavor;
import javax.sql.DataSource;

//@Configuration
//@EnableConfigurationProperties(JbdcConfiguration.class)//指定加载哪个配置信息属性类
public class JdbcConfig {
@Bean
public DataSource getDataSource(){
DruidDataSource druidDataSource=new DruidDataSource();
return druidDataSource;

}
}
读取application.properties中前缀为:jdbc的属性 并把对应的值一一赋值给JbdcConfiguration中的变量(以jdbc为前缀的属性名 去掉jdbc的部分 必须与jdbcConfigration中的变量名一一对应 比如说 driver必须有jdbc.driver与其对应
)
JdbcConfig类读取JbdcConfiguration中的值 并创建datasource
方法3:
//@Configuration
public class JdbcConfig {
//@ConfigurationProperties(prefix = "jdbc")
    @Bean
public DataSource getDataSource(){
DruidDataSource druidDataSource=new DruidDataSource();
return druidDataSource;

}
}
直接读取 application.perperties中以jdbc为前缀的值 并实例化 datasource
方法4:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=xxxx
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
在application文件中直接配置datasource并加入容器






springboot整合jdbc(1)

上一篇:2020年高教社杯全国大学生数学建模竞赛C题 第三问详细解答+代码


下一篇:freecplus框架-MySQL数据库操作