|
@@ -0,0 +1,99 @@
|
|
|
+package com.sf.druid.dao.impl;
|
|
|
+
|
|
|
+import com.sf.druid.dao.BookDao;
|
|
|
+import com.sf.druid.entity.Book;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.jdbc.core.*;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.stereotype.Repository;
|
|
|
+
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+// dao层 对数据增删改查的一层
|
|
|
+//@Component
|
|
|
+// @Repository 是一个具体的@Component 是操作数据库的bean
|
|
|
+@Repository("bookDao")
|
|
|
+public class BookDaoImpl implements BookDao {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Book> findAll() {
|
|
|
+ List<Book> bookList = jdbcTemplate.
|
|
|
+ query("select * from book", new BeanPropertyRowMapper<Book>(Book.class));
|
|
|
+ return bookList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int insert(Book book) {
|
|
|
+ String sql = """
|
|
|
+ insert into book(book_id,book_name,type_id,author_id,author_name,book_img,book_desc)
|
|
|
+ values(?,?,?,?,?,?,?)
|
|
|
+ """;
|
|
|
+ Object[] args = {book.getBookId(), book.getBookName(), book.getTypeId(),
|
|
|
+ book.getAuthorId(), book.getAuthorName(), book.getBookImg(), book.getBookDesc()};
|
|
|
+ int update = jdbcTemplate.update(sql, args);
|
|
|
+ return update;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Book findOne(String bookId) {
|
|
|
+ Book book = jdbcTemplate.queryForObject("select * from book where book_id = ?",
|
|
|
+ new BeanPropertyRowMapper<Book>(Book.class), bookId);
|
|
|
+ return book;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int delete(Book book) {
|
|
|
+ // id book_id
|
|
|
+ String sql = """
|
|
|
+ delete from book where book_id = ?
|
|
|
+ """;
|
|
|
+ Object[] args = {book.getBookId()};
|
|
|
+ int update = jdbcTemplate.update(sql, args);
|
|
|
+ return update;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int deleteById(String bookId) {
|
|
|
+ String sql = """
|
|
|
+ delete from book where book_id = ?
|
|
|
+ """;
|
|
|
+ int update = jdbcTemplate.update(sql, bookId);
|
|
|
+ return update;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String findAuthorIdById(String bookId) {
|
|
|
+ // 找到一行数据 或者 一个数据
|
|
|
+ Book book = jdbcTemplate.queryForObject(
|
|
|
+ "select * from book where book_id = ?",
|
|
|
+ new BeanPropertyRowMapper<Book>(Book.class), bookId
|
|
|
+ );
|
|
|
+ System.out.println(book);
|
|
|
+
|
|
|
+ String authorId = jdbcTemplate.queryForObject(
|
|
|
+ "select author_id from book where book_id = ?",
|
|
|
+// new BookRowMapper(), bookId
|
|
|
+ new SingleColumnRowMapper<>(String.class), bookId
|
|
|
+ );
|
|
|
+ System.out.println(authorId);
|
|
|
+
|
|
|
+ return book.getAuthorId();
|
|
|
+ }
|
|
|
+
|
|
|
+ static class BookRowMapper implements RowMapper<String> {
|
|
|
+ public static final BookRowMapper INSTANCE = new BookRowMapper();
|
|
|
+
|
|
|
+ private BookRowMapper() {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String mapRow(ResultSet rs, int rowNum) throws SQLException {
|
|
|
+ return rs.getString("author_id");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|