#万物oop
上一章文章 我们做了音乐播放器 数据表的创建。参加地址:
https://blog.csdn.net/Drug_/article/details/145093705
今天分享的这篇文章就是根据数据表 来写 业务逻辑 。 今天我们主要是实现管理后台的功能。
对于这篇文章 的理解 需要小伙伴有 springboot框架 基础 。
代码实现起来还是比较简单的,就是常规业务开发的增删改查 。
实现需求:
管理后台:
账号管理、专辑管理、作者管理、歌曲管理
一:账号管理
后端代码 如下:
因为代码内容过多
对于接口类和实体类 我就不分享了
实体类 里的对应的就是 数据表里的字段 因为我们是用 实体类接参数,所以需要根据数据表一一创建实体类
直接分享的是 控制器和实现类
用户表控制器:
package com.music.base.controller;
import com.music.base.aop.noLogin.NoLogin;
import com.music.base.entities.MuUser;
import com.music.base.out.R;
import com.music.base.request.BaseRequest;
import com.music.base.service.inteface.IMuUserService;
import com.music.base.webFilterIntercept.LoginUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* <p>
* 账号表 前端控制器
* </p>
*
* @author Json
* @since 2024-12-22
*/
@RestController
@RequestMapping("/mu-user")
public class MuUserController extends BaseController {
@Autowired
IMuUserService userService;
//注册
@PostMapping("register")
public R register(@RequestBody MuUser muUser){
return userService.register(muUser);
}
//删除
@GetMapping("del")
public R del(Integer id){
return userService.del(id);
}
//登录
@PostMapping("login")
@NoLogin
public R login(@RequestBody MuUser muUser){
return userService.login(muUser);
}
//列表
@GetMapping("pageList")
public R pageList(BaseRequest baseRequest){
return userService.pageList(baseRequest);
}
//当前登陆人信息
@GetMapping("getUserInfo")
public R getUserInfo(){
return R.data(userService.getById(LoginUserInfo.getLoginUserId()));
}
}
用户表实现类
package com.music.base.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.music.base.entities.MuUser;
import com.music.base.mapper.MuUserMapper;
import com.music.base.out.R;
import com.music.base.request.BaseRequest;
import com.music.base.service.inteface.IMuUserService;
import com.music.base.utils.JwtHelper;
import com.music.base.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/**
* <p>
* 账号表 服务实现类
* </p>
*
* @author Json
* @since 2024-12-22
*/
@Service
public class MuUserServiceImpl extends ServiceImpl<MuUserMapper, MuUser> implements IMuUserService {
@Override
public R register(MuUser muUser) {
if(StringUtils.isEmpty(muUser.getUsername()) || StringUtils.isEmpty(muUser.getPassword())){
return R.fail("账号密码不能为空!");
}
if(StringUtils.isEmpty(muUser.getNickname()) ){
return R.fail("用户昵称不能为空!");
}
long count=count(new QueryWrapper<MuUser>().lambda().eq(MuUser::getUsername, muUser.getUsername()));
if(count>0){
return R.fail("账号已存在!");
}
muUser.setPassword(MD5.encoder(muUser.getPassword()));
save(muUser);
return R.success();
}
@Override
public R login(MuUser muUser) {
if(StringUtils.isEmpty(muUser.getUsername()) || StringUtils.isEmpty(muUser.getPassword())){
return R.fail("账号密码不能为空!");
}
MuUser one = getOne(new QueryWrapper<MuUser>().lambda().eq(MuUser::getUsername, muUser.getUsername()));
if(ObjectUtils.isEmpty(one)){
return R.fail("账号不存在");
}
if(!one.getPassword().equals(MD5.encoder(muUser.getPassword()))){
return R.fail("密码不正确!");
}
String token = JwtHelper.sign(one.getId());
return R.data(token);
}
@Override
public R pageList(BaseRequest request) {
Page<MuUser> page = new Page<>(request.getPage() == null ? 1 : request.getPage(), request.getPageSize() == null ? 10 : request.getPageSize());
QueryWrapper<MuUser> queryWrapper = new QueryWrapper<>();
page(page, queryWrapper.lambda().orderByDesc(MuUser::getId));
return R.data(page);
}
@Override
public R del(Integer id) {
MuUser byId = getById(id);
if(ObjectUtils.isEmpty(byId)){
return R.fail("未查询到用户,删除失败!");
}
if(byId.getIsSuper().equals(1)){
return R.fail("内置管理员,不能删除");
}
removeById(id);
return R.success();
}
}
二.专辑管理
专辑控制器
package com.music.base.controller;
import com.music.base.entities.MuAlbum;
import com.music.base.out.R;
import com.music.base.request.BaseRequest;
import com.music.base.service.inteface.IMuAlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* <p>
* 专辑表 前端控制器
* </p>
*
* @author Json
* @since 2024-12-22
*/
@RestController
@RequestMapping("/mu-album")
public class MuAlbumController extends BaseController {
@Autowired
IMuAlbumService iMuAlbumService;
//列表
@GetMapping("pageList")
public R pageList(BaseRequest baseRequest){
return iMuAlbumService.pageList(baseRequest);
}
//添加
@PostMapping("add")
public R add(@RequestBody MuAlbum muAlbum){
return iMuAlbumService.add(muAlbum);
}
//删除
@GetMapping("del")
public R del(Integer id){
return iMuAlbumService.del(id);
}
}
专辑实现类
package com.music.base.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.music.base.entities.MuAlbum;
import com.music.base.entities.MuSong;
import com.music.base.mapper.MuAlbumMapper;
import com.music.base.out.R;
import com.music.base.request.BaseRequest;
import com.music.base.service.inteface.IMuAlbumService;
import com.music.base.service.inteface.IMuSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
/**
* <p>
* 专辑表 服务实现类
* </p>
*
* @author Json
* @since 2024-12-22
*/
@Service
public class MuAlbumServiceImpl extends ServiceImpl<MuAlbumMapper, MuAlbum> implements IMuAlbumService {
@Autowired
private IMuSongService iMuSongService;
@Override
public R pageList(BaseRequest request) {
Page<MuAlbum> page = new Page<>(request.getPage() == null ? 1 : request.getPage(), request.getPageSize() == null ? 10 : request.getPageSize());
QueryWrapper<MuAlbum> queryWrapper = new QueryWrapper<>();
page(page, queryWrapper.lambda().orderByDesc(MuAlbum::getId));
if(!page.getRecords().isEmpty()){
page.getRecords().forEach(muAlbum -> {
Long count= iMuSongService.count(new QueryWrapper<MuSong>().lambda().eq(MuSong::getAlbumId, muAlbum.getId()));
muAlbum.setAlbumSongCount(count);
});
}
return R.data(page);
}
@Override
public R add(MuAlbum muAlbum) {
if(StringUtils.isEmpty(muAlbum.getAlbumName())){
return R.fail("作者名称不能为空!");
}
long count=count(new QueryWrapper<MuAlbum>().lambda().eq(MuAlbum::getAlbumName, muAlbum.getAlbumName().trim()));
if(count>0){
return R.fail("作者名称已存在!");
}
muAlbum.setAlbumName( muAlbum.getAlbumName().trim());
save(muAlbum);
return R.success();
}
@Override
public R del(Integer id) {
removeById(id);
return R.success();
}
}
三.作者管理
作者表控制器
package com.music.base.controller;
import com.music.base.entities.MuAuthor;
import com.music.base.out.R;
import com.music.base.request.BaseRequest;
import com.music.base.service.inteface.IMuAuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* <p>
* 作者表 前端控制器
* </p>
*
* @author Json
* @since 2024-12-22
*/
@RestController
@RequestMapping("/mu-author")
public class MuAuthorController extends BaseController {
@Autowired
IMuAuthorService iMuAuthorService;
//列表
@GetMapping("pageList")
public R pageList(BaseRequest baseRequest){
return iMuAuthorService.pageList(baseRequest);
}
//添加
@PostMapping("add")
public R add(@RequestBody MuAuthor muAuthor){
return iMuAuthorService.add(muAuthor);
}
//删除
@GetMapping("del")
public R del(Integer id){
return iMuAuthorService.del(id);
}
}
作者表实现类
package com.music.base.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.music.base.entities.MuAuthor;
import com.music.base.entities.MuSong;
import com.music.base.mapper.MuAuthorMapper;
import com.music.base.out.R;
import com.music.base.request.BaseRequest;
import com.music.base.service.inteface.IMuAuthorService;
import com.music.base.service.inteface.IMuSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
/**
* <p>
* 作者表 服务实现类
* </p>
*
* @author Json
* @since 2024-12-22
*/
@Service
public class MuAuthorServiceImpl extends ServiceImpl<MuAuthorMapper, MuAuthor> implements IMuAuthorService {
@Autowired
private IMuSongService iMuSongService;
@Override
public R pageList(BaseRequest request) {
Page<MuAuthor> page = new Page<>(request.getPage() == null ? 1 : request.getPage(), request.getPageSize() == null ? 10 : request.getPageSize());
QueryWrapper<MuAuthor> queryWrapper = new QueryWrapper<>();
page(page, queryWrapper.lambda().orderByDesc(MuAuthor::getId));
if(!page.getRecords().isEmpty()){
page.getRecords().forEach(muAuthor -> {
Long count= iMuSongService.count(new QueryWrapper<MuSong>().lambda().eq(MuSong::getAuthorId, muAuthor.getId()));
muAuthor.setAuthorSongCount(count);
});
}
return R.data(page);
}
@Override
public R add(MuAuthor muAuthor) {
if(StringUtils.isEmpty(muAuthor.getAuthorName())){
return R.fail("作者名称不能为空!");
}
long count=count(new QueryWrapper<MuAuthor>().lambda().eq(MuAuthor::getAuthorName, muAuthor.getAuthorName().trim()));
if(count>0){
return R.fail("作者名称已存在!");
}
muAuthor.setAuthorName( muAuthor.getAuthorName().trim());
save(muAuthor);
return R.success();
}
@Override
public R del(Integer id) {
removeById(id);
return R.success();
}
}
四.歌曲管理
歌曲表控制器
package com.music.base.controller;
import com.music.base.entities.MuSong;
import com.music.base.out.R;
import com.music.base.request.MuSongRequest;
import com.music.base.service.inteface.IMuSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* <p>
* 歌曲表 前端控制器
* </p>
*
* @author Json
* @since 2024-12-22
*/
@RestController
@RequestMapping("/mu-song")
public class MuSongController extends BaseController {
@Autowired
IMuSongService iMuSongService;
//列表
@GetMapping("pageList")
public R pageList(MuSongRequest baseRequest){
return iMuSongService.pageList(baseRequest);
}
//添加
@PostMapping("add")
public R add(@RequestBody MuSong muSong){
return iMuSongService.add(muSong);
}
//编辑
@PostMapping("edit")
public R edit(@RequestBody MuSong muSong){
return iMuSongService.edit(muSong);
}
//删除
@GetMapping("del")
public R del(Integer id){
return iMuSongService.del(id);
}
}
歌曲表实现类
package com.music.base.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.music.base.entities.MuAlbum;
import com.music.base.entities.MuAuthor;
import com.music.base.entities.MuSong;
import com.music.base.mapper.MuSongMapper;
import com.music.base.out.R;
import com.music.base.request.MuSongRequest;
import com.music.base.service.inteface.IMuAlbumService;
import com.music.base.service.inteface.IMuAuthorService;
import com.music.base.service.inteface.IMuSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* <p>
* 歌曲表 服务实现类
* </p>
*
* @author Json
* @since 2024-12-22
*/
@Service
public class MuSongServiceImpl extends ServiceImpl<MuSongMapper, MuSong> implements IMuSongService {
@Autowired
IMuAlbumService iMuAlbumService;
@Autowired
IMuAuthorService iMuAuthorService;
@Override
public R pageList(MuSongRequest request) {
Page<MuSong> page = new Page<>(request.getPage() == null ? 1 : request.getPage(), request.getPageSize() == null ? 10 : request.getPageSize());
QueryWrapper<MuSong> queryWrapper = new QueryWrapper<>();
if(!StringUtils.isEmpty(request.getSearch())){
queryWrapper.lambda().like(MuSong::getSongName, request.getSearch());
}
if(request.getAlbumId()!=null){
queryWrapper.lambda().eq(MuSong::getAlbumId, request.getAlbumId());
}
if(request.getAuthorId()!=null){
queryWrapper.lambda().eq(MuSong::getAuthorId, request.getAuthorId());
}
page(page, queryWrapper.lambda().orderByDesc(MuSong::getId));
return R.data(page);
}
@Override
public R add(MuSong muSong) {
if(StringUtils.isEmpty(muSong.getSongName())){
return R.fail("歌曲名称不能为空!");
}
if(StringUtils.isEmpty(muSong.getSongUrl())){
return R.fail("歌曲不能为空!");
}
MuAlbum muAlbum = iMuAlbumService.getOne(new QueryWrapper<MuAlbum>().lambda().eq(MuAlbum::getAlbumName, muSong.getAlbumName()));
if(!ObjectUtils.isEmpty(muAlbum)){
muSong.setAlbumId(muAlbum.getId());
}
MuAuthor muAuthor = iMuAuthorService.getOne(new QueryWrapper<MuAuthor>().lambda().eq(MuAuthor::getAuthorName, muSong.getAuthorName()));
if(!ObjectUtils.isEmpty(muAuthor)){
muSong.setAuthorId(muAuthor.getId());
}
save(muSong);
return R.success();
}
@Override
public R del(Integer id) {
removeById(id);
return R.success();
}
@Override
public R edit(MuSong muSong) {
if(StringUtils.isEmpty(muSong.getId())){
return R.fail("id不能为空!");
}
if(StringUtils.isEmpty(muSong.getSongName())){
return R.fail("歌曲名称不能为空!");
}
if(StringUtils.isEmpty(muSong.getSongUrl())){
return R.fail("歌曲不能为空!");
}
MuAlbum muAlbum = iMuAlbumService.getOne(new QueryWrapper<MuAlbum>().lambda().eq(MuAlbum::getAlbumName, muSong.getAlbumName()));
if(!ObjectUtils.isEmpty(muAlbum)){
muSong.setAlbumId(muAlbum.getId());
}
MuAuthor muAuthor = iMuAuthorService.getOne(new QueryWrapper<MuAuthor>().lambda().eq(MuAuthor::getAuthorName, muSong.getAuthorName()));
if(!ObjectUtils.isEmpty(muAuthor)){
muSong.setAuthorId(muAuthor.getId());
}
updateById(muSong);
return R.success();
}
}
最后 再分享一下 文件上传的 控制器。
因为我们 后台是可以上传封面和歌曲的 所以 需要 一个文件上传的控制器。
package com.music.base.controller;
import com.music.base.aop.noLogin.NoLogin;
import com.music.base.out.R;
import com.music.base.utils.UploadHelper;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.Downloader;
import org.dromara.x.file.storage.core.FileInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
/**
* <p>
* 文件记录表 前端控制器
* </p>
*
* @author Json
* @since 2024-12-22
*/
@RestController
@RequestMapping("/file")
@Slf4j
public class FileDetailController extends BaseController {
@Autowired
UploadHelper uploadHelper;
@PostMapping("upload")
public R upload(@RequestBody MultipartFile file){
FileInfo fileInfo = uploadHelper.uploadFile(file, "link_music");
return R.data(fileInfo);
}
@GetMapping("view")
@NoLogin
public void view(String id, HttpServletResponse response) {
try {
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(id.getBytes("UTF-8"), "iso-8859-1"));
Downloader downloader=uploadHelper.downLoadFile(id);
downloader.outputStream(response.getOutputStream());
response.flushBuffer();
} catch (Exception e) {
log.error("文件下载失败: " + e.getMessage());
}
}
}
最后 首页 我简单的做了一个 报表统计
这样我们的音乐播放器的管理后台基本上就开发完整了。
说明:
因核心分享 的是java编程。后续文章中 基本分享的都是java语言的代码。
对于前端的代码,在文章中就不分享了。只会分享一下页面效果。
当然项目完结后,我会把前后端项目源码打包好放到最后一篇文章中。
如果有小伙伴有兴趣可以订阅此专栏,后续会持续更新直到音乐播放器这个软件完成。
我会尽可能详细的以文字的形式分享出来 从0到1 写出来一个音乐播放器项目。