Glasnhost
Glasnhost

Reputation: 1135

Spring JPA + Mysql, how to implement a SELECT, FROM, WHERE query?

I'm struggling to understand how to implement a simple query like

SELECT * from Orders where category=1 and userid=1 

with Spring + JPA + Mysql.

I see that extending JpaRepository the query findByUserId and findByCategory are already implemented and working. However, how do I add a simple filter to the basic query? I cannot apply the filter to the result as it will be too big.

I am struggling to find the right direction.

Upvotes: 0

Views: 107

Answers (1)

Santi Wagner
Santi Wagner

Reputation: 328

If you extended JpaRepository, you can do something like this:

List<Order> findByUserIdAndCategory(Integer userId, Integer category);

You can take a look at JpaRepository query methods here: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

Upvotes: 1

Related Questions