Before introducing @query comments, let's look at how to use @namedquery for named queries
1. Now the definition method on the entity class has a specific query statement
@Entity
@NamedQuery (name = "Task.findbytaskname",
query = "Select T from Task t where T.taskname =? 1")
public Class task{
}
2. After we inherit the interface, we can use this method directly, it will execute our defined query statement and return the result
Public interface Taskdao extends Jparepository<task, long> {
Task findbytaskname (String taskname);
}
Imagine, if we want to define the execution of the query, using a named query, obviously not, because, will write a lot of @namedquery on the entity class, in this case, we can use @query directly on the method to define the query statement, such as this
Public interface Taskdao extends Jparepository<task, long> {
@Query ("select-T from Task t where T.taskname =? 1" )
Task findbytaskname (String taskname);
}
@Query above 1 represents the order of the method parameters, in addition to writing hql, we can also write SQL statements
Public interface Taskdao extends Jparepository<task, long> {
@Query ("select * from Tb_task t where T.task_name = ? 1 ", Nativequery = True)
Task findbytaskname (String taskname);
We can also use this as a parameter binding.
Public interface Taskdao extends Jparepository<task, long> {
@Query ("select-T from Task t where T.taskname =: ta Skname and T.createtime =: Createtime ")
Task findbytaskname (@Param (" TaskName ") String TaskName, @Param (" Createtime ") Date createtime);
}
Of course, on the parameter binding, we can also write a question mark directly
Public interface Taskdao extends Jparepository<task, long> {
@Query ("select-T from Task t where T.taskname =? and t.createtime =? ")
Task Findbytaskname (String taskname, Date createtime);
}
Using the Spel expression, we write the entity class as a dynamic
Public interface Taskdao extends Jparepository<task, long> {
@Query ("Select T from #{#entityName} t where t.task Name =? and t.createtime =? ")
Task Findbytaskname (String taskname, Date createtime);
}
The effect of this is that when two entity classes have a common parent class, for example
The identity of the JPA base class
@MappedSuperclass
@SuppressWarnings ("serial") public
abstract class identity implements serializable{
protected Long ID;
@Id
@GeneratedValue (strategy = generationtype.identity) public
Long getId () {
return Id;
}
public void SetId (Long id) {
this.id = ID;
}
}
@Entity public class
Task extends identity{
}
@Entity public
class Project extends identity{
}
Then there's a common interface
Public interface genericdao<t> extends Jparepository<t, id> {
@Query ("Select T from #{#entityName} T whe Re t.id=? 1 ") Public
T FindByID (Long ID);
}
Then Taskdao and Projectdao to inherit this interface, this way, the common method on the common interface, there is no need to repeat the method of writing.
Okay, let's talk about this, update with @modifying.
@Modifying
@Query ("Update Task t set t.taskname =? 1 where t.id =? 2")
int updatetask (String taskname, Long ID);
Here we say that spring data JPA's query strategy, spring data JPA can be queried using the creation method, or can be queried using the @query annotation, if @query is used on the method of the naming specification, the spring data is JPA a query executed by our defined statements, or by a canonical approach? Look at the query policy
The configuration of the query policy can be configured in Query-lookup-strategy, such as this
<jpa:repositories base-package= "Com.liuxg.**.dao"
repository-impl-postfix= "Impl
" Query-lookup-strategy = "Create-if-not-found"
entity-manager-factory-ref= "Entitymanagerfactory"
transaction-manager-ref= "TransactionManager" >
</jpa:repositories>
He has three kinds of values to configure.
Create-if-not-found (default): If a query statement is specified by a @Query, the statement is executed, if not, then the query is not @namequery specified, and if not, it is queried by resolving the method name
Create: Creates a query by parsing the method name. Even if there is @Query, @NameQuery will ignore
Use-declared-query: Executes the query by executing the statement defined by @query, and if not, see if there is no execution @namequery to execute the query, and no exception is thrown
@Query See here first, next time to understand how to expand the Spring data JPA interface, for example, I want to use sping data JPA interface, but I want to define some of the interface, we put them into one.