JAVA code login registration function code is simple and easy to understand, copy and use

Log in

Create a public class named Result

package com.buba.po;

import lombok.Data;

@Data
public class Result<T>{
    private String msg;
    private String code;
    private T data;

    public Result(String msg, String code, T t) {
        this.msg = msg;
        this.code = code;
        this.data = t;
    }

    public static <T> Result success(T t) {
        return new Result<T>("success", "200",t);
    }
    public static <T> Result error(T t) {
        return new Result<T>("error", "400",t);
    }

    public Result(T t) {
        this.msg = "success";
        this.code = "200";
        this.data = t;
    }
}

Controller

 @GetMapping("Login")
    public Result login(@Param("Name") String Name, @Param("Password") String Password) {
        Integer res = userService.login(Name, Password);
        if (res != null && res > 0) {
            User user = new User();
            return Result.success(user);
//            return new Result<User>("登录成功","200",user);
        }
        return Result.error("用户名密码不正确,请重新输入");
    }

Service

    @Override
    public Integer login(String name, String password) {
        return userMapper.login(name, password);
    }

Mapper

Integer login(@Param("name") String name, @Param("password")String password);

 Mapper.xml

    <select id="login" parameterType="com.buba.po.User" resultType="int">
        select * from user where name = #{name} and password = #{password}
    </select>

register

Controller

    /**
     * register 注册
     * @param user
     * @return
     */

    @PostMapping("register")
    public String register(@RequestBody User user) {
        userService.register(user);
        return "注册成功";
    }

Service

    @Override
    public void register(User user) {
        userMapper.register(user);
    }

Mapper

void register(User user);

 Mapper.xml

 <insert id="register" parameterType="com.buba.po.User">
        insert into user(name,password,email,phone)
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name!=null and name!=''">
                #{name},
            </if>
            <if test="password!=null and password!=''">
                #{password},
            </if>
            <if test="email!=null and email!=''">
                #{email},
            </if>
            <if test="phone!=null and phone!=''">
                #{phone}
            </if>
        </trim>
    </insert>

Guess you like

Origin blog.csdn.net/qq_55629923/article/details/128074112