对于数据访问层,无论是哪种数据库,springBoot 底层都是采用Spring Data的方式统一处理各种的数据库,Spring Data也是Spring中的Spring Boot、Spring Cloud一样的知名项目;
Sping Data 官网:https://spring.io/projects/spring-data
数据库相关的启动器 : 可以参考官方文档:https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/htmlsingle/#using-boot-starter
数据库启动器:

JDBC
首先创建一个新项目,在创建项目时要注意导入依赖,

在项目创建成功后就会看到在 pom.xml 文件中找到,但是如果在创建项目的时候没有导入,就要在pom.xml中手动的添加依赖;
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>连接数据库
在连接数据库之后,编写数据库配置文件
编写配置是一定要注意时区问题
spring:
  datasource:
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
#com.mysql.jdbc.Driver 是5版本的驱动
#com.mysql。cj.jdbc.Driver 是8版本的驱动
#?serverTimezone=UTC 解决时区的报错
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8配置完成后,会发现有 driver-class-name: com.mysql.jdbc.Driver 的报错,是因为自己使用的是 5 版本的数据驱动,但是导入的是 8版本的驱动,只需要在pom.xml文件中将<scope>runtime</scope>注掉就可以了;
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <!--<scope>runtime</scope>-->
        </dependency>配置完成,就可以直接使用,因为springboot自动完成了我们之前封装的步骤,那么就可以进行测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootDataApplicationTests {
    //数据源
    @Autowired
    //自动装配
     DataSource dataSource;
    @ Test
    public void contextLoads() throws SQLException {
//       查看默认数据源 class com.zaxxer.hikari.HikariDataSource
        System.out.println(dataSource.getClass());
        //获取数据库连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
//        关闭连接
        connection.close();
    }
}在输出后,可以看到默认配置的数据源:class com.zaxxer.hikari.HikariDataSource,搜索一下就可以找到数据源的所有自动配置都在 :DataSourceProperties 文件下;
CRUD操作
有了数据源就可以使用jdbc语句操作数据库;
数据库操作都是在 jdbcTemplate 中进行的
- jdbcTemplate 是springboot的核心文件
 - jdbcTemplate 用来简化数据库操作,内部定义了很多避免错误的机制
 - springboot默认提供了数据源com.zaxxer.hikari.HikariDataSource
 - jdbcTemplate 自动注入数据源,使用它不用管理数据源,也不用管理关闭问题
 
jdbcTemplate下面有提供了几种方法:
- query方法:用于执行数据库的查询
 - execute方法:可以执行任意的SQL语句
 - update方法:更新数据库,可以增加,删除,和修改
 
@RestController
public class JdbcController {
    @Autowired
// jdbcTemplate 是springboot的核心文件  用来简化数据库操作,内部定义了很多避免错误的机制
// springboot默认提供了数据源com.zaxxer.hikari.HikariDataSource
// springboot自动注入数据源,使用它不用管理数据源,也不用管理关闭问题
            JdbcTemplate jdbcTemplate;
    @GetMapping("/query")
    public List<Map<String, Object>> queryAll() {
        String sql = "select * from user";
        System.out.println(jdbcTemplate);
//       执行sql语句
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }
    //    增加用户
    @GetMapping("/addUser")
    public String addUser() {
        String sql = "insert into mybatis.user(id,name,pwd) values (5,'小明','123456')";
        jdbcTemplate.update(sql);
        return "addUser-ok";
    }
    //更新用户
    @GetMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id") int id) {
        String sql = "update mybatis.user set name=?,pwd=? where id=" + id;
        Object[] objects = new Object[2];
        objects[0] = "小明2";
        objects[1] = "zxcv";
        jdbcTemplate.update(sql, objects);
        return "updateUser-ok";
    }
    //删除用户
    @GetMapping("/delUser/{id}")
    public String delUser(@PathVariable("id") int id) {
        String sql = "delete from user where id=?";
        jdbcTemplate.update(sql, id);
        return "delUser-ok";
    }
}以上代码编译运行测试:

增加测试就可以看到添加成功,在数据库里面可以查看到已经添加好了

总结:
SpringBoot使用jdbc操作数据库的步骤:
- 编写JDBC的连接配置文件
 
- 根据驱动选择
 - 注意URL的时区问题
 - 处理乱码问题
 
- 在程序中执行
 
- 注入JdbcTemplate
 - 编写SQL语句
 - 使用JdbcTemplate执行SQL语句
 - 根据具体的方法的构造重载判断选择可以使用? 占位符,
 









