我们已经完成了项目的搭建可以进入代码的编写阶段了。
一.创建实体包
第一步,创建一个与表中数据类型一直的实体包,并以表名命名。
二.创建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框架的增删改查操作。
需要注意的点
- 通过
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());//创建JdbcTemplate对象
创建完成后直接调用template的方法即可。 - 在执行增删改操作时,调用update方法,执行多查询时调用query方法,执行单查询时调用queryForObject方法。
- 在执行查询操作时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);
}
通过这次学习,我感觉到了框架的方便,不用有那么繁琐的对数据库连接的操作,只需要调用相应的方法就可以完成相应的功能,感觉十分的方便,刷新了我的认知。