猿问

java spring long [](数组)类型

如果我有下一个代码


/**

 * Spring Data JPA repository for the Event entity.

 */

@Repository

public interface EventRepository extends JpaRepository<Event, Long>{

    @Query("SELECT id, name FROM event WHERE id IN (:ids)")

    List<EventItem> findEvents(@Param("ids") Long[] ids);

}

并想要使用它


Long[] ids = new Long[3];

ids[0] = new Long(1);

ids[1] = new Long(2);

ids[2] = new Long(3);

eventRepository.findEvents(ids);

如何正确使用。我是Spring框架的初学者。我想同时获得一些带有特殊ID的记录。


www说
浏览 334回答 2
2回答

慕桂英3389331

使用JPA@NamedQuery 即事件实体@Entity@Table(name = "event")@NamedQuery(name = "Event.fetchEventItem",&nbsp; &nbsp; &nbsp; &nbsp; query = "SELECT id, name FROM event WHERE id IN (:ids)")public class Event {....}您的介面&nbsp;@Repository&nbsp; &nbsp; public interface EventRepository extends JpaRepository<Event, Long>{&nbsp; &nbsp; &nbsp; &nbsp; List<EventItem> findEvents(Long[] ids);&nbsp; &nbsp; }接口实现类@Repository@Transactional(readOnly = true)public class EventRepositoryImpl implements EventRepository {&nbsp;@PersistenceContext&nbsp;EntityManager entityManager;&nbsp; &nbsp; &nbsp;@Override&nbsp; &nbsp; &nbsp; &nbsp; public List<EventItem> findEvents(Long[] ids) {&nbsp; &nbsp; &nbsp; &nbsp; List<Event> list = new ArrayList<Event>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Query query = em.createNamedQuery("SELECT c FROM Country c");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query = entityManager.createNamedQuery("Event.fetchEventItem", Event.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.setParameter(1, ids);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list = query.getResultList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Here You can Prapared List<EventItem>&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答