@Configuration
public class AppConfig implements WebMvcConfigurer {
@Bean
public BCryptPasswordEncoder getBCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
public int insert(String title, String author, String path, int userId){
return musicMapper.insert(title,author,path,userId);
}
public List selectByTitle(String title) {
return musicMapper.selectByTitle(title);
}
10.2.2 在 MusicMapper 中添加代码
int insert(String title, String author, String path, int userId);
List selectByTitle(String title);
10.2.3 在 MusicMapper.xml 中添加代码
insert into music(title,author,path,userId) values (#{title},#{author},#{path},#{userId});
public Music selectById(int musicId) {
return musicMapper.selectById(musicId);
}
public int deleteById(int musicId) {
return musicMapper.deleteById(musicId);
}
collectServer
public int deleteLoveMusicById(int musicId){
return collectMapper.deleteLoveMusicById(musicId);
}
12.1.2 在 MusicMapper 和 CollectMapper 中添加代码
musicMapper
Music selectById(int musicId);
int deleteById(int musicId);
@RequestMapping("/deleteMore")
public ResponseBodyMessage deleteMoreMusic(@RequestParam("musicId[]") List musicId) {
int sum = 0;
for (int i = 0; i < musicId.size(); i++) {
Music music = musicServer.selectById(musicId.get(i));
if(music == null) {
return new ResponseBodyMessage<>(-1,"没有你要删除的音乐",false);
}
int ret = musicServer.deleteById(musicId.get(i));
if (ret == 1) {
int index = music.getPath().lastIndexOf("=");
String PathName = music.getPath().substring(index+1);
File file = new File(SAVE_PATH + PathName+".mp3");
if(file.delete()){
collectServer.deleteLoveMusicById(musicId.get(i));
sum += ret;
}else{
return new ResponseBodyMessage<>(-1,"服务器删除失败!",false);
}
}else {
return new ResponseBodyMessage<>(-1,"数据库删除失败!",false);
}
}
if(sum == musicId.size()) {
return new ResponseBodyMessage<>(1,"音乐删除成功!",true);
}else{
return new ResponseBodyMessage<>(-1,"音乐删除失败!",false);
}
}
@Data
public class Collect {
private int collectId;
private int userId;
private int musicId;
}
13.2 使用 Mybatis 操作数据库
首先要通过 musicId 和 userId去查找当前是否存在 collect 表中
在通过 musicId 和 userId 去添加歌曲
13.2.1 在 CollectServer 中添加代码
public Collect findCollectMusic(int userId, int musicId) {
return collectMapper.findCollectMusic(userId,musicId);
}
public int insertLoveMusic(int userId, int musicId) {
return collectMapper.insertLoveMusic(userId, musicId);
}
13.2.2 在 CollectMapper 中添加代码
Collect findCollectMusic(int userId, int musicId);
int insertLoveMusic(int userId, int musicId);
13.2.3 在 CollectMapper.xml 中添加代码
13.3 向 CollectControll 中添加代码
通过用户Id 和 musicId查看是否存在歌曲
如果存在就返回收藏失败
如果不存在, 就根据用户id和musicId 添加收藏
@RequestMapping("/loveMusic")
public ResponseBodyMessage AddLoveMusic(@RequestParam String musicId, HttpServletRequest request) {
int music_Id = Integer.parseInt(musicId);
HttpSession session = request.getSession(false);
User user = (User) session.getAttribute(Constant.USER_SESSION_KEY);
int userId = user.getUserId();
Collect collect = collectServer.findCollectMusic(userId,music_Id);
if (collect != null) {
return new ResponseBodyMessage<>(-1, "当前已经收藏了",false);
}else{
int ret = collectServer.insertLoveMusic(userId,music_Id);
if(ret == 1) {
return new ResponseBodyMessage<>(1, "收藏成功!",true);
}else{
return new ResponseBodyMessage<>(-1,"收藏失败",false);
}
}
}
public List findMusic() {
return musicMapper.findMusic();
}
public List findMusicByName(String name) {
return musicMapper.findMusicByName(name);
}
15.1.2 在 MusicMapper 中添加代码
List findMusic();
List findMusicByName(String name);
15.1.3 在 MusicMapper.xml 中添加代码
15.2 向 MusicController 中添加代码
这里判断前端传来的 name是否为空
不为空, 进入模糊查询
为空, 进入空查询
@RequestMapping("/findMusic")
public ResponseBodyMessage> findMusic(@RequestParam(required = false) String name) {
List list = null;
if(name != null) {
list = musicServer.findMusicByName(name);
}else {
list = musicServer.findMusic();
}
return new ResponseBodyMessage<>(1,"查询完毕!",list);
}
15.3 前端代码
$(function(){load()});
function load(musicName) {
$.ajax({
url: 'music/findMusic',
method: 'POST',
data: {"name":musicName},
dataType: "json",
success: function(data,status) {
if(data.data!=null){
createMusic1(data.data);
let audios = document.querySelectorAll('#player2');
for(let audio of audios) {
new MediaElementPlayer(audio, {
pluginPath: 'https://cdn.jsdelivr.net/npm/mediaelement@4.2.7/build/',
shimScriptAccess: 'always',
success: function () {
let play = document.querySelector('.player');
play.style = "visibility: visible;";
}
});
}
}
}
})
}
function createMusic1(lists) {
let s = '';
for(let list of lists) {
s+= '';
s+= '';
s+= '';
s+= '';
s+= '
'+list.title+'';
s+= ''+list.author+'/'+DateFormat(list.uploadtime)+'';
s+= '';
s+= '';
s+= '';
s+= '';
s+= '';
s+= '';
}
$("#list23").html(s);
}
// 把毫秒级时间戳转化成格式化日期
function DateFormat(timeStampMS) {
var date = new Date(timeStampMS);
var year = date.getFullYear(),
month = date.getMonth()+1,//月份是从0开始的
day = date.getDate(),
hour = date.getHours(),
min = date.getMinutes(),
sec = date.getSeconds();
var newTime = year + '-' +
(month < 10? '0' + month : month) + '-' +
(day < 10? '0' + day : day) + ' ' +
(hour < 10? '0' + hour : hour) + ':' +
(min < 10? '0' + min : min) + ':' +
(sec < 10? '0' + sec : sec);
return newTime;
}
$(function(){
$("#submit1").click( function(){
var name = $("#exampleInputName2").val();
load(name);
});
});
15.4 测试代码
16. 收藏页面 - 查询模块
16.1 使用 Mybatis 操作数据库
这里有空查询和模糊查询两种数据库操作
空查询 不带 name
模糊查询带name
16.1.1 在 CollectServer 中添加代码
public List findLoveMusicByUserId(int userId){
return collectMapper.findLoveMusicByUserId(userId);
}
public List findLoveMusicByNameAndUserId(String name,int userId){
return collectMapper.findLoveMusicByNameAndUserId(name,userId);
}
16.1.2 在 CollectMapper 中添加代码
List findLoveMusicByUserId(int userId);
List findLoveMusicByNameAndUserId(String name,int userId);
16.1.3 在 CollectMapper.xml 中添加代码
select m.* from collect c,music m where m.musicId = c.musicId and c.userId = #{userId};
select m.* from collect c,music m where m.musicId = c.musicId and c.userId = #{userId} and m.title like concat('%',#{name},'%');
16.2 向 CollectController 中添加代码
这里判断前端传来的 name是否为空
不为空, 进入模糊查询
为空, 进入空查询
@RequestMapping("findLoveMusic")
public ResponseBodyMessage> findLoveMusic(@RequestParam(required = false) String musicName,HttpServletRequest request) {
HttpSession session = request.getSession(false);
if(session == null) {
return new ResponseBodyMessage<>(-1,"当前未登录",null);
}
User user = (User) session.getAttribute(Constant.USER_SESSION_KEY);
int userId = user.getUserId();
List list = null;
if(musicName == null) {
list = collectServer.findLoveMusicByUserId(userId);
}else{
list = collectServer.findLoveMusicByNameAndUserId(musicName,userId);
}
return new ResponseBodyMessage<>(1,"查询成功!",list);
}
16.3 前端代码
$(function(){load()});
function load(musicName) {
$.ajax({
url: 'collect/findLoveMusic',
method: 'POST',
data: {"musicName":musicName},
dataType: "json",
success: function(data,status) {
if(data.data!=null){
createMusic1(data.data);
let audios = document.querySelectorAll('#player2');
for(let audio of audios) {
new MediaElementPlayer(audio, {
pluginPath: 'https://cdn.jsdelivr.net/npm/mediaelement@4.2.7/build/',
shimScriptAccess: 'always',
success: function () {
let play = document.querySelector('.player');
play.style = "visibility: visible;";
}
});
}
}
}
})
}
$(function(){
$("#submit1").click( function(){
var name = $("#exampleInputName2").val();
load(name);
});
});
function createMusic1(lists) {
let s = '';
for(let list of lists) {
s+= '';
s+= '';
s+= '';
s+= '
'+list.title+'';
s+= ''+list.author+'/'+DateFormat(list.uploadtime)+'';
s+= '';
s+= '';
s+= '';
s+= '';
s+= '';
}
$("#list23").html(s);
}
// 把毫秒级时间戳转化成格式化日期
function DateFormat(timeStampMS) {
var date = new Date(timeStampMS);
var year = date.getFullYear(),
month = date.getMonth()+1,//月份是从0开始的
day = date.getDate(),
hour = date.getHours(),
min = date.getMinutes(),
sec = date.getSeconds();
var newTime = year + '-' +
(month < 10? '0' + month : month) + '-' +
(day < 10? '0' + day : day) + ' ' +
(hour < 10? '0' + hour : hour) + ':' +
(min < 10? '0' + min : min) + ':' +
(sec < 10? '0' + sec : sec);
return newTime;
}