Home  >  Article  >  Java  >  How SpringBoot integrates JdbcTemplate

How SpringBoot integrates JdbcTemplate

王林
王林forward
2023-05-20 21:16:181359browse

    Preface

    Spring’s database operations are encapsulated in a deeper way on jdbc, and JdbcTemplate is a convenient tool provided by Spring to operate the database. . We can use JdbcTemplate to perform all database operations, such as inserting, updating, deleting and retrieving data from the database, and effectively avoiding the tedious coding caused by directly using jdbc

    Initializing the SpringBoot project

    Use IDEA creates a project

    Click File–>New–>Project

    How SpringBoot integrates JdbcTemplate

    Click spring initializr, pay attention to your SDK version, and then click next

    How SpringBoot integrates JdbcTemplate

    Fill in your own group and artifact. Note that the Java version is consistent with the previous SDK version. Click next

    How SpringBoot integrates JdbcTemplate

    to choose yourself The dependencies to be added will be automatically added to maven when the project is created, then
    click next

    How SpringBoot integrates JdbcTemplate

    Fill in your project name and click finish

    How SpringBoot integrates JdbcTemplate

    Import JDBC dependencies

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

    Import database driver

    The MySQL database I use here, just import the MySQL database driver

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

    Modify the configuration File

    I am using the yml format configuration file here

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

    Database sys_user table structure

    How SpringBoot integrates JdbcTemplate

    Test class code

    Query the data volume of the sys_user table

    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);
    }

    Running results

    How SpringBoot integrates JdbcTemplate

    Query a piece of data in the sys_user table

    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);
        }
    }

    Running results

    How SpringBoot integrates JdbcTemplate

    Query all data in sys_user table

    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);
        }
    }

    Add a new piece of data in sys_user table

    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 + "条数据");
        }
    }

    Run results

    How SpringBoot integrates JdbcTemplate

    How SpringBoot integrates JdbcTemplate

    Modify a piece of data in the sys_user table

    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 + "条数据");
        }
    }

    Run results

    How SpringBoot integrates JdbcTemplate

    How SpringBoot integrates JdbcTemplate

    ##Delete sys_user Table of data

    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 + "条数据");
        }
    }

    Running results

    How SpringBoot integrates JdbcTemplate

    How SpringBoot integrates JdbcTemplate

    The above is the detailed content of How SpringBoot integrates JdbcTemplate. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete