|
楼主 |
发表于 2024-2-9 03:19:27
|
显示全部楼层
我把更完整的代码发给你看看吧
/bigEventB/src/main/java/com/ms/bigEventB/controller/UserController.java中代码如下
package com.ms.bigEventB.controller;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ms.bigEventB.pojo.Result;
import com.ms.bigEventB.pojo.User;
import com.ms.bigEventB.service.UserService;
import com.ms.bigEventB.util.Md5Util;
import jakarta.validation.constraints.Pattern;
@Validated
@Mapper
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public Result register(@Pattern(regexp = "^\\S{5,16}$")String username, @Pattern(regexp = "^\\S{5,16}$")String password) {
User u = userService.findByUserName(username);
if(u == null) {
userService.register(username, password);
return Result.success();
}else {
return Result.error("用户名已被占用");
}
}
@PostMapping("/login")
public Result login(@Pattern(regexp = "^\\S{5,16}$")String username, @Pattern(regexp = "^\\S{5,16}$")String password) {
User loginUser = userService.findByUserName(username);
if (loginUser == null) {
return Result.error("用户名错误");
}
if (Md5Util.getMD5String(password) == loginUser.getPassword()) {
return Result.success("jwt token");
}
return Result.error("密码错误");
}
}
/bigEventB/src/main/java/com/ms/bigEventB/exception/GlobalExcetionHandler.java中代码如下
package com.ms.bigEventB.exception;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.ms.bigEventB.pojo.Result;
@RestControllerAdvice
public class GlobalExcetionHandler {
@ExceptionHandler(Exception.class)
public Result handlepException(Exception e) {
e.printStackTrace();
return Result.error(StringUtils.hasLength(e.getMessage())? e.getMessage(): "操作失败");
}
}
/bigEventB/src/main/java/com/ms/bigEventB/mapper/UserMapper.java中代码如下
package com.ms.bigEventB.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserMapper {
@Select("select * from user where username = #{username}")
void findByUserName(String username);
@Insert("insert into user(username, password, create_time, update_time) value (#{username}, #{password}, now(), now())")
void add(String username, String password);
}
/bigEventB/src/main/java/com/ms/bigEventB/pojo/Result.java中代码如下
package com.ms.bigEventB.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//统一响应结果
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
private Integer code;//业务状态码 0-成功 1-失败
private String message;//提示信息
private T data;//响应数据
//快速返回操作成功响应结果(带响应数据)
public static <E> Result<E> success(E data) {
return new Result<>(0, "操作成功", data);
}
//快速返回操作成功响应结果
public static Result success() {
return new Result(0, "操作成功", null);
}
public static Result error(String message) {
return new Result(1, message, null);
}
}
/bigEventB/src/main/java/com/ms/bigEventB/pojo/User.java中代码如下
package com.ms.bigEventB.pojo;
import java.time.LocalDateTime;
import lombok.Data;
@Data
public class User {
private Integer id;//主键ID
private String username;//用户名
private String password;//密码
private String nickname;//昵称
private String email;//邮箱
private String userPic;//用户头像地址
private LocalDateTime createTime;//创建时间
private LocalDateTime updateTime;//更新时间
}
/bigEventB/src/main/java/com/ms/bigEventB/service/impl/UserServiceImpl.java中代码如下
package com.ms.bigEventB.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ms.bigEventB.mapper.UserMapper;
import com.ms.bigEventB.pojo.User;
import com.ms.bigEventB.service.UserService;
import com.ms.bigEventB.util.Md5Util;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findByUserName(String username) {
userMapper.findByUserName(username);
return null;
}
@Override
public void register(String username, String password) {
String md5Util = Md5Util.getMD5String(password);
userMapper.add(username, md5Util);
}
}
/bigEventB/src/main/java/com/ms/bigEventB/service/UserService.java中代码如下
package com.ms.bigEventB.service;
import com.ms.bigEventB.pojo.User;
public interface UserService {
User findByUserName(String username);
void register(String username, String password);
}
/bigEventB/src/main/java/com/ms/bigEventB/util/Md5Util.java中代码如下
package com.ms.bigEventB.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Md5Util {
/**
* 默认的密码字符串组合,用来将字节转换成 16 进制表示的字符,apache校验下载的文件的正确性用的就是默认的这个组合
*/
protected static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
protected static MessageDigest messagedigest = null;
static {
try {
messagedigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException nsaex) {
System.err.println(Md5Util.class.getName() + "初始化失败,MessageDigest不支持MD5Util。");
nsaex.printStackTrace();
}
}
/**
* 生成字符串的md5校验值
*
* @param s
* @return
*/
public static String getMD5String(String s) {
return getMD5String(s.getBytes());
}
/**
* 判断字符串的md5校验码是否与一个已知的md5码相匹配
*
* @param password 要校验的字符串
* @param md5PwdStr 已知的md5校验码
* @return
*/
public static boolean checkPassword(String password, String md5PwdStr) {
String s = getMD5String(password);
return s.equals(md5PwdStr);
}
public static String getMD5String(byte[] bytes) {
messagedigest.update(bytes);
return bufferToHex(messagedigest.digest());
}
private static String bufferToHex(byte bytes[]) {
return bufferToHex(bytes, 0, bytes.length);
}
private static String bufferToHex(byte bytes[], int m, int n) {
StringBuffer stringbuffer = new StringBuffer(2 * n);
int k = m + n;
for (int l = m; l < k; l++) {
appendHexPair(bytes[l], stringbuffer);
}
return stringbuffer.toString();
}
private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
char c0 = hexDigits[(bt & 0xf0) >> 4];// 取字节中高 4 位的数字转换, >>>
// 为逻辑右移,将符号位一起右移,此处未发现两种符号有何不同
char c1 = hexDigits[bt & 0xf];// 取字节中低 4 位的数字转换
stringbuffer.append(c0);
stringbuffer.append(c1);
}
}
/bigEventB/src/main/java/com/ms/bigEventB/BigEventBApplication.java中代码如下
package com.ms.bigEventB;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BigEventBApplication
{
public static void main( String[] args )
{
SpringApplication.run(BigEventBApplication.class, args);
}
}
/bigEventB/src/main/resources/application.yml中代码如下
spring:
datasource:
drive-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/big_event
username: root
password: root
/bigEventB/pom.xml中代码如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
</parent>
<groupId>com.ms</groupId>
<artifactId>bigEventB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>bigEventB</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
</project>
点击用springboot运行 不会报错 但是用postman发送登录或者注册请求就会出现错误 报错信息如下
at java.base/java.lang.Thread.run(Thread.java:842)
Caused by: org.apache.ibatis.executor.ExecutorException: No constructor found in void matching [java.lang.Long, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.time.LocalDateTime, java.time.LocalDateTime]
你能看出问题出在哪里吗?
|
|