public List<Product> getAllProductMess(List<Product> products){ List<Product> allProducts = null; String sql ="select pid,pname,pimage,shop_price,cid from product"; try { allProducts = queryRunner.query(connection, sql, new BeanListHandler<>(Product.class)); } catch (SQLException throwables) { throwables.printStackTrace(); } return allProducts; }这个方法在service里怎么写
时间: 2024-02-14 08:32:56 浏览: 34
在 Service 中,可以考虑先获取数据库连接,然后调用 DAO 层的方法执行查询操作,再将查询结果返回。例如:
```
public List<Product> getAllProductMess() {
List<Product> allProducts = null;
try {
// 获取数据库连接
Connection conn = DBUtil.getConnection();
// 调用 DAO 层的方法执行查询操作
allProducts = productDao.getAllProductMess(conn);
// 关闭数据库连接
DBUtil.closeConnection(conn);
} catch (SQLException e) {
e.printStackTrace();
}
return allProducts;
}
```
其中,`productDao` 是对应的 DAO 层对象,`DBUtil` 是数据库连接工具类。在 DAO 层中,可以将原来的方法改为接收一个数据库连接参数,如下所示:
```
public List<Product> getAllProductMess(Connection conn) throws SQLException {
List<Product> allProducts = null;
String sql = "select pid,pname,pimage,shop_price,cid from product";
allProducts = queryRunner.query(conn, sql, new BeanListHandler<>(Product.class));
return allProducts;
}
```
这样就可以在 Service 层中获取到所有商品信息了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)