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

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

内容简介: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]);
        }
    }

}

原文链接:


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

查看所有标签

猜你喜欢:

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

The Apache Modules Book

The Apache Modules Book

Nick Kew / Prentice Hall PTR / 2007-02-05 / USD 54.99

"Do you learn best by example and experimentation? This book is ideal. Have your favorite editor and compiler ready-you'll encounter example code you'll want to try right away. You've picked the right......一起来看看 《The Apache Modules Book》 这本书的介绍吧!

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具