如何在 spring boot 和 hibernate 中统计特定 id 的记录
How to count records of particular id in spring boot and hibernate
我正在开发一个使用 Spring 启动和休眠的项目。我想根据 table 中的 id 计算记录。我可以计算 table 的所有记录,但无法根据外键计算记录。
这是我的控制器代码
@RequestMapping("/rating/{id}")
@ResponseBody
public long getRatingInfo(@PathVariable("id") long id, HttpServletRequest req, HttpServletResponse res) throws Exception {
long postobj = myDao.count();
return postobj;
}
这里我在url中传递了一个id
但是没有count
的方法来传递这个id来查找记录。
这是我的 Dao 接口
@Transactional
public interface MyDaoInterface extends CrudRepository<Rating, Long>{
}
将此添加到您的 DAO:
@Query(countByPostId)
Integer countByPostId(Long post_id);
final String countByPostId= "SELECT COUNT(ra) FROM Rating ra WHERE ra.post_id = ?1"
也可以这样称呼它:
@RequestMapping("/rating/{id}")
@ResponseBody
public long getRatingInfo(@PathVariable("id") long id, HttpServletRequest req, HttpServletResponse res) throws Exception {
long postobj = myDao.countByPostId(id);
return postobj;
}
编辑:评论中的第二个问题:
@Transactional
public interface MyDaoInterface extends CrudRepository<Rating, Long>{
List<Rating> findByPostId(Long id);
}
和您的来电者:
@RequestMapping("/rating/{id}")
@ResponseBody
public long getRatingInfo(@PathVariable("id") long id, HttpServletRequest req, HttpServletResponse res) throws Exception {
List<Rating> ratings = myDao.findByPostId(id);
long postobj = ratings.size() //use this code where you effectively need the # of entires.
return ratings;
}
@RequestMapping("/rating/{id}")
@ResponseBody
public long getRatingInfo(@PathVariable("id") long id, HttpServletRequest req, HttpServletResponse res) throws Exception {
List<Rating> ratings = myDao.findByPostId(id);
long postobj = ratings.size() //use this code where you effectively need the # of entires.
return ratings;
}
我正在开发一个使用 Spring 启动和休眠的项目。我想根据 table 中的 id 计算记录。我可以计算 table 的所有记录,但无法根据外键计算记录。
这是我的控制器代码
@RequestMapping("/rating/{id}")
@ResponseBody
public long getRatingInfo(@PathVariable("id") long id, HttpServletRequest req, HttpServletResponse res) throws Exception {
long postobj = myDao.count();
return postobj;
}
这里我在url中传递了一个id
但是没有count
的方法来传递这个id来查找记录。
这是我的 Dao 接口
@Transactional
public interface MyDaoInterface extends CrudRepository<Rating, Long>{
}
将此添加到您的 DAO:
@Query(countByPostId)
Integer countByPostId(Long post_id);
final String countByPostId= "SELECT COUNT(ra) FROM Rating ra WHERE ra.post_id = ?1"
也可以这样称呼它:
@RequestMapping("/rating/{id}")
@ResponseBody
public long getRatingInfo(@PathVariable("id") long id, HttpServletRequest req, HttpServletResponse res) throws Exception {
long postobj = myDao.countByPostId(id);
return postobj;
}
编辑:评论中的第二个问题:
@Transactional
public interface MyDaoInterface extends CrudRepository<Rating, Long>{
List<Rating> findByPostId(Long id);
}
和您的来电者:
@RequestMapping("/rating/{id}")
@ResponseBody
public long getRatingInfo(@PathVariable("id") long id, HttpServletRequest req, HttpServletResponse res) throws Exception {
List<Rating> ratings = myDao.findByPostId(id);
long postobj = ratings.size() //use this code where you effectively need the # of entires.
return ratings;
}
@RequestMapping("/rating/{id}")
@ResponseBody
public long getRatingInfo(@PathVariable("id") long id, HttpServletRequest req, HttpServletResponse res) throws Exception {
List<Rating> ratings = myDao.findByPostId(id);
long postobj = ratings.size() //use this code where you effectively need the # of entires.
return ratings;
}