JavaWeb——基于JDBCTemplate框架的增删改查(二)

本文详细介绍了使用JDBCTemplate框架进行数据库操作的全过程,包括实体类创建、DAO层接口定义、具体实现及测试。通过示例展示了如何进行图书信息的添加、删除、修改、查询等操作。

我们已经完成了项目的搭建可以进入代码的编写阶段了。

一.创建实体包

第一步,创建一个与表中数据类型一直的实体包,并以表名命名。

二.创建Dao包

第二步,创建BookDao包,里面包含增删改查所需要用到接口方法。

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);
}

三.实例化Dao包

创建Dao包的实例类BookDaoImpl,实现BookDao接口。

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());
    }
}

四.创建测试类进行测试

以上就是完整的基于JDBCTemplate框架的增删改查操作。

需要注意的点

  1. 通过JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());//创建JdbcTemplate对象创建完成后直接调用template的方法即可。
  2. 在执行增删改操作时,调用update方法,执行多查询时调用query方法,执行单查询时调用queryForObject方法。
  3. 在执行查询操作时query方法的参数应该为(sql语句,new BeanPropertyRowMapper<实体>(实体.class,数组)

例1:

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

例2:

public int findCountByPubComp(String pubComp) {

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

通过这次学习,我感觉到了框架的方便,不用有那么繁琐的对数据库连接的操作,只需要调用相应的方法就可以完成相应的功能,感觉十分的方便,刷新了我的认知。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值