JavaWeb-Additions, deletions, and modifications based on the JDBCTemplate framework (two)

We have completed the construction of the project and can enter the coding phase.

1. Create an entity package

The first step is to create an entity package consistent with the data type in the table and name it after the table name.

2. Create Dao package

The second step is to create the BookDao package, which contains the interface methods needed for adding, deleting, modifying and checking.

public interface BookDao {
    
    
    //添加图书
    int save(Book b);

    //删除图书
    int del(String bid);

    //修改图书
    int update(Book b);

    //查询全部图书
    List<Book> findAll();

    //根据书号查找图书对象
    Book findBookByBid(String bid);

    //统计图书数量:参数为出版社模糊查询
    int findCountByPubComp(String pubComp);

    /*查询图书,参加为四个参数:出版社模糊查询,
    出版日期范围查询,书名模糊查询,价格范围查询*/
    List<Book> findByFourPara(String bName, String pubComp, String pubDate, Double price);
}

Three. Instantiate the Dao package

Create an instance class BookDaoImpl of the Dao package to implement the BookDao interface.

JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());//创建JdbcTemplate对象

    //添加
    public int save(Book b) {
    
    
        String sql = "insert into book values(?, ?, ?, ?, ?, ?, ?)";
        return template.update(sql, b.getBid(), b.getbName(), b.getAuthor(), b.getPubComp(),
                b.getPubDate(), b.getbCount(), b.getPrice());
    }

    //删除
    public int del(String bid) {
    
    
        String sql = "delete from book where bid = ?";
        return template.update(sql, bid);
    }

    //修改
    public int update(Book b) {
    
    
        String sql = "update book set bName = ?, author = ?, pubComp = ?, pubDate = ?, " +
                "bCount = ?, price = ? where bid = ?";
        return template.update(sql, b.getbName(), b.getAuthor(), b.getPubComp(),
                b.getPubDate(), b.getbCount(), b.getPrice(), b.getBid());
    }

    //查询
    public List<Book> findAll() {
    
    
        String sql = "select * from book";
        return template.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
    }

    //根据书号,查找图书对象
    public Book findBookByBid(String bid) {
    
    
        String sql = "select * from book where bid = ?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), bid);
    }

    //统计图书数量:参数为出版社模糊查询
    public int findCountByPubComp(String pubComp) {
    
    

        String sql = "select count(bid) from book where pubComp like '%' ? '%'";
        return template.queryForObject(sql, Integer.class, pubComp);
    }

    /*查询图书,参加为四个参数:出版社模糊查询,
    出版日期范围查询,书名模糊查询,价格范围查询*/
    public List<Book> findByFourPara(String bName, String pubComp, String pubDate, Double price) {
    
    
        List<Object> list = new ArrayList<Object>();
        String sql = "select * from book where 1 = 1 ";
        if (bName != null && bName.length() != 0) {
    
    
            list.add(bName);
            sql = sql + " and bName like '%' ? '%' ";
        }

        if (pubComp != null && pubComp.length() != 0) {
    
    
            list.add(pubComp);
            sql = sql + "and pubComp like '%' ? '%' ";
        }
        if (pubDate != null && pubDate.length() != 0) {
    
    
            list.add(pubDate);
            sql = sql + "and pubDate like '%' ? '%' ";
        }
        if (price > 0.0) {
    
    
            list.add(price);
            sql = sql + "and price > ?";
        }
        return template.query(sql, new BeanPropertyRowMapper<Book>(Book.class), list.toArray());
    }
}

Four. Create a test class for testing

The above is the complete addition, deletion, modification, and check operation based on the JDBCTemplate framework.

Points to note

  1. By JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());//创建JdbcTemplate对象directly calling template of the method can be created.
  2. When performing addition, deletion and modification operations, the update method is called, the query method is called when multiple queries are executed , and the queryForObject method is called when executing a single query .
  3. When performing a query operation parameters of the method should query ( SQL statement , new BeanPropertyRowMapper < Entity > ( entity .class, arrays )

Example 1:

public Book findBookByBid(String bid) {
    
    
        String sql = "select * from book where bid = ?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), bid);
    }

Example 2:

public int findCountByPubComp(String pubComp) {
    
    

        String sql = "select count(bid) from book where pubComp like '%' ? '%'";
        return template.queryForObject(sql, Integer.class, pubComp);
    }

Through this study, I feel the convenience of the framework, there is no need to have such tedious operations on the database connection, only need to call the corresponding method to complete the corresponding function, I feel very convenient, refreshing my understanding.

Guess you like

Origin blog.csdn.net/missingtheway/article/details/104947226