很久之前做的一个小项目了,现在再重新对项目进行一个总结
在application.properties文件中进行数据库配置如下:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/musicserver?characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#配置xml
mybatis.mapper-locations=classpath:mybatis/**Mapper.xml
spring.servlet.multipart.max-file-size = 15MB
spring.servlet.multipart.max-request-size=100MB
music.local.path=/root/music
debug=true
logging.level.root=INFO
logging.level.com.example.onlinemusic.mapper=debug
logging.level.druid.sql.Statement=DEBUG
logging.level.com.example=DEBUG
create database if not exists `onlinemusic` character set utf8;
-- 使用数据库
use `onlinemusic`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(255) NOT NULL
);
DROP TABLE IF EXISTS `music`;
CREATE TABLE `music` (
`id` int PRIMARY KEY AUTO_INCREMENT,
`title` varchar(50) NOT NULL,
`singer` varchar(30) NOT NULL,
`time` varchar(13) NOT NULL,
`url` varchar(1000) NOT NULL,
`userid` int(11) NOT NULL
);
DROP TABLE IF EXISTS `lovemusic`;
CREATE TABLE `lovemusic` (
`id` int PRIMARY KEY AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`music_id` int(11) NOT NULL
);
package com.example.onlinemusic.tools;
import lombok.Data;
@Data
public class ResponseBodyMessage<T>{
//状态码
private int status;
//返回的信息 【出错信息,不出错信息】
private String message;
//返回给前端的数据
private T data;
public ResponseBodyMessage(int status, String message, T data) {
this.status = status;
this.message = message;
this.data = data;
}
}
这是待会登录后session所需要用的
package com.example.onlinemusic.tools;
public class Constant {
public static final String USERINFO_SESSION_KEY="USERINFO_SESSION_KEY";
}
创建model.User类
package com.example.onlinemusic.model;
import lombok.Data;
@Data //get,set方法都有了
public class User {
private int id;
private String username;
private String password;
}
package com.example.onlinemusic.mapper;
import com.example.onlinemusic.model.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
User selectByName(String username);
}
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@RequestMapping(value = "/login")
public ResponseBodyMessage<User> login2(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {
User user = userMapper.selectByName(username);
if (user == null) {
return new ResponseBodyMessage<>(-1,"登录失败",user);
} else {
boolean flg=bCryptPasswordEncoder.matches(password,user.getPassword());
if(!flg){
return new ResponseBodyMessage<>(-1,"用户名密码错误!",user);
}
request.getSession().setAttribute("USERINFO_SESSION_KEY",user);
return new ResponseBodyMessage<>(0, "登录成功", user);
}
}
这个项目最开始我用的是MD5对密码加密,后来我用的是BCrypt加密,下面来介绍一下这两种加密方式。
MD5是一个安全的散列算法,输入两个不同的明文不会得到相同的输出值,根据输出值,不能得到原始的明文,即其过程不可逆; 但是虽然不可逆,但是不是说就是安全的。因为自从出现彩虹表后,这样的密码也"不安全"。
MD5虽然能对密码进行加密,但是他每次加密后的结果是一样的,也就是说比如123,他就会根据散列算法的到一串巴拉巴拉的串,而且每次123进行散列之后都是同样的串,这样的话有些人就把这些串给总结出来了,也就是彩虹表。
彩虹表:彩虹表就是一个庞大的、针对各种可能的字母组合预先计算好的哈希值的集合,不一定是针对MD5算法的,各种算法的都有,有了它可以快速的破解各类密码。是复杂的密码,需要的彩虹表就越大,现在主流的彩虹表都是100G以上。
因为MD5算法不算特别安全,于是出现了更安全的做法,就是在MD5算法的基础上进行加盐或者长密码的做法,这样会让得到的串更长,更难破解。加盐的话对于MD5来说是固定的盐值,其实也不太安全
<!-- md5 依赖 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
Bcrypt就是一款加密工具,可以比较方便地实现数据的加密工作。你也可以简单理解为它内部自己实现了随机加盐处理 。我们使用MD5加密,每次加密后的密文其实都是一样的,这样就方便了MD5通过大数据的方式进行破解。Bcrypt生成的密文是60位的。而MD5的是32位的。Bcrypt破解难度更大。
添加依赖:
<!-- security依赖包 (加密)-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
在SpringBoot添加一个启动类(过滤器)
@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class})
写一个测试类
package com.example.onlinemusic.tools;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
public class BCryptTest {
public static void main(String[] args) {
//模拟从前端获得的密码
String password = "123456";
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
String newPassword = bCryptPasswordEncoder.encode(password);
System.out.println("加密的密码为: "+newPassword);
//使用matches方法进行密码的校验
boolean same_password_result = bCryptPasswordEncoder.matches(password,newPassword);
//返回true
System.out.println("加密的密码和正确密码对比结果: "+same_password_result);
boolean other_password_result = bCryptPasswordEncoder.matches("987654",newPassword);
//返回false
System.out.println("加密的密码和错误的密码对比结果: " + other_password_result); }
}
encode方法:对用户密码进行加密
matches方法:参数一,待检验的未加密的密码 。参数二:从数据库中查询出的加密后密码 。
BCrypt加密: 一种加盐的单向Hash,不可逆的加密算法,同一种明文(plaintext),每次加密后的密文都不一样,而且不可反向破解生成明文,破解难度很大。
MD5加密: 是不加盐的单向Hash,不可逆的加密算法,同一个密码经过hash的时候生成的是同一个hash值,在大多数的情况下,有些经过md5加密的方法将会被破解。
Bcrypt生成的密文是60位的。而MD5的是32位的。
目前,MD5和BCrypt比较流行。相对来说,BCrypt比MD5更安全,但加密更慢。 虽然BCrpyt也是输入的字符串+盐,但是与MD5+盐的主要区别是:每次加的盐不同,导致每次生成的结果也不相同。无法比对!
@Mapper
public interface UserMapper {
int insertUser(User user);
}
<insert id="insertUser">
insert into user(username,password) values(#{username},#{password});
</insert>
@RequestMapping("/register")
public ResponseBodyMessage<User> register(@RequestParam String username,@RequestParam String password,
HttpServletRequest request) {
// 1. 先对 输入的 password 进行加密
String newPassword = bCryptPasswordEncoder.encode(password);
// 创建 User 对象
User user = new User();
// 设置账号和 账号 和 密码
user.setUsername(username);
user.setPassword(newPassword);
// 在数据库中进行查询 看用户是否存在
User user1 = userMapper.selectByName(username);
if(user1 != null) {
System.out.println("用户已注册!");
return new ResponseBodyMessage<>(-1,"用户已注册!",user);
}
// 注册,往数据库中插入 新的 用户
int ret = userMapper.insertUser(user);
if(ret == 1) {
System.out.println("注册成功啦!");
return new ResponseBodyMessage<>(0,"注册成功啦!",user);
}else {
System.out.println("注册失败!");
return new ResponseBodyMessage<>(-1,"注册失败!",user);
}
}
@Data
public class Music {
private Integer id;
private String title;
private String singer;
private String time;
private String url;
private Integer userid;
}
@RestController
@RequestMapping("/music")
public class MusicController {
// 从配置文件中 将 路径 读取出来
@Value("${music.local.path}")
private String SAVE_PATH;
@RequestMapping("/upload")
public ResponseBodyMessage<Boolean> insertMusic(@RequestParam String singer,
@RequestParam("filename") MultipartFile file,
HttpServletRequest request) {
// 1. 检查是否登录
// 获取 session
HttpSession session = request.getSession(false);
// 判空
if(session == null || session.getAttribute
(Constant.USERINFO_SESSION_KEY) == null) {
System.out.println("没有登录!");
return new ResponseBodyMessage<>(-1,"请登录后上传!",false);
}
// 2. 上传到了服务器 ---- 拿到完整的文件名称 xxx.mp3
String fileNameAndType = file.getOriginalFilename(); // 获取完整文件名称
System.out.println("fileNameAndType" + fileNameAndType);
String path = SAVE_PATH + fileNameAndType;
File dest = new File(path); // dest 目录
if(!dest.exists()) {
dest.mkdir(); // 如果 dest(目录)不存在 创建目录
}
// 如果存在,通过 file.transferTo 将文件上传
try {
file.transferTo(dest);
return new ResponseBodyMessage<>(0,"上传成功!",true);
} catch (IOException e) {
e.printStackTrace();
}
return new ResponseBodyMessage<>(-1,"上传失败!",false);
}
}
其实每个类型的文件都有自己的格式,我们不能简单的通过文件的后缀名来判断
我去了解过MP3文件大体分为三部分:ID3V2,Frame,ID3V1。在ID3V1中有128个字节,其中有3个字节的标签标志“TAG”,通过这个标识可以识别出是否是mp3文件,但是我在项目中还没有对文件进行这方面的检测,后续可能会考虑去尝试做一下这个。
@Mapper
public interface MusicMapper {
/**
*
* @param title
* @param singer
* @param time
* @param url
* @param userid
* @return
*/
int insert(@Param("title") String title, @Param("singer") String singer, @Param("time") String time, @Param("url") String url, @Param("userid") int userid);
}
<?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 所对应的目录-->
<mapper namespace="com.example.onlinemusic.mapper.MusicMapper">
<insert id="insert" >
insert into music(title,singer,time,url,userid)
values(#{title},#{singer},#{time},#{url},#{userid});
</insert>
</mapper>
@RestController
@RequestMapping("/music")
public class MusicController {
@Value("${music.local.path}")
private String SAVE_PATH/* = "C:/work/local/music1/"*/;
@Autowired
private MusicMapper musicMapper;
@Resource
private LoveMusicMapper loveMusicMapper;
@RequestMapping("/upload")
public ResponseBodyMessage<Boolean> insertMusic(@RequestParam String singer,
@RequestParam("filename") MultipartFile file,
HttpServletRequest request,
HttpServletResponse resp) throws IOException {
//1、检查是否登录了
HttpSession httpSession = request.getSession(false);
if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
System.out.println("没有登录!");
return new ResponseBodyMessage<>(-1,"请登录后上传",false);
}
//2、先查询数据库当中 是否有当前音乐【歌曲名+歌手】
//2、上传到了服务器
String fileNameAndType = file.getOriginalFilename();//xxx.mp3
System.out.println("fileNameAndType: "+fileNameAndType);
String path = SAVE_PATH +"/"+fileNameAndType;
File dest = new File(path);
if(!dest.exists()) {
dest.mkdir();
}
try {
file.transferTo(dest);
//return new ResponseBodyMessage<>(0,"上传成功!",true);
} catch (IOException e) {
e.printStackTrace();
return new ResponseBodyMessage<>(-1,"服务器上传失败!",false);
}
// return new ResponseBodyMessage<>(-1,"上传失败!",false);
// 判断是不是 MP3 文件(TAG)
File file1 = new File(path);
byte[] result = null;
try {
result = Files.readAllBytes(file1.toPath());
if(result == null){
return new ResponseBodyMessage<>(-1,"当前文件不存在!",false);
}
String str = new String(result);
if(!str.contains("TAG")){
file1.delete();
return new ResponseBodyMessage<>(-1,"当前上传的不是 MP3 文件!",false);
}
} catch (IOException e) {
e.printStackTrace();
return new ResponseBodyMessage<>(-1,"服务器上传失败2!",false);
}
//进行数据库的上传
//1、准备数据 2、调用insert
int index = fileNameAndType.lastIndexOf(".");//lastIndexOf
String title = fileNameAndType.substring(0,index);
User user = (User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
int userid = user.getId();
//1、播放音乐-》http请求 存进去的时候,没有加后缀.mp3
String url = "/music/get?path="+title;
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
String time = sf.format(new Date());
try {
int ret = 0;
ret = musicMapper.insert(title,singer,time,url,userid);
if(ret == 1) {
//这里应该跳转到音乐列表页面
resp.sendRedirect("/list.html");
return new ResponseBodyMessage<>(0,"数据库上传成功!",true);
}else {
return new ResponseBodyMessage<>(-1,"数据库上传失败!",false);
}
}catch (BindingException e) {
dest.delete();
return new ResponseBodyMessage<>(-1,"数据库上传失败!",false);
}
//另外的一个问题: 如果重复上传一首歌曲,能否上传成功?? 可以
}
/**
* 播放音乐的时候:/music/get?path=xxx.mp3
* @param path
* @return
*/
@RequestMapping("/get")
public ResponseEntity<byte[]> get(String path) {
File file = new File(SAVE_PATH+"/"+path);
byte[] a = null;
try {
a = Files.readAllBytes(file.toPath());
if(a == null) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok(a);
} catch (IOException e) {
e.printStackTrace();
}
return ResponseEntity.badRequest().build();
//return ResponseEntity.internalServerError().build();
//return ResponseEntity.notFound().build();
}
/**
* 删除单个音乐
* @param id
* @return
*/
@RequestMapping("/delete")
public ResponseBodyMessage<Boolean> deleteMusicById(@RequestParam String id) {
//1、先检查这个音乐是不是存在的?
int iid = Integer.parseInt(id);
//2、如果存在要进行删除
Music music = musicMapper.findMusicById(iid);
if(music == null) {
System.out.println("没有这个id的音乐");
return new ResponseBodyMessage<>(-1,"没有你要删除的音乐",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);//liu
File file = new File(SAVE_PATH+"/"+fileName+".mp3");
System.out.println("当前的路径:"+file.getPath());
if(file.delete()) {
//同步删除lovemusic表当中的这个音乐
loveMusicMapper.deleteLoveMusicByMusicId(iid);
return new ResponseBodyMessage<>(0,"服务器当中的音乐删除成功!",true);
}else {
return new ResponseBodyMessage<>(-1,"服务器当中的音乐删除失败!",false);
}
}else {
return new ResponseBodyMessage<>(-1,"数据库当中的音乐没有删除成功!",false);
}
}
}
/**
* 批量进行删除
* @param id 【1,3,5,7,9】
* @return
*/
@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++) {
int musicId = id.get(i);
Music music = musicMapper.findMusicById(musicId);
if(music == null) {
System.out.println("没有这个id的音乐");
return new ResponseBodyMessage<>(-1, "没有你要删除的音乐", false);
}
int ret = musicMapper.deleteMusicById(musicId);
if(ret == 1) {
//2.2 删除服务器上的数据
int index = music.getUrl().lastIndexOf("=");
String fileName = music.getUrl().substring(index+1);//liu
File file = new File(SAVE_PATH+"/"+fileName+".mp3");
if(file.delete()) {
//同步检查lovemusic表当中 是否存在这个音乐
loveMusicMapper.deleteLoveMusicByMusicId(musicId);
sum += ret;
//return new ResponseBodyMessage<>(0,"服务器当中的音乐删除成功!",true);
}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);
}
}
@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);
}
}
@RequestMapping("/get")
public ResponseEntity<byte[]> get(String path) {
File file = new File(SAVE_PATH+"/"+path);
byte[] a = null;
try {
a = Files.readAllBytes(file.toPath());
if(a == null) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok(a);
} catch (IOException e) {
e.printStackTrace();
}
return ResponseEntity.badRequest().build();
//return ResponseEntity.internalServerError().build();
//return ResponseEntity.notFound().build();
int deleteById(int musicId);
<!-- 根据 id 删除音乐 -->
<delete id="deleteById" parameterType="java.lang.Integer">
delete from music where id = #{id}
</delete>
/**
* 删除单个音乐
* @param id
* @return
*/
@RequestMapping("/delete")
public ResponseBodyMessage<Boolean> deleteMusicById(@RequestParam String id) {
//1、先检查这个音乐是不是存在的?
int iid = Integer.parseInt(id);
//2、如果存在要进行删除
Music music = musicMapper.findMusicById(iid);
if(music == null) {
System.out.println("没有这个id的音乐");
return new ResponseBodyMessage<>(-1,"没有你要删除的音乐",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);//liu
File file = new File(SAVE_PATH+"/"+fileName+".mp3");
System.out.println("当前的路径:"+file.getPath());
if(file.delete()) {
//同步删除lovemusic表当中的这个音乐
loveMusicMapper.deleteLoveMusicByMusicId(iid);
return new ResponseBodyMessage<>(0,"服务器当中的音乐删除成功!",true);
}else {
return new ResponseBodyMessage<>(-1,"服务器当中的音乐删除失败!",false);
}
}else {
return new ResponseBodyMessage<>(-1,"数据库当中的音乐没有删除成功!",false);
}
}
}
/**
* 批量进行删除
* @param id 【1,3,5,7,9】
* @return
*/
@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++) {
int musicId = id.get(i);
Music music = musicMapper.findMusicById(musicId);
if(music == null) {
System.out.println("没有这个id的音乐");
return new ResponseBodyMessage<>(-1, "没有你要删除的音乐", false);
}
int ret = musicMapper.deleteMusicById(musicId);
if(ret == 1) {
//2.2 删除服务器上的数据
int index = music.getUrl().lastIndexOf("=");
String fileName = music.getUrl().substring(index+1);//liu
File file = new File(SAVE_PATH+"/"+fileName+".mp3");
if(file.delete()) {
//同步检查lovemusic表当中 是否存在这个音乐
loveMusicMapper.deleteLoveMusicByMusicId(musicId);
sum += ret;
//return new ResponseBodyMessage<>(0,"服务器当中的音乐删除成功!",true);
}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);
}
}
@Mapper
public interface LoveMusicMapper {
/**
* 收藏音乐
* @param userId
* @param musicId
* @return
*/
boolean insertLoveMusic(Integer userId,Integer musicId);
/**
* 查询喜欢的音乐
* @param userId
* @param musicId
* @return
*/
Music findLoveMusicByMusicIdAndUserId(Integer userId, Integer musicId);
}
<?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.onlinemusic.mapper.LoveMusicMapper">
<insert id="insertLoveMusic">
insert into lovemusic(user_id,music_id) values(#{userId},#{musicId})
</insert>
<select id="findLoveMusicByMusicIdAndUserId" resultType="com.example.onlinemusic.model.Music">
select * from lovemusic where user_id=#{userId} and music_id=#{musicId}
</select>
</mapper>
package com.example.onlinemusic.controller;
import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {
@Autowired
private LoveMusicMapper loveMusicMapper;
@RequestMapping("/likeMusic")
public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request){
int musicId=Integer.parseInt(id); //转换id,变成一个整数
System.out.println("musicId"+musicId);
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<>(0,"收藏失败!",false);
}
}
@RequestMapping("/findlovemusic")
public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false)String musicName, HttpServletRequest request){
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){
loveMusicMapper.findLoveMusicByUserId(userId);
}else {
musicList=loveMusicMapper.findLoveMusicBykeyAndUID(musicName,userId);
}
return new ResponseBodyMessage<>(0,"查询到了所有的歌曲信息!",musicList);
}
@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);
}
}
}
/**
* 查询这个用户收藏过的所有音乐
* @param userId
* @return
*/
List<Music> findLoveMusicByUserId(Integer userId);
/**
* 查询当前用户,指定为musicName的音乐,支持模糊查询
* @param musicName
* @param userId
* @return
*/
List<Music> findLoveMusicBykeyAndUID(String musicName,Integer userId);
<select id="findLoveMusicByUserId" resultType="com.example.onlinemusic.model.Music">
select m.* from lovemusic lm,music m where m.id = lm.music_id and lm.user_id=#{userInd}
</select>
<select id="findLoveMusicBykeyAndUID" resultType="com.example.onlinemusic.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>
package com.example.onlinemusic.controller;
import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {
@Autowired
private LoveMusicMapper loveMusicMapper;
@RequestMapping("/likeMusic")
public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request){
int musicId=Integer.parseInt(id); //转换id,变成一个整数
System.out.println("musicId"+musicId);
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<>(0,"收藏失败!",false);
}
}
@RequestMapping("/findlovemusic")
public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false)String musicName, HttpServletRequest request){
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){
loveMusicMapper.findLoveMusicByUserId(userId);
}else {
musicList=loveMusicMapper.findLoveMusicBykeyAndUID(musicName,userId);
}
return new ResponseBodyMessage<>(0,"查询到了所有的歌曲信息!",musicList);
}
@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);
}
}
}