UserJpaRepository

/**
* 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 list = new ArrayList();
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 list = new ArrayList();
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 userlists = userJpaRepository.findAll();
Assert.assertTrue(userlists.size()> 0);
}

/**
* Sort query according to age
*/< br> @Test
public void findALLSortAge() {
List lists = userJpaRepository.findAll(Sort.by(Sort.Direction.ASC, “age”));
for (User list: lists) {
System.out.println(list);
}
}

/**
* Paging query
*/
@Test< br> public void find AllByPage() {
PageRequest pageRequest = new PageRequest(0, 1);
Page userPage = userJpaRepository.findAll(pageRequest);
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 userPage = userJpaRepository.findAll(pageRequest);
List userList = userPage .getContent();
for (User user: userList) {
System.out.println(user);
}
}

/**
* Get all data according to the set of id
*/
@Test
public void findAllByIds() {
List ids = new ArrayList();
ids.add( 1L);
ids.add(2L);
ids.add(3L);
ids.add(4L);
List userList = userJpaRepository.findAllById(ids);< br> System.out.println(userList);
}

/**
* 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 userList = new ArrayList();
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 list = userJpaRepository.findAll(Example.of(user));
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 example = Example.of(user, matcher); List userList = userJpaRepository.findAll(example); System.out.println(userList); })

< /p>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2793 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.