springboot2数据访问
SQL
1、在maven项目中导入starter依赖,导入jdbc场景
<dependency>-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency
2、分析自动配置
3、由用户自己导入数据库驱动
以mysql5.3为例,依赖版本需要和自己的mysql一致, 官方自动仲裁的版本是8.0.22,所以需要更改版本。还有jdbc驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
4、在yaml文件中设置各项内容
5、在测试类中测试连接
@Test
void contextLoads() {
List<Map<String, Object>> books= jdbcTemplate.queryForList("select * from books");
for (Map<String, Object> b :
books) {
System.out.println(b);
}
}
简单的jdbc配置和使用就此结束,springboot在底层为我们自动装配了很多东西,所以我们所要的操作就很少