Spring datajpa series 02 - add, delete, modify and query (based on springboot2.0.3+MySQL5.7)

Keywords: Database Spring

Reprint please indicate Original: https://me.csdn.net/jxysgzs

1, Necessary foreword

We learned about the basic use of spring datajpa through the previous article. In this article, we will explain the basic addition, deletion, modification and query. After all, CURD operation is essential for every persistence layer framework.

In fact, we have talked about the increase of spring datajpa in the last article. I will not go into details here. The other ones are similar to the new ones, which encapsulate how to use them.

This tutorial continues to use the small Dome written in the previous article

2, Modify the database first

Add two lines of content to the database. In terms of our subsequent CURD operations

3, Modification of CURD

As I mentioned in the previous tutorial, the usersJpaDao.save() method has the ability of modification. Let's try it.

1. Modify startup class: add a modification interface

@SpringBootApplication
@RestController
public class SpringdatajpaApplication {
    @Autowired
    private UsersJpaDao usersJpaDao;

    public static void main(String[] args) {
        SpringApplication.run(SpringdatajpaApplication.class, args);
    }

    /**
     * Newly added
     * @param userName
     * @param userPwd
     * @return
     */
    @GetMapping("/insertUser")
    public Users insertUser(@RequestParam("userName")String userName,@RequestParam("userPwd")String userPwd){
        Users users=new Users();
        users.setUserName(userName);
        users.setUserPwd(userPwd);
        return this.usersJpaDao.save(users);
    }

    /**
     * modify
     * @param userId
     * @param userName
     * @param userPwd
     * @return
     */
    @GetMapping("/updateUser")
    public Users updateUser(@RequestParam("userId")Integer userId,@RequestParam("userName")String userName,@RequestParam("userPwd")String userPwd){
        Users users=new Users();
        users.setUserName(userName);
        users.setUserPwd(userPwd);
        users.setUserId(userId);
        return this.usersJpaDao.save(users);
    }

}

Remember that objects in this interface need to add a primary key to indicate which data to modify

2. test

Start the project, and enter it in the browser after the completion of startup
http://localhost:8080/updateUser?userName=zhaoliu&userPwd=666666&userId=1


See the database after the return information as shown in the figure above appears


Here, Zhang San is changed to Zhao Liu.

3. summary

Both modification and addition can be done by using the same method. There are many other operations, such as

saveAll() inserts or modifies a set of data. Other users can view the API documents by themselves!

4, CURD - delete

There are also many encapsulated methods for deleting operations, including deleting by primary key, deleting all, deleting a group of data, etc. Let's take the deletion of primary key as an example, and the others are the same.

1. Modify startup class: add a delete interface

@SpringBootApplication
@RestController
public class SpringdatajpaApplication {
    @Autowired
    private UsersJpaDao usersJpaDao;

    public static void main(String[] args) {
        SpringApplication.run(SpringdatajpaApplication.class, args);
    }

    /**
     * Newly added
     * @param userName
     * @param userPwd
     * @return
     */
    @GetMapping("/insertUser")
    public Users insertUser(@RequestParam("userName")String userName,@RequestParam("userPwd")String userPwd){
        Users users=new Users();
        users.setUserName(userName);
        users.setUserPwd(userPwd);
        return this.usersJpaDao.save(users);
    }

    /**
     * modify
     * @param userId
     * @param userName
     * @param userPwd
     * @return
     */
    @GetMapping("/updateUser")
    public Users updateUser(@RequestParam("userId")Integer userId,@RequestParam("userName")String userName,@RequestParam("userPwd")String userPwd){
        Users users=new Users();
        users.setUserName(userName);
        users.setUserPwd(userPwd);
        users.setUserId(userId);
        return this.usersJpaDao.save(users);
    }

    /**
     * delete
     * @param userId
     * @return
     */
    @GetMapping("/deleteUser")
    public String  deleteUser(@RequestParam("userId")Integer userId){
        this.usersJpaDao.deleteById(userId);
        return "Delete successful";
    }
}

It is worth mentioning that there is no return value for a method, so it is necessary to use other means to determine whether the deletion is successful, or you can customize a deletion method, which will be explained in the following tutorials.

Here's a specific note: the usersJpaDao.deleteById() method seems to be deleted with an ID field, which makes people feel like where id = {ID}. In fact, it's not. It means to delete according to the primary key. Any field name can be used. Also, the primary key type here can be seen from the source code, which is determined by the type defined in your interface.

The following two figures show the selection of types in the source code:


The following figure shows the type you entered when creating and inheriting JPA repository

2. test

Launch project, browser input:

http://localhost:8080/deleteUser?userId=3

View the data and find that the deletion is successful

Our king five is gone!

Five, summary

You may want to ask, what about the query??? As we all know, query is the most complex existence in CURD, so I'm going to explain it to you in several pieces. The purpose is to make your thoughts clearer and your hands faster, or that sentence. If you don't understand it, you can leave a message or chat privately.

Published 20 original articles, won praise and 462 visitors
Private letter follow

Posted by melmoth on Wed, 29 Jan 2020 04:22:54 -0800