springboot整合数据库
如何使用:
我们只需要在pom.xm文件中配置启动器即可:导入了数据源 JDBC 还有事务
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency>
但是由于springboot不知道我们用的是什么数据库,因此我们还要导入数据库连接配置
数据库版本和连接驱动版本要对应
由于springboot会进行版本仲裁,默认是最高版本。因此我们用maven的配置就近原则,将版本改为我们自己的版本
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency>
application.yaml 中修改配置项,启动项目。启动项目不报错。证明项目已经连接好数据源了
spring:
datasource:
url: jdbc:mysql://localhost:3306/ssmbuild
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver