Springboot 使用 JdbcTemplate

Springboot 使用JdbcTemplate

book

package com.draymonder.book.jdbc;

public class Book {
  private Integer id;
  private String name;
  private String author;

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

  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 getAuthor() {
    return author;
  }

  public void setAuthor(String author) {
    this.author = author;
  }
}

bookDao

package com.draymonder.book.jdbc;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class BookDao {

  @Autowired
  JdbcTemplate jdbcTemplate;

  public int addBook(Book book) {
    return jdbcTemplate
        .update("insert into book(name, author) values (?, ?)", book.getName(), book.getAuthor());
  }

  public int updateBook(Book book) {
    return jdbcTemplate
        .update("update book set name=?, author=? where id=?", book.getName(), book.getAuthor(),
            book.getId());
  }

  public int deleteBookById(Integer id) {
    return jdbcTemplate.update("delete from book where id=?", id);
  }

  public Book getBookById(Integer id) {
    return jdbcTemplate.queryForObject("select * from book where id=?", new BeanPropertyRowMapper<>(Book.class), id);
  }

  public List<Book> getAllBooks() {
    return jdbcTemplate.query("select * from book", new BeanPropertyRowMapper<>(Book.class));
  }

}

bookService

package com.draymonder.book.jdbc;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookService {

  @Autowired
  BookDao bookDao;

  public int addBook(Book book) {
    return bookDao.addBook(book);
  }

  public int updateBook(Book book) {
    return bookDao.updateBook(book);
  }

  public int deleteBookById(Integer id) {
    return bookDao.deleteBookById(id);
  }

  public Book getBookById(Integer id) {
    return bookDao.getBookById(id);
  }

  public List<Book> getAllBooks() {
    return bookDao.getAllBooks();
  }

}

bookController

package com.draymonder.book.jdbc;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BookController {
  @Autowired
  BookService bookService;

  @GetMapping("/bookOps")
  public void bookOps() {
    Book b1 = new Book();
    b1.setName("西厢记");
    b1.setAuthor("王实甫");
    int i = bookService.addBook(b1);
    System.out.println("add book >>> " + i);

    Book b2 = new Book();
    b2.setId(1);
    b2.setName("朝花夕拾");
    b2.setAuthor("鲁迅");
    int updateBook = bookService.updateBook(b2);
    System.out.println("update book >>> " + updateBook);

    Book b3 = bookService.getBookById(1);
    System.out.println("get book >>> " + b3);

    int delete = bookService.deleteBookById(1);
    System.out.println("delete book >>> " + delete);

    List<Book> allBooks = bookService.getAllBooks();
    allBooks.forEach(System.out::println);
  }
}
posted @ 2019-11-09 16:26  Draymonder  阅读(184)  评论(0编辑  收藏  举报