SpringBoot仿制极简图床练习--后台

此练习为前后端分离开发,此处为后台程序

目录结构

image.png

实体类

  • Album类
package com.example.album.entity;

import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Data
public class Album {
    @GeneratedValue
    @Id
    private Integer id;
    private String albumCover;
    private String albumTitle;
    private String  albumDescription;
    private Integer likes;


    @OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.REMOVE)
    @JoinColumn(name = "picture_id")

    private List pictureList = new ArrayList<>();


    public Album() {
    }

    public Album(String albumCover, String albumTitle, String albumDescription, Integer likes) {
        this.albumCover = albumCover;
        this.albumTitle = albumTitle;
        this.albumDescription = albumDescription;
        this.likes = likes;
    }
}
  • PictureList类
package com.example.album.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
@Data

public class PictureList {
    @GeneratedValue
    @Id
    private Integer id;
    private String pictureTitle;
    private String pictureRoad;

    public PictureList(String pictureTitle, String pictureRoad) {
        this.pictureTitle = pictureTitle;
        this.pictureRoad = pictureRoad;
    }

    public PictureList() {
    }

}

DAO层

  • AlbumRepository接口
package com.example.album.dao;

import com.example.album.entity.Album;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface AlbumRepository  extends JpaRepository {
    @Query("FROM Album a ORDER BY a.likes DESC")
    List findHotAlbum();
}
  • AlbumRepository接口
package com.example.album.dao;

import com.example.album.entity.PictureList;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PictureListRepository extends JpaRepository {
}

Service层

package com.example.album.service;
import com.example.album.entity.Album;
import java.util.List;

public interface AlbumService {
    List getAll();

    Album get(int id);

    /**
     * 按相册喜欢数获取热门相册
     * @return
     */
    List getHotAlbums();
}
  • ServiceImpl
package com.example.album.service.Impl;

import com.example.album.dao.AlbumRepository;
import com.example.album.entity.Album;
import com.example.album.service.AlbumService;
import org.springframework.stereotype.Service;

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

@Service
public class AlbumServiceImpl implements AlbumService {
    @Resource
    private AlbumRepository albumRepository;
    @Override
    public List getAll() {
        return albumRepository.findAll();
    }

    @Override
    public Album get(int id) {
        return albumRepository.findById(id).get();
    }

    @Override
    public List getHotAlbums() {
        return albumRepository.findHotAlbum();
    }
}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/springboot_test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##连接池中最大的活跃连接数
spring.datasource.tomcat.max-active=20  
##连接池中最大、最小的空闲连接数
spring.datasoure.max-idle=8
spring.datasoure.min-idle=8
##初始化连接数
spring.datasoure.initial=10
spring.jpa.database=mysql
# 显示SQL语句
spring.jpa.show-sql=true
##指定DDL mode (none, validate, update, create, create-drop)
spring.jpa.properties.hibernate.hbm2ddl.auto=update
swagger.enabled=true
swagger.title=spring-data-jpa module
swagger.description=Starter for swagger 2.x
swagger.license=Apache License, Version 2.0
swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger
swagger.contact.name=RanRan Tao
swagger.contact.url=https://www.jianshu.com/u/822585e5c69a
[email protected]
swagger.base-package=com.example.book.controller
swagger.base-path=/**
swagger.exclude-path=/error, /ops/**

你可能感兴趣的:(SpringBoot仿制极简图床练习--后台)