Modify user information changeinfo

Time:2022-5-5

When judging the persistence layer:

Problem: there is such a problem when modifying user information. For example: the user’s email is not required. It was not empty originally. At this time, the user deletes the mailbox information and submits it. At this time, if it is not empty to judge whether it needs to be modified, the system will think that the mailbox information does not need to be modified. Therefore, this judgment method will confuse the two situations of “there was information but the user set it to null” and “there was information but the user did not make changes”.

resolvent:

1) There are generally two methods to update. One is to update the object entity as a whole. This update is overwritten each time, so you should read out the object record first, and then update all fields back after modification. The other is to update the specified fields, which is spell SQL. Most of the time, it is recommended to use the method of updating according to objects. Although this method needs to overwrite all fields every time even if only one field is updated, it is simple to write code.

2) This problem cannot be solved in the Dao of persistence layer. Firstly, Dao should not consider the problem of front-end business. 2 Then this problem needs to be solved by the business layer. Then you actually have a problem here, that is, it is not standardized, that is, it is completely unclear whether the front end wants to update this empty field or not. That is, if your business layer provides a general method, but there is no clear specification, it is impossible to judge, so a specification is needed from the front end to the business layer, for example, you need to set this field blank, You need to pass special characters such as – 1, and then the business layer can make a common thing.

In the following, the system modification information function is directly realized by the full coverage method

1) Persistent layer

Interface:

User getUserByUid(Integer uid);

    int updateInfoByUid(User user);

xml:

select * from store.t_user where uid=#{uid};
    

    
        update store.t_user
        set phone = #{phone},email=#{email},gender=#{gender},modified_user=#{modifiedUser},modified_time=#{modifiedTime}
        where uid=#{uid};

2) Business layer

Interface:

//Get user information according to UID
    public User getUserByUid(Integer uid);

    //Modify user personal information
    public void changeUserInfo(Integer uid,String username,User user);// The user object here encapsulates the data modified by the user

Implementation class:

//It is mainly used to return the basic information to the front-end user: user name, email, gender and phone
    @Override
    public User getUserByUid(Integer uid) {
        User user = userMapper.getUserByUid(uid);
        if (user == null){
            Throw new usernotfoundexception ("user data does not exist!");
        }
        if (user.getIsDelete().equals(1)){
            Throw new usernotfoundexception ("the user has logged off and cannot return user information!");
        }

        System. out. Println ("user found by business layer:" + user. Tostring());

        //In order to reduce the amount of data and improve the transmission performance, re create a user object and put the required field encapsulation back to the front end
        User user1=new User();
        user1.setUsername(user.getUsername());
        user1.setPhone(user.getPhone());
        user1.setEmail(user.getEmail());
        user1.setGender(user.getGender());


        return user1;
    }

    @Override
    public void changeUserInfo(Integer uid, String username, User user) {
        //Find the user through uid to determine whether the user exists or logs off
        User user1 = userMapper.getUserByUid(uid);
        if (user1 == null){
            Throw new usernotfoundexception ("user data does not exist!");
        }
        if (user1.getIsDelete().equals(1)){
            Throw new usernotfoundexception ("the user has logged off and cannot return user information!");
        }

        //Complete the data in the parameter user
        user.setUid(uid);
        user.setModifiedUser(username);
        user.setModifiedTime(new Date());

        int rows = userMapper.updateInfoByUid(user);
        if (rows != 1) {
            Throw new updateexception ("unknown error occurred when updating user data, please contact the system administrator");
        }

    }

And the preparation of some exceptions in the ex package: updateexception, etc

3) Control layer

@RequestMapping("/getUser")
    public JsonResult getUserById(HttpSession session) {
        Integer uid = (Integer) session.getAttribute("uid");
        User user = userService.getUserByUid(uid);
        return new JsonResult(OK,user);

    }

    @RequestMapping("/changeUInfo")
    public JsonResult changeUserInfo(User user, HttpSession httpSession){
        Integer uid = getUidFromSession(httpSession);
        String username = getUsernameFromSession(httpSession);

        userService.changeUserInfo(uid,username,user);

        return new JsonResult(OK);

    }

And the preparation of some exceptions in basecontroller

4) Front end

Form: abbreviated version

male
      female

JS code:

$(document). Ready (function() {// this function is triggered when the web page is loaded
			$.ajax({
				url:"/users/getUser",
				type:"get",
				dataType:"json",
				success:function (json) {
					if (json.state==200){

							console.log(json);
							// console.log("phone=" + json.data.phone);
							// console.log("email=" + json.data.email);
							// console.log("gender=" + json.data.gender);
							$("#username").val(json.data.username);
							$("#phone").val(json.data.phone);
							$("#email").val(json.data.email);
							let radio = json.data.gender == 0 ? $("#gender-female") :
									$("#gender-male");
							radio.prop("checked", "checked");


						}else{
						Alert ("failed to get current user data!"+ json. message);
					}
				}
			})
		});

		$("#btn-change-info").click(function() {
			$.ajax({
				url: "/users/changeUInfo",
				type: "POST",
				data: $("#form-change-info").serialize(),
				dataType: "json",
				success: function(json) {
					if (json.state == 200) {
						Alert ("modification succeeded!");
						location.href = "login.html";
					} else {
						Alert ("modification failed!"+ json. message);
					}
				},
				error: function(xhr) {
					Alert ("your login information has expired, please login again! HTTP response code:" + XHR. Status);
					location.href = "login.html";
				}
			});
		});

Reference link:https://bbs.csdn.net/topics/391065737

Recommended Today

IOS data storage

IOS data storage https://www.jianshu.com/p/19fdbf81a086 How to store data 1. Documents 3、NSUserDefaults 2. Database file 1. Sandbox 2、Plist 3. Nskeyedarchiver archiving / nskeyedunarchiver de archiving NSUserDefaults database 1、SQLite3 2、FMDB 3、Core Data file Sandbox The data stored in IOS localization is saved in a sandbox. Documents: iTunes will back up the directory. It is generally used to […]