整合JDBC:
1.创建数据表
2.修改POM.XML文件,添加jdbc启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
添加mysql数据库驱动,其中数据库版本可以去https://mvnrepository.com去查看
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
3.配置数据源,有两种方式
(1)一个是自定义配置文件方式
将数据源配置文件放在外部的配置文件中,spring项目启动时只会加载本项目下的application.properties或application.yml,这时就需要通过@PropertySource注解读取配置文件
这需要在POM.XML文件中添加Druid数据源依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
在/resources目录下添加.yml配置文件,配置关于数据库的信息
#数据库驱动
jdbc.driverClassName=com.mysql.jdbc.Driver
#连接数据库的url,本机地址localhost,默认端口3306,可以修改端口,连接的数据库名称为test,后边加一些设置,useSSL默认为true
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
#数据库用户名
jdbc.username=root
#数据库密码
jdbc.password=1234
创建配置类,一定要加@Configuration,可以在项目启动的时候加载这个配置类
由于该类中需要设置数据库的相关信息,所以要添加注解
@PropertySource("classpath:/jdbc.properties")
//自己的.properties文件放在/resources目录下,/resources目录最终是classpath
@Configuration
@PropertySource("classpath:/jdbc.properties")
public class JdbcConfiguration {
@Value("${jdbc.driverClassName}")//@Value()是从配置文件中取值,通过@Value()获取.properties配置文件中的值
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean//实例化
public DataSource getDataSource(){
DruidDataSource source = new DruidDataSource();
source.setPassword(this.password);
source.setName(this.username);
source.setUrl(this.url);
source.setClassName(this.driverClassName);
return source;
}
}
通过@ConfigurationProperties注解读取配置信息,这个注解是springboot下的注解,不是framework下的
这个方法对本电脑不使用,而且不常用
(2)一个是通过spring的application.properties或application.yml文件方式
即便pom.xml文件中配置了Druid启动器,如果不在配置文件中配置Druid数据源,则SpringBoot默认使用HikarDataSource数据源
在pom.xml中配置相关启动器等
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
在application.yml文件中配置Druid数据源
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: 'jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai&rewriteBatchStatements=true'
username: root
password: root
initial-size: 10
max-active: 100
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
#Oracle需要打开注释
#validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
stat-view-servlet:
enabled: true
url-pattern: /druid/*
#login-username: admin
#login-password: admin
filter:
stat:
log-slow-sql: true
slow-sql-millis: 5000
merge-sql: false
wall:
config:
multi-statement-allow: true