6. 删除音乐模块设计
6.1 删除单个音乐
1. 请求响应设计
2. 开始实现
首先在musicmapper新增操作
Music findMusicById(int id);int deleteMusicById(int musicId);
其次新增相对应的.xml代码:
<select id="findMusicById" resultType="com.example.spring_musicserver_1113.model.Music">select * from music where id = #{id};</select><delete id="deleteMusicById" parameterType="java.lang.Integer">delete from music where id = #{id};</delete>
最后musiccontroller代码:
/*** 删除单个音乐* @param id* @return*/@RequestMapping("/delete")public ResponseBodyMessage<Boolean> deleteMusicById(@RequestParam String id){//1、检查当前ID的音乐是否存在int iid = Integer.parseInt(id);//2、如果当前ID音乐存在的话就要删除Music music = musicMapper.findMusicById(iid);if(music == null){System.out.println("没有查询到当前id的音乐");return new ResponseBodyMessage<>(-1,"没有查询到当前id的音乐",false);}else {//2.1 删除数据路int ret = musicMapper.deleteMusicById(iid);if(ret == 1){//2.2 删除服务器上的数据int index = music.getUrl().lastIndexOf("=");String fileName = music.getUrl().substring(index+1);File file = new File(SAVE_PATH+"/"+fileName+".mp3");System.out.println("当前的路径"+file.getPath());if (file.delete()){return new ResponseBodyMessage<>(0,"服务器中的音乐删除成功",true);}else {return new ResponseBodyMessage<>(-1,"服务器中的音乐删除失败",false);}}else {return new ResponseBodyMessage<>(-1,"数据库当中的音乐没有删除成功",false);}}}
进行测试:
发现数据库和服务器路径下的mp3文件都被删除了;
6.2 批量删除选中的音乐
1、请求响应设计
2、代码实现
新增deleteSelMusic方法:
其逻辑和之前单个删除的类似,在之前代码的基础上使用一个for循环,来查询每一个音乐文件是否存在并进行删除和统计,当统计的数量值是需要删除的数量值的时候就是批量删除成功;
@RequestMapping("/deleteSel")public ResponseBodyMessage<Boolean> deleteSelMusic(@RequestParam("id[]")List<Integer> id){System.out.println("所有的id"+ id);int sum = 0;for (int i = 0; i < id.size(); i++) {Music music = musicMapper.findMusicById(id.get(i));if(music == null){System.out.println("没有查询到当前id的音乐");return new ResponseBodyMessage<>(-1,"没有查询到当前id的音乐",false);}int ret = musicMapper.deleteMusicById(id.get(i));if(ret == 1) {int index = music.getUrl().lastIndexOf("=");String fileName = music.getUrl().substring(index + 1);File file = new File(SAVE_PATH + "/" + fileName + ".mp3");if (file.delete()) {sum += ret;} else {return new ResponseBodyMessage<>(-1, "服务器中的音乐删除失败", false);}}else {return new ResponseBodyMessage<>(-1,"数据库当中的音乐没有删除成功",false);}}if(sum == id.size()){System.out.println("批量删除成功");return new ResponseBodyMessage<>(0,"音乐批量删除成功",true);}else {System.out.println("批量删除失败");return new ResponseBodyMessage<>(-1,"音乐批量删除失败",false);}}
进行测试:
7. 查询⾳乐模块设计
1、请求响应模块设计
此处查询需要满⾜⼏个功能:
1. ⽀持模糊查询
2. ⽀持传⼊参数为空
当我们传递的参数为空的时候,查询的结果就是当前表里面的所所有music数据;
2、代码实现
数据层代码如下:
musicmapper新增接口:
List<Music> findMusic();List<Music> findMusicByName(String musicName);
.xml配置如下:
<select id="findMusic" resultType="com.example.spring_musicserver_1113.model.Music">select * from music;</select><select id="findMusicByName" resultType="com.example.spring_musicserver_1113.model.Music">select * from music where title like concat('%',#{musicName},'%');</select>
控制层代码如下:
@RequestMapping("/findmusic")public ResponseBodyMessage<List<Music>> findMusic(@RequestParam(required = false) String musicName){List<Music> musicList = null;if(musicName != null){musicList = musicMapper.findMusicByName(musicName);}else {musicList = musicMapper.findMusic();}return new ResponseBodyMessage<>(0,"查询到了当前的所有音乐",musicList);}
进行测试,模糊有参查找:
空参查找测试:
8. 喜欢音乐模块设计
8.1 添加⾳乐⾄喜欢的列表模块设计
1、响应和请求
2、代码实现
数据层:
实现LoveMusicMapper接⼝,收藏/喜欢⾳乐功能:
1、需要查询此次收藏⾳乐是否之前收藏过,收藏过则不能添加
2、没有收藏过,插⼊数据库中⼀条记录
Music findLoveMusicByMusicIdAndUserId(int userId,int musicId);boolean insertLoveMusic(int userId,int musicId);
进行xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.spring_musicserver_1113.mapper.LoveMusicMapper"><select id="findLoveMusicByMusicIdAndUserId" resultType="com.example.spring_musicserver_1113.model.Music">select * from lovemusic where user_id = #{userId} and music_id = #{musicId};</select><insert id="insertLoveMusic">insert into lovemusic(user_id,music_id) values(#{userId},#{musicId});</insert>
</
控制层:实现LoveMusicController类
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {@Resourceprivate LoveMusicMapper loveMusicMapper;@RequestMapping("/likeMusic")public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id,HttpServletRequest request){int musicId = Integer.parseInt(id);System.out.println("musicId:"+musicId);//1、检查当前是否等了HttpSession httpSession = request.getSession(false);if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null){System.out.println("没有登录!");return new ResponseBodyMessage<>(-1,"请登录后进行收藏喜欢",false);}User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);int userId = user.getId();System.out.println("userId:"+userId);Music music = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId,musicId);if(music != null){//这里加一个取消收藏的功能return new ResponseBodyMessage<>(-1,"该音乐已被喜欢收藏",false);}boolean effect = loveMusicMapper.insertLoveMusic(userId,musicId);if(effect){return new ResponseBodyMessage<>(0,"收藏成功",true);}else {return new ResponseBodyMessage<>(-1,"收藏失败",false);}}
}
测试结果如下:
首先进行喜欢收藏测试,测试通过;当同一首歌进行第二次喜欢收藏时:
lovemusic表如下所示:
8.2 查询喜欢的⾳乐模块设计
1、请求和响应
上面是无参数请求,同时和有参数请求类似;
2、代码实现
数据层:实现LoveMusicMapper新增⽅法:
List<Music> findLoveMusicByUserId(int userId);List<Music> findLoveMusicByKeyAndUID(String musicName,int userId);
实现LoveMusicMapper.xml
<select id="findLoveMusicByUserId" resultType="com.example.spring_musicserver_1113.model.Music">select m.* from lovemusic lm,music m where m.id = lm.music_id and lm.user_id = #{userId};</select><select id="findLoveMusicByKeyAndUID" resultType="com.example.spring_musicserver_1113.model.Music">select m.* from lovemusic lm,music m where m.id = lm.music_id and lm.user_id = #{userId}and title like concat("%",#{musicName},"%");</select>
控制层:实现LoveMusicController,新增⽅法
@RequestMapping("findLoveMusic")public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,HttpServletRequest request){//1、验证当前是否登录HttpSession httpSession = request.getSession(false);if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null){System.out.println("没有登录!");return new ResponseBodyMessage<>(-1,"请登录后进行查找收藏喜欢的音乐",null);}User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);int userId = user.getId();System.out.println("userId"+userId);List<Music> musicList=null;if(musicName == null){musicList =loveMusicMapper.findLoveMusicByUserId(userId);}else {musicList = loveMusicMapper.findLoveMusicByKeyAndUID(musicName,userId);}return new ResponseBodyMessage<>(0,"查询到了所有的歌曲信息",musicList);}
对于
@RequestParam
注解:该注解
@RequestParam
可以从请求中提取查询参数、表单参数甚至是多个参数,即获取下面请求中的查询字符串的值;变量名和参数名都是相同的(userId),如果变量名称和参数名称不同,可以使用
name
属性配置@RequestParam
名称:@RequestParam(name = "name") String musicName:将请求中name的值提取出来赋给musicName:127.0.0.1:8089/lovemusic/findLoveMusic?name=w
使用
@RequestParam
注解的方法参数默认为必填参数(默认是require== true),如果想要进行下面请求中带有参数的请求的话,spring会按照要求处理请求中查询字符串参数;如果没有请求字符串的话服务器会返回404的状态码;@RequestParam(required = false)就是对于请求中的查询字符串不做是否存在的要求;即没有查询字符串是为查询当前所有的数据;
有查询字符串是按照要求查找当前指定的数据;
8.3 移除喜欢的⾳乐模块设计
当前我们的收藏音乐表:
1. 请求和响应
2.代码实现
数据层:
int deleteLoveMusic(int userId,int musicId);
<delete id="deleteLoveMusic" parameterType="java.lang.Integer">delete from lovemusic where user_id =#{userId} and music_id =#{musicId};</delete>
控制层:
@RequestMapping("/deletelovemusic")public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id,HttpServletRequest request){int musicId = Integer.parseInt(id);//验证登录HttpSession httpSession = request.getSession(false);if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null){System.out.println("没有登录!");return new ResponseBodyMessage<>(-1,"请登录后进行查找收藏喜欢的音乐",null);}User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);int userId = user.getId();int ret = loveMusicMapper.deleteLoveMusic(userId,musicId);if(ret == 1){return new ResponseBodyMessage<>(0,"取消收藏成功",true);}else {return new ResponseBodyMessage<>(-1,"取消收藏失败",false);}}
如上所示,测试成功;
此时我们存在这样一个问题:
表一为收藏音乐的表,表二为音乐列表,我们在表一中进行取消收藏操作是不会对表二有影响的;对表二进行删除操作也是没有影响的(但是实际上表二中的删除操作会导致表一中音乐会被间接的被取消收藏)
问题解决方法:
在音乐删除操作的逻辑中,添加通过musicid取消收藏的逻辑:
int deleteLoveMusicByMusicId(int musicId);
<delete id="deleteLoveMusicByMusicId" parameterType="java.lang.Integer">delete from lovemusic where music_id =#{musicId};</delete>
测试结果如下:进行删除单曲音乐之后:
存在的问题:音乐在服务器中删除之后还能进行收藏;