@Query annotation queries apply to queries that are queried for data that cannot be queried for results by keyword queries. This kind of query can get rid of the constraint like keyword query, declare the query directly in the corresponding interface method, the structure is clearer, this is the unique implementation of spring data.
- Index parameters and named parameters
1, the index parameter is as follows, the index value starts from 1, the query "? the number of X " needs to be in line with the number of parameters defined by the method, and the order must be consistent."
1 1? 2") 2 List<person> testQueryAnnotationParams1 (String lastName, string email);
Note: The 1,?2 in the above code represents a placeholder for the parameter, which needs to be in the same order as the parameters passed in the method. X is starting from 1.
2. Named parameters (this is recommended): You can define a parameter name, assign a value using @param ("parameter name"), instead of the order of the tubes.
1 // how parameters are passed for @query annotations 1: Named arguments 2 : LastName : Email")3 list<person> testQueryAnnotationParams2 (@Param ("Email ") string email, @Param ("lastName") string lastName);
Note: In the code above: lastName,: Email is named for the parameter, the parameters passed in the method use the @param annotation to identify the named parameter. In this way, the order of the parameters is not used.
3 . Queries containing like keywords
Mode 1: You can add "%" to the placeholder so that you do not have to add "%" in the Query method
1 // Span style= "COLOR: #008000" > like query Spring Date allows you to add a% 2 @Query ("Select p from person p WHERE p.lastname like %?1% OR p.email like %?2% ") 3 list<person> Testqueryannotationlikeparam (string lastName, string email);
1 @Test 2 Public void TestAnnoationParams3 () {3 list<person> persons = Personrepsitory.testqueryannotationlikeparam ("A", "[email protected]"); 4 System.out.println (persons); 5 }
Mode 2: Do not add "%" to the placeholder, so you must add "%" to the parameters of the Query method
1 // Span style= "COLOR: #008000" > like query 2 @Query ("Select p from person p WHERE p . lastName like? 1 OR p.email like? 2 ") 3 list& Lt Person> testQueryAnnotationLikeParam2 (String lastName, string email);
1 @Test 2 Public void TestAnnoationParams4 () {3 list<person> persons = PERSONREPSITORY.TESTQUERYANNOTATIONLIKEPARAM2 ("%a%", "%[email protected]%"); 4 System.out.println (persons); 5 }
Method 3: Add "%" to the named parameter
// Like queries use named parameters %:lastname% %:email% " ) List<Person> testQueryAnnotationLikeParam3 (@Param ("email") String email, @Param ("LastName") String lastName);
4. Query using native SQL
* * Set nativequery=true to query with native SQL * @return * *True ) long gettotalcount ();
Note: You can use native SQL to query when setting nativequery=true
1, write in @query annotation jpql implement delete and update operation must add @modifying annotation, to inform spring Data This is a delete or update operation.
2. The update or delete operation requires a transaction, at which point the service layer is defined and a transaction operation is added to the service layer's methods.
3, note that JPQL does not support insert operations.
1 @Transactional 2 @Modifying3 @Query ("UPDATE person p SET p.email =: email WHERE p.id =: id")4 void updatepersonemail (@Param ("id") Integer ID, @Param ("email") String email);
Springdata series four @Query annotations and @modifying annotations