非常简易的SpringBoot后台项目

非常简易的SpringBoot后台项目

1. 创建项目

使用IDEA创建 Spring项目,或在 https://start.spring.io/ 、 https://start.aliyun.com/bootstrap.html上生成后端初始化项目
 springboot 2.5.6 maven 选择插件:

spring boot devtools,
lombok,
spring configuration processor,
spring web,
spring data jpa ,
rest repositories,
mysql deriver
及其它需要的插件即可,初始化项目代码


2. 在MySQL中新建表

create DATABASE library;
CREATE TABLE `user` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`username` datetime DEFAULT NULL COMMENT '用户名',
`password` datetime DEFAULT NULL COMMENT '密码',
PRIMARY KEY (`id`)
) COMMENT='用户表'

  

3. 创建package entity

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Data
@Entity
@Table(name = "user")
public class User {

  @Id
  private int id;
  private String username;
  private String password;
  private String borrowNo;
}

 

4. 创建package repository

import com.library.backend.entity.Manager;
import com.library.backend.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import javax.transaction.Transactional;
import java.util.List;

public interface UserRepository extends JpaRepository<User, String> {

  User findByUsername(String name);

  @Transactional
  void deleteById(int id);

  List<User> findAllByUsernameContaining(String name);
}


5. 创建package controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.library.backend.utils.Constant.*;

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

  @Autowired
  private UserRepository userRepository;

  @GetMapping("/list")
  public Result managerLogin(User user) {
    if (user.getUsername() != null && !"".equals(user.getUsername())) {
      List<User> users = userRepository.findAllByUsernameContaining(user.getUsername());
      return new Result(SUCCESS_CODE, "", users);
    } else {
      List<User> users = userRepository.findAll();
      return new Result(SUCCESS_CODE, "", users);
    }
  }

  @PostMapping("/add")
  public Result add(@RequestBody User user) {
    try {
      User user1 = userRepository.findByUsername(user.getUsername());
      if (user1 != null) {
        return new Result(NAME_REPEAT, "名称重复");
      }
      user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
      userRepository.save(user);
      return new Result(SUCCESS_CODE, "新增成功", user);
     } catch (Exception e) {
      return new Result(FAILE_CODE, e.toString(), user);
    }
  }
  @PostMapping("/update")
  public Result update(@RequestBody User user) {
    try {
      userRepository.save(user);
      return new Result(SUCCESS_CODE, "修改成功", user);
    } catch (Exception e) {
      return new Result(FAILE_CODE, e.toString(), user);
    }
  }

  @DeleteMapping("/delete")
  public Result delete(@RequestBody User user) {
    try {
      userRepository.deleteById(user.getId());
      return new Result(SUCCESS_CODE, "删除成功", user);
    } catch (Exception e) {
      return new Result(FAILE_CODE, e.toString(), user);
    }
  }
}

  

6. 创建package config(跨域看需要配,测试时全开,正式时可设定By域名或IP开放)

 

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {

  @Bean
  public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.addAllowedHeader("*");
    configuration.addAllowedMethod("*");
    configuration.addAllowedOrigin("*");
    urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", configuration);
    return new CorsFilter(urlBasedCorsConfigurationSource);
  }
}

  

7. Application类(一般会自动生成,看创建项目的方式)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class XXXApplication {

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

}

  

8. application.properties配置

server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=123456
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://localhost:3306/library?useSSL=false&useUnicode=true&serverTimezone=Asia/Shanghai

  

除上述之外的完整的代码地址:
https://gitee.com/zehongzhyuan/test-l-i-b-r-a-r-y/tree/dev

上一篇:springboot集成kafka


下一篇:如何获取微信用户openid