奶茶精Gaaa 2024-06-22 20:43 采纳率: 47.4%
浏览 3
已结题

解决springboot问题如下

解决springboot问题如下

img


代码如下:

package com.itheima.springbootquickstart;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
//扫描itheima下的包(扫描的范围)
@ComponentScan(basePackages = "com.itheima")
public class SpringbootQuickstartApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootQuickstartApplication.class, args);
    }

}


package com.itheima.springbootquickstart.controller;

import com.itheima.springbootquickstart.pojo.Result;
import com.itheima.springbootquickstart.pojo.User;
import com.itheima.springbootquickstart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")

public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/register")
    public Result register(String username,String password){
//        查询用户
        User u =userService.findByUserName(username);
        if(u==null){
//            没有占用
//            注册
            userService.register(username,password);
            return  Result.success();
        }else {
//            占用
            return  Result.error("用户名已被占用");
        }

    }
}


package com.itheima.springbootquickstart.mapper;

import com.itheima.springbootquickstart.pojo.User;
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}")
User findByUserName(String username);

//添加
    @Insert("insert into user(username,password,create_time,update_time)"+
    " values(#{username,#{password},now(),now())")
    void add(String username,String password);
}


package com.itheima.springbootquickstart.pojo;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

//统一响应结果
@NoArgsConstructor
@AllArgsConstructor
@Data
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);
    }
}


package com.itheima.springbootquickstart.pojo;


import lombok.Data;
import java.time.LocalDateTime;
@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;//更新时间
}


package com.itheima.springbootquickstart.service;

import com.itheima.springbootquickstart.pojo.User;

public interface UserService {
//根据用户名查询用户
    User findByUserName(String username);
//注册
    void register(String username, String password);
}


package com.itheima.springbootquickstart.service.impl;

import com.itheima.springbootquickstart.mapper.UserMapper;
import com.itheima.springbootquickstart.pojo.User;
import com.itheima.springbootquickstart.service.UserService;
import com.itheima.springbootquickstart.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
//注入一个Usermapper对象
    @Autowired
   private UserMapper userMapper;

    @Override
    public User findByUserName(String username) {
       User u= userMapper.findByUserName(username);
        return u;
    }

    @Override
    public void register(String username, String password) {
//加密处理
        String md5String= Md5Util.getMD5String(password);
//        添加
        userMapper.add(username,md5String);
    }
}


#数据源
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/big_event
    username: root
    password: 123456



package com.itheima.springbootquickstart.utils;


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);
    }

}


img


数据库代码:

-- 创建数据库
create database big_event;

-- 使用数据库
use big_event;

-- 用户表
create table user (
                      id int unsigned primary key auto_increment comment 'ID',
                      username varchar(20) not null unique comment '用户名',
                      password varchar(32)  comment '密码',
                      nickname varchar(10)  default '' comment '昵称',
                      email varchar(128) default '' comment '邮箱',
                      user_pic varchar(128) default '' comment '头像',
                      create_time datetime not null comment '创建时间',
                      update_time datetime not null comment '修改时间'
) comment '用户表';

-- 分类表
create table category(
                         id int unsigned primary key auto_increment comment 'ID',
                         category_name varchar(32) not null comment '分类名称',
                         category_alias varchar(32) not null comment '分类别名',
                         create_user int unsigned not null comment '创建人ID',
                         create_time datetime not null comment '创建时间',
                         update_time datetime not null comment '修改时间',
                         constraint fk_category_user foreign key (create_user) references user(id) -- 外键约束
);

-- 文章表
create table article(
                        id int unsigned primary key auto_increment comment 'ID',
                        title varchar(30) not null comment '文章标题',
                        content varchar(10000) not null comment '文章内容',
                        cover_img varchar(128) not null  comment '文章封面',
                        state varchar(3) default '草稿' comment '文章状态: 只能是[已发布] 或者 [草稿]',
                        category_id int unsigned comment '文章分类ID',
                        create_user int unsigned not null comment '创建人ID',
                        create_time datetime not null comment '创建时间',
                        update_time datetime not null comment '修改时间',
                        constraint fk_article_category foreign key (category_id) references category(id),-- 外键约束
                        constraint fk_article_user foreign key (create_user) references user(id) -- 外键约束
)


接口测试

img

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2024-06-22 20:43
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    根据您提供的信息,以下是解决您Spring Boot应用程序启动时遇到的依赖注入问题的步骤:

    1. 检查UserService接口的实现类UserServiceImpl是否被Spring容器管理

      • 确保UserServiceImpl类上有@Service注解,这告诉Spring这是一个服务组件,应该被自动检测并注册为Spring应用上下文的Bean。
    2. 确保组件扫描路径正确

      • 您的@SpringBootApplication注解中使用了@ComponentScan(basePackages = "com.itheima"),确保所有相关的类都在com.itheima包及其子包下。
    3. 检查数据库配置

      • 检查application.propertiesapplication.yml文件中的数据库配置,确保数据库连接信息(如URL、用户名、密码)是正确的,并且数据库服务正在运行。
    4. 检查UserMapper接口中的SQL语句

      • 您的@Insert注解中的SQL语句似乎有语法错误,正确的语句应该是:
        @Insert("insert into user(username, password, create_time, update_time) values(#{username}, #{password}, now(), now())")
        
      • 请注意,占位符#{}应该放在参数的前面,而不是中间。
    5. 检查Md5Util工具类

      • 确保Md5Util类可以正确生成MD5字符串。您可以单独测试这个类,确保没有错误。
    6. 检查UserController中的注册逻辑

      • 确保UserControllerregister方法可以正确调用UserServiceregister方法,并且注册逻辑是按照预期执行的。
    7. 查看异常日志详情

      • 根据您提供的异常日志,建议开启Spring Boot的debug模式,这将提供更详细的条件评估报告,帮助您定位问题。
    8. 接口测试问题

      • 根据Postman的截图,看起来您在尝试发送请求时遇到了ECONNREFUSED错误,这通常意味着应用程序没有在8080端口上运行,或者防火墙/网络配置阻止了连接。确保您的Spring Boot应用程序正在运行,并且端口没有被其他应用程序占用。
    9. 参考链接

    请按照上述步骤逐一检查和解决问题。如果您在执行这些步骤时遇到任何具体问题,可以继续提问,我会尽力帮助您。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月30日
  • 已采纳回答 6月22日
  • 创建了问题 6月22日

悬赏问题

  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥20 看图片)删除这个自动化录屏脚本就一直报错找不到脚本文件,如何解决?(相关搜索:bat文件)
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件