总结一下spring中的JdbcTemplate的CRUD用法

栏目: Java · 发布时间: 6年前

内容简介:jpa做的项目,当条件查询多起来,还不固定的时候,查询方法就不好写了,又不好改成mybatis,那么JdbcTemplate将是个不错的选择表结构对应的实体类

jpa做的项目,当条件查询多起来,还不固定的时候,查询方法就不好写了,又不好改成mybatis,那么JdbcTemplate将是个不错的选择

表结构

CREATE TABLE `book` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `category` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

对应的实体类

public class Book implements Serializable {

    private Integer id;
    private String name;
    private String category;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", category='" + category + '\'' +
                '}';
    }
}

我这环境是spring-boot项目,依赖如下

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</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>
</dependencies>

文原接链: https://tomoya92.github.io/2019/05/31/spring-jdbc-template/

CRUD 用法

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JdbctemplateDemoApplicationTests {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 保存数据
    @Test
    public void save() {
        String sql = "insert into book (name, category) values (?, ?)";
        int update = jdbcTemplate.update(sql, "Spring", "java");
        System.out.println(update);
    }

    // 批量保存数据
    @Test
    public void batchSave() {
        String sql = "insert into book (name, category) values (?, ?)";
        List<Object[]> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Object[] objects = new Object[2];
            objects[0] = "Spring" + i;
            objects[1] = "java";
            list.add(objects);
        }
        int[] update = jdbcTemplate.batchUpdate(sql, list);
        for (int i = 0; i < update.length; i++) {
            System.out.println(update[i]);
        }
    }

    // 根据条件更新
    @Test
    public void update() {
        String sql = "update book set category = ? where name = ?";
        int update = jdbcTemplate.update(sql, "Java", "Spring");
        System.out.println(update);
    }

    // 批量更新
    @Test
    public void batchUpdate() {
        String sql = "update book set category = ? where name = ?";
        List<Object[]> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Object[] objects = new Object[2];
            objects[0] = "Java";
            objects[1] = "Spring" + i;
            list.add(objects);
        }
        int[] update = jdbcTemplate.batchUpdate(sql, list);
        for (int i = 0; i < update.length; i++) {
            System.out.println(update[i]);
        }
    }

    // 根据id查询book对象
    @Test
    public void queryForObject() {
        String sql = "select * from book where id = ?";
        Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Book.class), 1);
        System.out.println(book);
    }

    // 查询数量
    @Test
    public void queryForInt() {
        String sql = "select count(1) from book";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
        System.out.println(count);
    }

    // 查询返回列表(定义好的bean)
    // 如果不指定new BeanPropertyRowMapper<>(Book.class)则返回List<Map<String, Object>>格式的数据
    @Test
    public void queryForList() {
        String sql = "select * from book where category = ?";
        // queryForList() 方法没法返回自定义的bean列表,不过用这个query方法可以
        List<Book> books = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Book.class), "Java");
        System.out.println(books);
    }

    // 根据id删除
    @Test
    public void delete() {
        String sql = "delete from book where id = ?";
        int update = jdbcTemplate.update(sql, 1);
        System.out.println(update);
    }

    // 批量删除
    @Test
    public void batchDelete() {
        String sql = "delete from book where id = ?";
        List<Object[]> list = new ArrayList<>();
        for (int i = 2; i < 7; i++) {
            Object[] objects = new Object[1];
            objects[0] = i;
            list.add(objects);
        }
        int[] update = jdbcTemplate.batchUpdate(sql, list);
        for (int i = 0; i < update.length; i++) {
            System.out.println(update[i]);
        }
    }

}

原文链接:


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构与算法

数据结构与算法

[美] 乔兹德克 (Drozdek, A. ) / 郑岩、战晓苏 / 清华大学出版社 / 2006-1 / 69.00元

《国外计算机科学经典教材·数据结构与算法:C++版(第3版)》全面系统地介绍了计算机科学教育中的一个重要组成部分——数据结构,并以C++语言实现相关的算法。书中主要强调了数据结构和算法之间的联系,使用面向对象的方法介绍数据结构,其内容包括算法的复杂度分析、链表、栈队列、递归技术、二叉树、图、排序以及散列。《国外计算机科学经典教材·数据结构与算法:C++版(第3版)》还清晰地阐述了同类教材中较少提到......一起来看看 《数据结构与算法》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具