java spring Long[](数组)类型

标签 java sql spring

如果我有下一个代码

/**
 * 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 的记录。

最佳答案

使用 JPA @NamedQuery

事件实体

@Entity
@Table(name = "event")
@NamedQuery(name = "Event.fetchEventItem",
        query = "SELECT id, name FROM event WHERE id IN (:ids)"
)
public class Event {
....
}

您的界面

 @Repository
    public interface EventRepository extends JpaRepository<Event, Long>{
        List<EventItem> findEvents(Long[] ids);
    }

接口(interface)实现类

@Repository
@Transactional(readOnly = true)
public class EventRepositoryImpl implements EventRepository {
 @PersistenceContext
 EntityManager entityManager;

     @Override
        public List<EventItem> findEvents(Long[] ids) {
        List<Event> list = new ArrayList<Event>();
         Query query = em.createNamedQuery("SELECT c FROM Country c");
            Query query = entityManager.createNamedQuery("Event.fetchEventItem", Event.class);
            query.setParameter(1, ids);
            list = query.getResultList();

            // Here You can Prapared List<EventItem>

        }


    }

关于java spring Long[](数组)类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54052550/

相关文章:

Java Date 在任何时区看起来都一样

sql - jsonb @> 搜索的 PostgreSQL 索引

java - myBatis 多对多关系

java - Spring MVC : No handler found for correctly resolved JSP

spring - RabbitMQ Spring Boot session 错误

java - JTextArea 中的 ScrollPane

java - 在 OnCreate() 方法中绑定(bind)服务时出现 NullPointerException

java - 记录器()slf4j错误: The method logger() from the type CoreLogger refers to the missing type Logger

MySQL 插入/更新触发器与 AUTO_INCRMENT

java - spring jdbc 优于 hibernate