Implementation of simple registration function

How to achieve the simplest registration function, that is, to enter data in an HTML page, transfer it to the database, then get the data from the database, and if the data exists, complete the final response.
Beginners, many things are incomplete, the code is very crude, there is a very important mybatic configuration file not available.Writing this is just about taking notes.We look forward to your understanding.

Step 1: Use js code to get the value of the front end.

 <script>
 //Indicates that the method in parentheses is executed after the page has been loaded.The js code is executed in the browser.
        $(document).ready(function () {
 //Indicates that this command will be executed after a div click with id signup
        $("#signUp ").click(function () {
            //The value in the div with id user-name-label will be saved in the variable name
            var name = $("#user-name-label").val();
            //The value in the div representing id password-always-checkbox will be saved in the variable password.
            var password = $("#password-always-checkbox").val();
            //Here is a path where you can pass in parameters.Here the data has been passed into the database.
            var url = "/addUser?name=" + name + "&password=" +password ;
            //This is jQuery's get method.This method can be used to verify whether the data you just entered can be retrieved from the database.
            $.get(url, function (data, status) {
                alert("data: " + data + "\n state: " + status);
            });
        });
        });
    </script>

$.get() Details

The $.get() method uses an HTTP GET request to load data from the server.

Use format: $.get(url,[data],[callback])

Description: The url is the request address.

Data is a list of requested data (optional, or you can write parameters to be passed in a url).

Callback is a callback function that accepts two parameters, the first is the data returned by the server and the second is the state of the server, which is optional.

The format in which the server returns data is actually a string situation, not the json data format we want.So sometimes the json function is used to transform the data.

2. Create an entity class to facilitate later manipulation of the data.

package com.example.demo.model;

public class User { 
    private String name;
    private String password;
  
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password; }
        
   public void setPassword(String password) {
        this.password = password;
    }

3. Create a dao package and then a class, which is an interface through which you can manipulate the database.

package com.example.demo.dao;

import com.example.demo.model.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

//This interface class can write SQL statements, because this class is scanned into the database, so you can manipulate the database through SQL statements.
public interface UserMapper {
    @Insert({
            "insert into `user` (name,password)",
            "values (#{name,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR}",
            ")"
    })
    int insertByEntity(User record);

}

Of course, this class only makes sense when a configuration file exists.Here's how mybatics and springboot interact.These profiles are already written.In the early stages, you only need to know the following codes.

@Configuration//Indicates that this is a configuration class
@MapperScan(basePackages = {MybatisMysqlConfig.PACKAGE}, sqlSessionTemplateRef = "mysqlSqlSessionTemplate")//Indicates that all files in this class under the PACKAGE property path are scanned in.
public class MybatisMysqlConfig implements EnvironmentAware {

    protected static final String RESOURCES = "classpath*:mapper/mysql/*.xml";
    protected static final String PACKAGE = "com.example.demo.dao";
     /*This means that MapperScan will scan the files in this path into this configuration.Then you write the SQL statement by manipulating the files in that path.
     You can get things in the database.*/

4. Write a controller to establish contact with HTML files through the @GetMapping tag.

@Controller
public class HelloWorldController {
    @Autowired
    UserMapper userMapper;
    @GetMapping("/SignUp")
    public  String SignIn(){
        return "SignUp";
    }
    @GetMapping("/addUser")
    @ResponseBody//If you have this tag, the following method is executed in response to the return value of the method, not the page.
    public String addUser(@RequestParam(name = "name", required = true) String name,
                          @RequestParam(name = "password", required = true) String password){
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        int row = userMapper.insertByEntity(user);
        if (row > 0) {//If the number of rows returned affected is greater than 0, insertion into the database is successful
            return "login was successful";
        } else {
            return "login has failed";
        }
    }

5. Write HTML files.Write your own

Keywords: Database SQL JSON Apache

Added by tthmaz on Mon, 09 Sep 2019 05:17:20 +0300