/**
* Not only inherits PagingAndSortingRepository but also inherits QueryByExampleExecutor (example matcher)
*/
public interface UserJpaRepository extends JpaRepository
}< br>1
2
3
4
5
UserJpaRepositoryTests:
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserJpaRepositoryTests {
@Autowired
private UserJpaRepository userJpaRepository;
/**
* Execution seconds: 49422 49145
* Batch insert data
*/
@Test
public void BatchSave() {
long startTime = System.currentTimeMillis();
List
for (int i = 0 ; i <60000; i++) {
User user = new User();
user.setName(“ljk” + i);
user.setAge(i);
user.setAddress( “address” + i);
list.add(user);
if (i% 100 == 0) {
userJpaRepository.saveAll(list);
list.clear();< br> }
}
long endTime = System.currentTimeMillis();
System.out.println(“Execution seconds:” + (endTime-startTime));
}
/**
* Execution seconds: 48053 48394 execution speed is faster than BatchSave
* Save data in batches (efficient processing method) to reduce the submission of big things
*/
@Test
public void BatchSaveBest() {
long startTime = System.currentTimeMillis();
List
for (int i = 0; i <60000; i++) {
User user = new User();
user.setName( “ljk” + i);
list.add(user);
if (i% 100 == 0) {
userJpaRepository.saveAll(list);
userJpaRepository.flush();< br> list.clear();
}
}
long endTime = System.currentTimeMillis();
System.out.println(“Execution seconds:” + (endTime-startTime)) ;
}
/**
* Query all data
*/
@Test
public void findAll() {
List
Assert.assertTrue(userlists.size()> 0);
}
/**
* Sort query according to age
*/< br> @Test
public void findALLSortAge() {
List
for (User list: lists) {
System.out.println(list);
}
}
/**
* Paging query
*/
@Test< br> public void find AllByPage() {
PageRequest pageRequest = new PageRequest(0, 1);
Page
Assert.assertTrue(userPage.getContent().size() == 1);
}
/**
* Paging sort query
*/
@Test
public void findAllByPageAndSort() {
PageRequest pageRequest = new PageRequest(0, 3, Sort.by(Sort.Direction.ASC, “age”));
Page
List
for (User user: userList) {
System.out.println(user);
}
}
/**
* Get all data according to the set of id
*/
@Test
public void findAllByIds() {
List
ids.add( 1L);
ids.add(2L);
ids.add(3L);
ids.add(4L);
List
}
/**
* Delete all data in batch
*/
@Test
public void deleteAllInBatch( ) {
userJpaRepository.deleteAllInBatch();
}
/**
* Save data and refresh cache
*/
@Test
public void saveAndFlush() {
User user = new User();
user.setName(“ljk”);
user.setAge(18);
user.setAddress(“beijing”);
user.setSex(“1”);
User result = userJpaRepository.saveAndFlush(user);
Assert. assertNotNull(result);
}
/**
* Batch delete
*/
@Test
public void deleteInBatch() {
List
User user = new User();
user.setId(1l);
userList.add(user);
User user2 = new User ();
user2.setId(2l);
userList.add(user2);
User user3 = new User();
user3.setId(3l);
userList.add (user3);
User user4 = new User();
user4.setId(4l);
userList.add(user4);
userJpaRepository.deleteInBatch(userList);
}< /p>
/**
* Get data by id
*/
@Test
public void getOne() {
User user = userJpaRepository.findById(1L).get ();
System.out.println(user);
}
/**
* ExampleMatcher
*/
@Test
public void findUs erByExam() {
User user = new User();
user.setName(“ljk”);
List
System.out.println(list);
}
/** * Fuzzy query*/ @Test public void findUserByExamQuery() {User user = new User(); user.setName(” ljk”); user.setAddress(“beijing”); user.setAge(18); ExampleMatcher matcher = ExampleMatcher.matching() .withMatcher(“name”, ExampleMatcher.GenericPropertyMatchers.startsWith())//Fuzzy query match beginning, That is {username}% .withMatcher(“address”, ExampleMatcher.GenericPropertyMatchers.contains())//All fuzzy queries, that is, %{address}% .withIgnorePaths(“id”);//Ignore the field, that is, no matter what the id is No value is added to the query condition Example
< /p>