preface
Good attitude, not so tired. In a good mood, all you see is beautiful scenery.
"If you can't solve a problem for a while, use this opportunity to see your limitations and put yourself out of order." As the old saying goes, it's easy to let go. If you are distracted by something, learn to disconnect. Cut out the paranoia, the trash in the community, and get rid of the negative energy. Good attitude, not so tired. In a good mood, all you see is beautiful scenery.
SpringDataJPA
是 Spring Data
Technology under the subproject, usingSpringDataJPA
Access to data requires only the data access layer interface implementationJpaRepository
Interface. Due to theJpaRepository
Interface inheritsPagingAndSortingRepository
Interface, so it has some functionality as well.
For a quick start on SpringDataJpa, check out the blogger’sSpringDataJpa series of articles. Welcome to pay attention!
Default inheritance method
In the previous example, we introduced the method of adding, so we need to find one or more, delete one or more, how do we do it, find one or more, we just need to build on the last case, In com. Cyj. Springboot. StudentController under the controller class, add to add the following code:
/** * http://localhost:8090/findOne? id=1 *@param id
* @return student */
@RequestMapping("/findOne")
public Object findStudentById(int id) {
Student student = repository.findOne(id);
return student;
}
Copy the code
The result of querying a single operation is as follows:
Keyword abstract method
- According to Spring Data’s rules, you can perform custom operations such as queries by defining methods in the Repository interface
Must start with find, get, read, and when conditional queries are involved, SpringDataJpa supports defining conditional attributes in method names under the data access layer interface. The naming convention is as follows: – Conditional attributes are connected by conditional keywords. – The first letter of a condition attribute must be capitalized
SpringDataJpa parses the principle of methods
Find, findBy, read, readBy, get, getBy, etc.
Then parse the rest. And if the last argument to the method is of type Sort or Pageable, the relevant information is also extracted for regular sorting or paging queries. The following table lists the meanings of some keyword abstract methods: **
- And – equivalent to the And of SQL keyword, such as findByUsernameAndPassword (String user, Striang PWD).
- Or – equivalent to the Or keyword in SQL, such as findByUsernameOrAddress(String user, String addr);
- Between – Equivalent to the Between keyword in SQL, such as findBySalaryBetween(int Max, int min);
- LessThan – Equivalent to “<” in SQL, such as findBySalaryLessThan(int Max);
- GreaterThan – equivalent to “>” in SQL, such as findBySalaryGreaterThan(int min);
- IsNull – Equivalent to “is null” in SQL, such as findByUsernameIsNull();
- IsNotNull – Equivalent to “is not null” in SQL, such as findByUsernameIsNotNull();
- NotNull – equivalent to IsNotNull;
- Like – equivalent to “Like” in SQL, as in findByUsernameLike(String user);
- NotLike – Equivalent to “not like” in SQL, such as findByUsernameNotLike(String user);
- OrderBy – equivalent to the SQL “order by”, such as findByUsernameOrderBySalaryAsc (String user);
- Not – equivalent to the SQL “! = “, such as findByUsernameNot(String user);
- In – Equivalent to “In” In SQL, such as findByUsernameIn(Collection userList), the method argument can be Collection
Type, which can also be an array or an indeterminate parameter;
- NotIn – Equivalent to “not in” in SQL, such as findByUsernameNotIn(Collection userList), the argument to the method can be
Collection type, which can also be an array or an indeterminate parameter;
The use of the SpringDataJpa keyword abstract method
Based on the previous case, find all students whose names contain “Liu”.
In com. Cyj. Springboot. Under the repository configuration StudentRepository class to add the following code:
public interface StudentRepository extends JpaRepository<Student.Integer>{
// Query student information by name
public List<Student> findByStudentNameContaining(String name);
}
Copy the code
Add the following code to the StudentController class:
/*** * http://localhost:8090/findStudentByName? Name = liu *@param name
* @return* /
@RequestMapping("/findStudentByName")
public Object findStudentByName(String name) {
List<Student> student = repository.findByStudentNameContaining(name); return student;
}
Copy the code
Start the test and the result is as follows:
Well, that’s the end of it, and the next tutorial –SpringDataJpa Custom Query statements (JPQL) — is where you’ll learn the real stuff. Refueling everyone, the original is not easy, six points like collection is your biggest encouragement to me!
🎉 summary:
-
For more references, see here:The Blog of Chan Wing Kai
-
Like the small partner of the blogger can add a concern, a thumbs-up oh, continue to update hey hey!