4.2

package com.example.blog_app2.service;

import com.example.blog_app2.entity.User;
import com.example.blog_app2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {

@Autowired
private UserMapper userMapper;

@Transactional
public String register(User user) {
    // 校验用户名和密码非空
    if (user.getUsername() == null || user.getUsername().trim().isEmpty()) {
        throw new RuntimeException("用户名不能为空");
    }
    if (user.getPassword() == null || user.getPassword().trim().isEmpty()) {
        throw new RuntimeException("密码不能为空");
    }

    // 校验学号唯一性
    int count = userMapper.countByStudentId(user.getStudentId());
    if (count > 0) {
        throw new RuntimeException("学号已存在");
    }

    // 注册用户
    userMapper.register(user);
    return "注册成功";
}

public String login(String username, String password) {
    User user = userMapper.login(username, password);
    if (user == null) {
        throw new RuntimeException("用户名或密码错误");
    }
    return "登录成功";
}

}
package com.example.blog_app2.controller;

import com.example.blog_app2.entity.User;
import com.example.blog_app2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class UserController {

@Autowired
private UserService userService;

@PostMapping("/register")
public ResponseEntity<?> register(@RequestBody User user)  {
    try {
        String result = userService.register(user);
        return ResponseEntity.ok(result);
    } catch (RuntimeException e) {
        return ResponseEntity.badRequest().body(e.getMessage());
    }
}

@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody User user) {
    try {
        String result = userService.login(user.getUsername(), user.getPassword());
        return ResponseEntity.ok(result);
    } catch (RuntimeException e) {
        return ResponseEntity.badRequest().body(e.getMessage());
    }
}

}
package com.example.blog_app2.mapper;

import com.example.blog_app2.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {
@Insert("INSERT INTO blog_user(studentId, username, password, phone, className) " +
"VALUES(#{studentId}, #{username}, #{password}, #{phone}, #{className})")
void register(User user);

@Select("SELECT * FROM blog_user WHERE username = #{username} AND password = #{password}")
User login(@Param("username") String username, @Param("password") String password);

@Select("SELECT COUNT(*) FROM blog_user WHERE studentId = #{studentId}")
int countByStudentId(@Param("studentId") String studentId);

}
package com.example.blog_app2.entity;

import lombok.Data;

@Data
public class User {
private String studentId; // 学号(需校验唯一性)
private String username; // 姓名
private String password;
private String phone; // 手机号
private String className; // 班级

public User() {
}

public User(String studentId, String username, String password, String phone, String className) {
    this.studentId = studentId;
    this.username = username;
    this.password = password;
    this.phone = phone;
    this.className = className;
}

public String getStudentId() {
    return studentId;
}

public void setStudentId(String studentId) {
    this.studentId = studentId;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getClassName() {
    return className;
}

public void setClassName(String className) {
    this.className = className;
}

@Override
public String toString() {
    return "User{" +
            "studentId='" + studentId + '\'' +
            ", username='" + username + '\'' +
            ", password='" + password + '\'' +
            ", phone='" + phone + '\'' +
            ", className='" + className + '\'' +
            '}';
}

}

posted @ 2025-04-02 21:52  李蕊lr  阅读(6)  评论(0)    收藏  举报