IDEA 创建 SpringBoot项目 详细教程

1.安装JDK

2.安装IDEA

步骤

1.打开IDEA点击new Project

在这里插入图片描述

2.选择“Spring Initializr”,点击next;(jdk1.8默认即可)

在这里插入图片描述

3.src->main->java下包名会是:com->example-> project;点击next;

在这里插入图片描述

4.对初始化项目架包的勾选

在这里插入图片描述

5.选择项目路径,点击finish;打开新的窗口;

在这里插入图片描述

6.等待加载,进行maven的设置

插入maven的配置

  1. 下载maven,并且解压
    在这里插入图片描述
  2. 设置setting.xml 主要设置存放的仓库和引用阿里云
    D:\maven\apache-maven-3.6.1\conf下面的settings.xml
    在这里插入图片描述

    alimaven
    aliyun maven
    http://maven.aliyun.com/nexus/content/groups/public/
    central

    在这里插入图片描述
    7.Idea 点击设置(扳手图标)->maven settings
    (1)选择本地Maven路径;(2)勾选配置文件后边的选项,然后修改为本地Maven的配置文件,它会根据配置文件直接找到本地仓库位置
    在这里插入图片描述

等待新项目的生成…

生成以后创建对应的包(对应代码的存放位置)
在这里插入图片描述
至此springboot的创建基本结束

下面就是要进行一些配置

application.yml的配置(端口号和连接数据库)

server:
  #配置端口号
  port: 8086
spring:
  #通用的数据源配置
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://数据库地址:端口号/数据库名称?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
    username: ****
    password: *****
    platform: mysql
  jpa:
    #配置在日志中打印出执行的 SQL 语句信息。
    show-sql: true
    hibernate:
      #配置指明在程序启动的时候要删除并且创建实体类对应的表
      ddl-auto: update

启动类的配置
在这里插入图片描述
JpaAuditorAware类,用于给@CreateBy 赋值

package com.example.project.common;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @Configuration 系统启动后自动运行该类
 * 用于给 @CreatedBy,@LastModifiedBy赋值
 */
@Configuration
public class JpaAuditorAware implements AuditorAware<String> {

  @Resource
  private HttpServletRequest request;


  @Override
  public Optional<String> getCurrentAuditor() {

    return Optional.of("1213");
  }
}

返回数据结果的封装



package com.example.project.common;

import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * 返回结果的封装
 */
@Component
@Data
public class JsonResult {

    /**
     * 状态码
     */
    private  int status;
    /**
     * 返回数据
     */
    private  Object data;
    /**
     * 返回消息
     */
    private String message;

    /**
     * 成功消息的封装
     * @param data
     * @param message
     * @return
     */
    public static JsonResult success(Object data,String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatus(Content.SUCCESS_STATUS);
        jsonResult.setData(data);
        jsonResult.setMessage(message);
        return jsonResult;
    }

    /**
     * 失败消息的封装
     * @param data
     * @param message
     * @return
     */
    public static JsonResult error(Object data,String message){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setStatus(Content.ERROR_STATUS);
        jsonResult.setData(data);
        jsonResult.setMessage(message);
        return jsonResult;
    }

}

常量类的设置

package com.example.project.common;

public class Content {
    public static final int SUCCESS_STATUS=200;

    public static final int ERROR_STATUS=500;


    //设置过期时间
    public static final long EXPIRE_DATE=30*60*100000;
    //token秘钥
    public static final String TOKEN_SECRET = "ZCfasfhuaUUHufguGuwu2020BQWE";
}

Controller类

package com.example.project.controller;


import com.example.project.common.JsonResult;
import com.example.project.service.StudentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 学生信息
 */

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @GetMapping("/findAllStudent")
    public JsonResult findAllStudent(){
        return  studentService.findAllStudent();
    }
}

Service接口

package com.example.project.service;

import com.example.project.common.JsonResult;


public interface StudentService {
     JsonResult findAllStudent();
}

ServiceImpl

package com.example.project.service.impl;

import com.example.project.common.JsonResult;
import com.example.project.entity.StudentEntity;
import com.example.project.repository.StudentRepository;
import com.example.project.service.StudentService;
import com.example.project.vo.StudentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentRepository studentRepository;


    /**
     * 查找所有学生信息
     * @return
     */
    @Override
    public JsonResult findAllStudent() {
        List<StudentEntity> allStudent = studentRepository.findAll();
        List<StudentVo> studentVo=new ArrayList<>();
        for(StudentEntity entity :allStudent){
            StudentVo vo=new StudentVo();
            BeanUtils.copyProperties(entity,vo);
            studentVo.add(vo);
        }
        return JsonResult.success(studentVo,"111");
    }
}

Entity类

package com.example.project.entity;


import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;


import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public class BaseEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)//用于主键的自增长
    private Integer id;


    /**
     * @CreatedBy 创建者注解需要我们自己给其设置
     */
    @CreatedBy
    @Column(name = "create_user")
    private String createUser;

    /**
     * @LastModifiedBy 修改者注解需要我们自己给其设置
     */
    @LastModifiedBy
    @Column(name = "update_user")
    private String updateUser;

    /**
     * @CreatedDate 创建时间 JPA自动给其设置
     */
    @CreatedDate
    @Column(name = "create_date")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createDate;

    /**
     * @LastModifiedDate 创建时间 JPA自动给其设置
     */
    @LastModifiedDate
    @Column(name = "update_date")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateDate;



}

StudentEntity类

package com.example.project.entity;

import lombok.Data;

import javax.persistence.*;

/**
 * 学生实体类
 * @Table(name="student") JPA自动创建student表
 */
@Data
@Table(name="student")
@Entity
public class StudentEntity extends BaseEntity {

    /**
     * 姓名
     */
    @Column(name="name")
    private String name;
    /**
     * 年龄
     */
    @Column(name="age")
    private Long age;

}


StudentRepository类

package com.example.project.repository;

import com.example.project.entity.StudentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface StudentRepository extends JpaRepository<StudentEntity,Integer> {

}

启动ProjectApplication
在这里插入图片描述
出现如下是启动成功
在这里插入图片描述

数据库的表
在这里插入图片描述
地址栏输入连接测试
在这里插入图片描述

至此完成所有

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值