SpringBoot怎么整合JdbcTemplate

      前言

      Spring对数据库的操作在jdbc上面做了更深层次的封装,而JdbcTemplate便是Spring提供的一个操作数据库的便捷工具。我们可以借助JdbcTemplate来执行所有数据库操作,例如插入,更新,删除和从数据库中检索数据,并且有效避免直接使用jdbc带来的繁琐编码

      初始化SpringBoot项目

      使用IDEA创建项目

      点击File–>New–>Project

      点击spring initializr,注意自己的SDK版本,再点击next

      填写自己的group、artifact,注意Java version的版本和前面的SDK版本一致,点击next

      选择自己要添加的依赖,项目创建的时候会自动加入到maven中,然后
      点击next

      填写自己的项目名,点击finish

      导入JDBC依赖

              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-data-jdbc</artifactId>
              </dependency>

      导入数据库驱动

      我这里使用的MySQL数据库,就导入MySQL数据库驱动

       <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <version>5.1.42</version>
              </dependency>

      修改配置文件

      我这里使用的是yml格式配置文件

      spring:
        datasource:
          url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8
          username: root
          password: admin
          driver-class-name: com.mysql.jdbc.Driver

      数据库sys_user表结构

      测试类代码

      查询sys_user表数据量

      package com.gavin.boot;
      
      import lombok.extern.slf4j.Slf4j;
      import org.junit.jupiter.api.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.jdbc.core.JdbcTemplate;
      import java.util.List;
      import java.util.Map;
      
      @Slf4j
      @SpringBootTest
      class BootJdbcApplicationTests {
      
          @Autowired
          JdbcTemplate jdbcTemplate;
      
          @Test
          void contextLoads() {
              //获取sys_user表数据量
              Long aLong = jdbcTemplate.queryForObject("select count(*) from sys_user", Long.class);
              log.info("记录总数:{}", aLong);
      }

      运行结果

      查询sys_user表一条数据

      package com.gavin.boot;
      
      import lombok.extern.slf4j.Slf4j;
      import org.junit.jupiter.api.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.jdbc.core.JdbcTemplate;
      import java.util.List;
      import java.util.Map;
      
      @Slf4j
      @SpringBootTest
      class BootJdbcApplicationTests {
      
          @Autowired
          JdbcTemplate jdbcTemplate;
      
          @Test
          void contextLoads() {
              //获取sys_user表一条数据
              Map<String, Object> map = jdbcTemplate.queryForMap("select * from sys_user where id = 1");
              log.info("map数据为:" +  map);
          }
      }

      运行结果

      查询sys_user表所有数据

      package com.gavin.boot;
      
      import lombok.extern.slf4j.Slf4j;
      import org.junit.jupiter.api.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.jdbc.core.JdbcTemplate;
      import java.util.List;
      import java.util.Map;
      
      @Slf4j
      @SpringBootTest
      class BootJdbcApplicationTests {
      
          @Autowired
          JdbcTemplate jdbcTemplate;
      
          @Test
          void contextLoads() {
          	//获取sys_user表所有数据
              List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from sys_user");
              log.info("list数据为:" + list);
          }
      }

      新增sys_user表一条数据

      package com.gavin.boot;
      
      import lombok.extern.slf4j.Slf4j;
      import org.junit.jupiter.api.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.jdbc.core.JdbcTemplate;
      import java.util.List;
      import java.util.Map;
      
      @Slf4j
      @SpringBootTest
      class BootJdbcApplicationTests {
      
          @Autowired
          JdbcTemplate jdbcTemplate;
      
          @Test
          void contextLoads() {
              //新增一条数据
              int i = jdbcTemplate.update("insert into sys_user(user_name, phone) values(?,?)", new String[]{"小王", "13100720084"});
              log.info("新增了" + i + "条数据");
          }
      }

      运行结果

      修改sys_user表一条数据

      package com.gavin.boot;
      
      import lombok.extern.slf4j.Slf4j;
      import org.junit.jupiter.api.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.jdbc.core.JdbcTemplate;
      import java.util.List;
      import java.util.Map;
      
      @Slf4j
      @SpringBootTest
      class BootJdbcApplicationTests {
      
          @Autowired
          JdbcTemplate jdbcTemplate;
      
          @Test
          void contextLoads() {
              //修改一条数据
              int i2 = jdbcTemplate.update("update sys_user set user_name = ? where id = 1", new String[]{"小张"});
              log.info("修改了" + i2 + "条数据");
          }
      }

      运行结果

      删除sys_user表一条数据

      package com.gavin.boot;
      
      import lombok.extern.slf4j.Slf4j;
      import org.junit.jupiter.api.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.jdbc.core.JdbcTemplate;
      import java.util.List;
      import java.util.Map;
      
      @Slf4j
      @SpringBootTest
      class BootJdbcApplicationTests {
      
          @Autowired
          JdbcTemplate jdbcTemplate;
      
          @Test
          void contextLoads() {
              //删除一条数据
              int i3 = jdbcTemplate.update("delete from sys_user where id = ?", 1);
              log.info("删除了" + i3 + "条数据");
          }
      }

      运行结果

      以上就是SpringBoot怎么整合JdbcTemplate的详细内容,更多请关注www.sxiaw.com其它相关文章!