1. 程式人生 > 實用技巧 >springboot-ssm框架前後端訪問網易雲音樂,後端部署專案原理案例,完成資料庫瀏覽器訪問接收資料

springboot-ssm框架前後端訪問網易雲音樂,後端部署專案原理案例,完成資料庫瀏覽器訪問接收資料

建立專案

建立並配置application.yml檔案

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/music?serverTimezone=UTC
    username: root
    password: 123456

查詢所有的歌單

Dao層

ShowDaoSongSheet

@Mapper
public interface ShowDaoSongSheet {
    //查詢歌單
    @Select("select * from song_sheet")
    public List<SongSheet> findAll();
}

entity

SongSheet

接收查詢到資料再放到List<SongSheet>中

@Data
public class SongSheet {
private Integer id;
private String cover_url;
private String sheet_type;
private String desc;
}

util,封裝查詢的結果

ResultMusic

@Data
//封裝結果
public class ResultMusic<T> {
    private int code;
    private String msg;
    private T data;
}

ShowServiceSongSheet

public interface ShowServiceSongSheet {
    //查詢歌單
    public ResultMusic<List<SongSheet>> findAll();
}

ShowSongSheetImp

service判斷是否有資料,再做出反應

@Service
public class ShowSongSheetImp implements ShowServiceSongSheet {
    //注入dao
    @Autowired
    private ShowDaoSongSheet dao;
    @Override
    
public ResultMusic<List<SongSheet>> findAll() { List<SongSheet> all = dao.findAll(); ResultMusic<List<SongSheet>> result = new ResultMusic<List<SongSheet>>(); if(all!=null){ result.setCode(1); result.setMsg("查詢歌單成功"); result.setData(all); return result; } result.setCode(2); result.setMsg("查詢歌單失敗"); return result; } }


ShowControllerSongSheet

@RestController
public class ShowControllerSongSheet {
    @Autowired
    private ShowServiceSongSheet service;
    @RequestMapping("/song_sheet")
    public ResultMusic<List<SongSheet>> showAll(){
            return service.findAll();
    }
}

查詢歌單裡的所有歌曲

SongsController


@RestController public class SongsController { @Autowired private SongsService songsService; @RequestMapping("/songs") public ResultMusic<List<Songs>> showSongs(String sheet_type_id){ return songsService.findById(sheet_type_id); } }

entity

Songs

@Data
public class Songs {
    private Integer id;
    private String sheet_type_id;
    private String name;
    private String song_url;
    private String img_url;
    private String author;
    private String author_desc;
    private String song_tag;
}

SongsService

public interface SongsService {
    //根據歌單id查詢歌曲
    public ResultMusic<List<Songs>> findById(String sheet_type_id);
}

SongsServiceImp

@Service
public class SongsServiceImp implements SongsService {
    @Autowired
    private SongsDao dao;
    @Override
    public ResultMusic<List<Songs>> findById(String sheet_type_id) {
        List<Songs> songs = dao.findById(sheet_type_id);
        ResultMusic<List<Songs>> result = new ResultMusic<>();
        if(songs!=null){
            result.setCode(1);
            result.setMsg("歌曲查詢成功");
            result.setData(songs);
            return result;
        }
        result.setCode(2);
        result.setMsg("歌曲查詢失敗");
        return result;
    }
}
View Code

接收資料並判斷

SongsDao
@Mapper
public interface SongsDao {
    @Select("select * from songs where sheet_type_id = #{sheet_type_id}")
    public List<Songs> findById(String sheet_type_id);
}

SongsDao

查詢歌單裡的所有歌曲,返回的是個集合List,到service

@Mapper
public interface SongsDao {
    @Select("select * from songs where sheet_type_id = #{sheet_type_id}")
    public List<Songs> findById(String sheet_type_id);
}

CorsConfig

package com.lylg.music.util;//package com.example.demo.util;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")

                .allowedOrigins("*")

                .allowCredentials(true)

                .allowedMethods("GET", "POST", "DELETE", "PUT")

                .maxAge(3600);
    }
}
View Code

SimpleCORSFilter
package com.lylg.music.util;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component

public class SimpleCORSFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletResponse response = (HttpServletResponse) res;

        response.setHeader("Access-Control-Allow-Origin", "*");

        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");

        response.setHeader("Access-Control-Max-Age", "3600");

        response.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");


        chain.doFilter(req, res);

    }

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}

}
View Code

最後放上我的專案結構