如何在 spring boot 中使用 hql 只获取一条记录
How to get only one record using hql in spring boot
我在 spring 启动时使用休眠。我想通过传递 id 从 table 获取下一条和上一条记录。我在 hql 中编写了一个查询,以从 table 获取下一条和上一条记录。问题是我正在获取所有之前和之后的所有记录,但我只想要一个之前和最后一个记录。
这是我的 DAO class 下一条记录
@Transactional
public interface DataDao extends CrudRepository<Data, Long> {
@Query(getDataId)
NextPrevData getDataId(Long post_id, Long data_id);
final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id>?2";
}
这里是DAO 之前的记录
@Transactional
public interface PrevDao extends CrudRepository<Data, Long> {
@Query(getDataId)
NextPrevData getDataId(Long post_id, Long data_id);
final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id<?2";
}
这是我的控制器
@RequestMapping("/{post_id}/{data_id}")
@ResponseBody
public Next getDataInfo(@PathVariable("post_id") long post_id, @PathVariable("data_id") long data_id, HttpServletRequest req, HttpServletResponse res) throws ServletException {
NextPrevData Next = dataDao.getDataId(post_id, data_id);
NextPrevData Prev = prevDao.getDataId(post_id, data_id);
Post2 postobj = postDao2.findOne(post_id);
Data d = dataDao.findOne(data_id);
if(Next.isEquals(null)){
postobj.setNextStatus(false);
}
else{
postobj.setNextStatus(true);
}
if(Prev.isEmpty()){
postobj.setPrevStatus(false);
}
else{
postobj.setPrevStatus(true);
}
return new Next(Next,postobj,Prev,d);
}
这是我的下一个class
public class Next {
private NextAndPrevious Next;
private NextAndPrevious Prev;
private Post2 post;
private Data d;
public Post2 getPost() {
return post;
}
public void setPost(Post2 post) {
this.post = post;
}
public Next(NextAndPrevious Next, Post2 postobj, NextAndPrevious Prev, Data d) {
this.Next = Next;
this.post = postobj;
this.Prev = Prev;
this.d = d;
}
//Getters and seters
这是您的下一条记录的 DAO
@Transactional
public interface DataDao extends CrudRepository<Data, Long> {
@Query(getDataId)
List<NextPrevData> getDataId(Long post_id, Long data_id);
final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id>?2";
}
这是您之前记录的 DAO
@Transactional
public interface PrevDao extends CrudRepository<Data, Long> {
@Query(getDataId)
List<NextPrevData> getDataId(Long post_id, Long data_id);
final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id<?2";
}
你的控制器
RequestMapping("/{post_id}/{data_id}")
@ResponseBody
public Next getDataInfo(@PathVariable("post_id") long post_id, @PathVariable("data_id") long data_id, HttpServletRequest req, HttpServletResponse res) throws ServletException {
List<NextPrevData> Next = dataDao.getDataId(post_id, data_id);
List<NextPrevData> Prev = prevDao.getDataId(post_id, data_id);
Post2 postobj = postDao2.findOne(post_id);
Data d = dataDao.findOne(data_id);
long a=0;
long b=0;
if(Next.isEmpty()){
postobj.setNextStatus(false);
}
else{
postobj.setNextStatus(true);
a = Next.get(0).getDataId();
}
if(Prev.isEmpty()){
postobj.setPrevStatus(false);
}
else{
postobj.setPrevStatus(true);
b = Prev.get(0).getDataId();
}
return new Next(a,postobj,b,d);
}
这是你的下一个 class
public class Next {
private long Next;
private long Prev;
private Post2 post;
private Data d;
public Post2 getPost() {
return post;
}
public void setPost(Post2 post) {
this.post = post;
}
public Next(long Next, Post2 postobj, long Prev, Data d) {
this.Next = Next;
this.post = postobj;
this.Prev = Prev;
this.d = d;
}
//Getters and setters
}
我在 spring 启动时使用休眠。我想通过传递 id 从 table 获取下一条和上一条记录。我在 hql 中编写了一个查询,以从 table 获取下一条和上一条记录。问题是我正在获取所有之前和之后的所有记录,但我只想要一个之前和最后一个记录。
这是我的 DAO class 下一条记录
@Transactional
public interface DataDao extends CrudRepository<Data, Long> {
@Query(getDataId)
NextPrevData getDataId(Long post_id, Long data_id);
final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id>?2";
}
这里是DAO 之前的记录
@Transactional
public interface PrevDao extends CrudRepository<Data, Long> {
@Query(getDataId)
NextPrevData getDataId(Long post_id, Long data_id);
final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id<?2";
}
这是我的控制器
@RequestMapping("/{post_id}/{data_id}")
@ResponseBody
public Next getDataInfo(@PathVariable("post_id") long post_id, @PathVariable("data_id") long data_id, HttpServletRequest req, HttpServletResponse res) throws ServletException {
NextPrevData Next = dataDao.getDataId(post_id, data_id);
NextPrevData Prev = prevDao.getDataId(post_id, data_id);
Post2 postobj = postDao2.findOne(post_id);
Data d = dataDao.findOne(data_id);
if(Next.isEquals(null)){
postobj.setNextStatus(false);
}
else{
postobj.setNextStatus(true);
}
if(Prev.isEmpty()){
postobj.setPrevStatus(false);
}
else{
postobj.setPrevStatus(true);
}
return new Next(Next,postobj,Prev,d);
}
这是我的下一个class
public class Next {
private NextAndPrevious Next;
private NextAndPrevious Prev;
private Post2 post;
private Data d;
public Post2 getPost() {
return post;
}
public void setPost(Post2 post) {
this.post = post;
}
public Next(NextAndPrevious Next, Post2 postobj, NextAndPrevious Prev, Data d) {
this.Next = Next;
this.post = postobj;
this.Prev = Prev;
this.d = d;
}
//Getters and seters
这是您的下一条记录的 DAO
@Transactional
public interface DataDao extends CrudRepository<Data, Long> {
@Query(getDataId)
List<NextPrevData> getDataId(Long post_id, Long data_id);
final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id>?2";
}
这是您之前记录的 DAO
@Transactional
public interface PrevDao extends CrudRepository<Data, Long> {
@Query(getDataId)
List<NextPrevData> getDataId(Long post_id, Long data_id);
final String getDataId= "select new netgloo.models.NextPrevData(d.id) from Data d where d.postId=?1 and d.id<?2";
}
你的控制器
RequestMapping("/{post_id}/{data_id}")
@ResponseBody
public Next getDataInfo(@PathVariable("post_id") long post_id, @PathVariable("data_id") long data_id, HttpServletRequest req, HttpServletResponse res) throws ServletException {
List<NextPrevData> Next = dataDao.getDataId(post_id, data_id);
List<NextPrevData> Prev = prevDao.getDataId(post_id, data_id);
Post2 postobj = postDao2.findOne(post_id);
Data d = dataDao.findOne(data_id);
long a=0;
long b=0;
if(Next.isEmpty()){
postobj.setNextStatus(false);
}
else{
postobj.setNextStatus(true);
a = Next.get(0).getDataId();
}
if(Prev.isEmpty()){
postobj.setPrevStatus(false);
}
else{
postobj.setPrevStatus(true);
b = Prev.get(0).getDataId();
}
return new Next(a,postobj,b,d);
}
这是你的下一个 class
public class Next {
private long Next;
private long Prev;
private Post2 post;
private Data d;
public Post2 getPost() {
return post;
}
public void setPost(Post2 post) {
this.post = post;
}
public Next(long Next, Post2 postobj, long Prev, Data d) {
this.Next = Next;
this.post = postobj;
this.Prev = Prev;
this.d = d;
}
//Getters and setters
}