Wechat official account: If you have any questions or suggestions, please leave a message on the background. I will try my best to solve your problems.

preface

This is another contribution from a junior student, a friend who likes technology. The following is the transcript:

When I read the official documents of Spring Data JPA today, I found that there is no complete semantic translation of JPA keywords. So I wrote a Chinese document today. If there are any mistakes, please kindly comment.

Here is the official image with sample code and comments:

Start by referring to the official documentation to create the specified database

CREATE TABLE `demo_jpa`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL.`last_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL.`sex` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL.`email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL.`age` int(12) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
Copy the code

Sample code and comments < see above order >

/ * * *@Author: EvilSay
 * @Date: 2019/2/25 16:15 * /
public interface DemoJpaRepositories extends JpaRepository<DemoJpa.Integer> {

    // Search by firstName and LastName (both must be in database)
    DemoJpa findByFirstNameAndLastName(String firstName, String lastName);

    // Find by firstName or LastName (either will do)
    DemoJpa findByLastNameOrFirstName(String lastName,String firstName);

    FirstName = firstName; firstName = firstName;
    //DemoJpa findByFirstName(String firstName);
    DemoJpa findByFirstNameIs(String firstName);

    // Age Specifies the value between Age and age2
    List<DemoJpa> findByAgeBetween(Integer age, Integer age2);

    // Data between values less than the specified age
    List<DemoJpa> findByAgeLessThan(Integer age);

    // Data less than or equal to the specified age value
    List<DemoJpa> findByAgeLessThanEqual(Integer age);

    // Data between values greater than the specified age
    List<DemoJpa> findByAgeGreaterThan(Integer age);

    // Data greater than or equal to the specified age value
    List<DemoJpa> findByAgeGreaterThanEqual(Integer age);

    
       
       
      
    List<DemoJpa> findByAgeAfter(Integer age);

    
       
       
      
    List<DemoJpa>  findByAgeBefore(Integer age);

    // Return data with an empty AGE field
    List<DemoJpa> findByAgeIsNull(a);

    // Return data with a non-empty AGE field
    List<DemoJpa> findByAgeNotNull(a);

    /** * This keyword is similar to database fuzzy query, * but I go to the official document to see it does not contain wildcards. * So I think it's like * DemoJpa findByFirstName(String firstName); *@seehttps://docs.spring.io/spring-data/jpa/docs/2.1.5.RELEASE/reference/html/#jpa.repositories * /
    DemoJpa findByFirstNameLike(String firstName);

    / / same as above
    List<DemoJpa> findByFirstNameNotLike(String firstName);

    Jpa returns multiple data sources with names starting with M.
    List<DemoJpa> findByFirstNameStartingWith(String firstName);

    // find a database with a different name specified (same as above)
    List<DemoJpa> findByFirstNameEndingWith(String firstName);

    // Find the data source that contains the specified data source (this differs from the above two fields in that it must enter complete data to query)
    List<DemoJpa> findByFirstNameContaining(String firstName);

    // Select all data sources by age and sort them by LastName
    List<DemoJpa> findByAgeOrderByLastName(Integer age);

    // Return all data that does not specify age
    List<DemoJpa> findByAgeNot(Integer age);

    // Find data that contains multiple returns of the specified age
    List<DemoJpa> findByAgeIn(List<Integer> age);

}
Copy the code

Unit tests < all passed >

@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class DemoJpaRepositoriesTest {

    @Autowired
    private DemoJpaRepositories repositories;

    @Test
    public void findByFirstNameAndLastName(a) {
        DemoJpa demoJpa = repositories.findByFirstNameAndLastName("May"."Eden");
        Assert.assertEquals(demoJpa.getFirstName(),"May");
    }

    @Test
    public void findByLastNameOrFirstName(a) {
        DemoJpa demoJpa = repositories.findByLastNameOrFirstName("Geordie"."Eden");
        Assert.assertNotEquals(demoJpa.getLastName(),"Eden");
    }

    @Test
    public void findByFirstNameIs(a) {
        DemoJpa demoJpa = repositories.findByFirstNameIs("amy");
        Assert.assertNull(demoJpa);
    }

    @Test
    public void findByAgeBetween(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeBetween(15.17);
        Assert.assertEquals(3,demoJpaList.size());
    }

    @Test
    public void findByAgeLessThan(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeLessThan(17);
        Assert.assertEquals(2,demoJpaList.size());
    }

    @Test
    public void findByAgeLessThanEqual(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeLessThanEqual(17);
        Assert.assertEquals(3,demoJpaList.size());
    }

    @Test
    public void findByAgeGreaterThan(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeGreaterThan(17);
        Assert.assertEquals(2,demoJpaList.size());
    }

    @Test
    public void findByAgeGreaterThanEqual(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeGreaterThanEqual(17);
        Assert.assertEquals(3,demoJpaList.size());
    }

    @Test
    public void findByAgeAfter(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeAfter(17);
        Assert.assertEquals(2,demoJpaList.size());
    }

    @Test
    public void findByAgeBefore(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeBefore(17);
        Assert.assertEquals(2,demoJpaList.size());
    }

    @Test
    public void findByAgeIsNull(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeIsNull();
        Assert.assertEquals(0,demoJpaList.size());
    }

    @Test
    public void findByAgeNotNull(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeNotNull();
        Assert.assertEquals(5,demoJpaList.size());
    }

    @Test
    public void findByFirstNameLike(a) {
        DemoJpa demoJpa = repositories.findByFirstNameLike("May");
        Assert.assertNotNull(demoJpa);
    }

    @Test
    public void findByFirstNameNotLike(a) {}@Test
    public void findByFirstNameStartingWith(a) {
        List<DemoJpa> demoJpaList = repositories.findByFirstNameStartingWith("May");
        Assert.assertEquals(2,demoJpaList.size());
    }

    @Test
    public void findByFirstNameEndingWith(a) {
        List<DemoJpa> demoJpaList = repositories.findByFirstNameEndingWith("Evil");
        Assert.assertEquals(0,demoJpaList.size());
    }

    @Test
    public void findByFirstNameContaining(a) {
        List<DemoJpa> demoJpaList = repositories.findByFirstNameContaining("hack");
        Assert.assertEquals(0,demoJpaList.size());
    }

    @Test
    public void findByAgeOrderByLastName(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeOrderByLastName(18);
        for (DemoJpa demoJpaL : demoJpaList){
            log.info("Data result"+demoJpaL.toString()); }}@Test
    public void findByAgeNot(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeNot(20);
        Assert.assertEquals(5,demoJpaList.size());
    }

    @Test
    public void findByAgeIn(a) {
        List<DemoJpa> demoJpaList = repositories.findByAgeIn(Arrays.asList(15.16));
        Assert.assertEquals(2,demoJpaList.size()); }}Copy the code

After the language

If this article is of any help to you, please help to read it. Your good looks are my motivation to keep writing.

In addition, after paying attention to send 1024 can receive free learning materials. Python, C++, Java, Linux, Go, front-end, algorithm sharing