This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

📖 preface

Don't just be a tool man!

Because I write simple business requirements on a daily basis, I feel like a toolman, and I rarely learn more about the technology stack over time. You see tools, you see components, you see frames, you take them, you use them, you don’t have any volume, you don’t have any problems. But if you want to make more money, even if you’re building a wheel over and over again, you have to try and build one, and you don’t have to produce it, you can play with it. Some things only their own experience, in order to have the deepest feelings, participated in the practice, just good summary review learning.

SpringDataJPA is a subproject of SpringData technology. To use SpringDataJPA to access Data, only Data access layer interface implementation JpaRepository interface is required. Because JpaRepository interface inherits the PagingAndSortingRepository interface, so there are some of its functions.

Welcome to mySpringDataJpa column


🎈 Default inheritance method

In the previous case, we have introduced the method of adding, so we need to find one or more, how to remove one or more, to find one or more, we just need to based on the previous 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 results of querying a single operation are as follows:


🎆 keyword abstract method

  • In accordance with Spring Data rules, you can perform custom operations such as queries by defining methods in the Repository interface

When conditional queries are involved, SpringDataJpa supports defining conditional attributes in method names under data access layer interfaces. The naming convention is as follows: – Conditional attributes are connected by the conditional keyword. – The first letter of a conditional attribute must be capitalized

🎇SpringDataJpa parse method is the principle

When the framework resolves a method name, it intercepts the redundant prefixes of the method name, such as find, findBy, read, readBy, get, and getBy, and then resolves the rest. And if the last parameter of the method is Sort or Pageable, the relevant information is also extracted for sorting by rule 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, such as 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), where the method argument can be Collection

Type, which can be an array or an index-length parameter;

  • NotIn – Equivalent to “not in” in SQL, such as findByUsernameNotIn(Collection userList), the method argument can be

Collection type, which can also be an array or variable length parameter;

✨ Use of the SpringDataJpa keyword abstract method

Building on the previous case (Portal:SpringBoot series: SpringBoot projects using Jpa (SpringDataJpa) | August more challenges) to 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 > {/ / fuzzy search Student information to the public according to the name 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 running effect is as follows:


🎊 Transactions that operate on a single object

Spring Data provides a default Transactionality approach, where all queries are declared as read-only transactions, and update and delete objects are declared as transactions for persistence. See also:org.springframework.data.jpa.repository.support.SimpleJpaRepository<T,ID>

@org.springframework.stereotype.Repository @Transactional(readOnly = true) public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepository<T, ID>, JpaSpecificationExecutor<T> {...... @Transactional public void delete(ID id) { delete(findOne(id)); }... }Copy the code

For custom methods, to change the default Transactional style provided by Spring Data, annotate the @transactional declaration on the method, as shown in the following example:

@Transactional @Modifying @Query("update Student s set s.studentName=? 1 where s.studentId=? 2") int setFixedStudentNameFor(String studentName,int studentId);Copy the code

🎎 involves multipleRepositoryTransaction processing of

For multipleRepositoryThis part belongs to the business logic layer. Therefore, you need to implement multiple Repository calls in the Service layer and declare the transaction on the corresponding method.

Simply add the @transaction annotation to the method to make it easy to implement the Transaction. But here are a few things to note:

  • The transaction manager implemented by this annotation is the default, and if you don’t want the default to be the transaction manager, you can add it yourself.
  • The isolation level of transactions can also be set.
  • The transaction propagation behavior can also be set

Well, that’s the end of the story. The next section on SpringDataJpa’s common annotated tags and custom query statements (JPQL), you’ll need to do more to learn the real thing. Come on, ladies and gentlemen


The last

  • For more references, see here:Chen Yongjia’s Blog

  • Like the little friend of the blogger can add a concern, point a like oh, continue to update hey hey!