boot spring 怎么执行hql_如何在spring启动时只使用hql获取一条记录

I am using hibernate with spring boot. I want to get next and previous record from table by passing an id. I have written a query in hql to get next and previous records from table. The problem is that i am getting all previous and all next records but i want only one previous and one last record.

Here is my DAO class for next record

@Transactional

public interface DataDao extends CrudRepository {

@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";

}

Here is DAO for previous record

@Transactional

public interface PrevDao extends CrudRepository {

@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.id2";

}

Here is my Controller

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

}

Here is myNext 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

解决方案

Here is your DAO for next record

@Transactional

public interface DataDao extends CrudRepository {

@Query(getDataId)

List 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";

}

Here is your DAO for previous record

@Transactional

public interface PrevDao extends CrudRepository {

@Query(getDataId)

List 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.id2";

}

Your Controller

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 Next = dataDao.getDataId(post_id, data_id);

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

}

And here is your Next 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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,你可以使用JPA(Java API)和HQL(Hibernate Query Language)来查询MySQL日期提前一天的数据。下面是一个示例代码: 首先,你需要在pom.xml文件中添加相应的依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 然后,在你的实体类中,使用`@Temporal`注解将日期字段标记为Temporal类型: ```java @Entity @Table(name = "your_table") public class YourEntity { // other fields @Temporal(TemporalType.DATE) private Date yourDate; // getters and setters } ``` 接下来,在你的Repository接口中定义一个自定义的查询方法,使用HQL语句来查询提前一天的日期数据: ```java @Repository public interface YourRepository extends JpaRepository<YourEntity, Long> { @Query("SELECT e FROM YourEntity e WHERE e.yourDate < :date") List<YourEntity> findDateBefore(@Param("date") LocalDate date); } ``` 最后,在你的Service或Controller中使用该方法来查询提前一天的日期数据: ```java @Service public class YourService { private final YourRepository yourRepository; public YourService(YourRepository yourRepository) { this.yourRepository = yourRepository; } public List<YourEntity> findDateBeforeYesterday() { LocalDate yesterday = LocalDate.now().minusDays(1); return yourRepository.findDateBefore(yesterday); } } ``` 这样,你就可以使用Spring Boot和JPA来查询提前一天的日期数据了。记得在使用前配置好你的数据库连接信息和实体类的映射关系。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值