音乐网站SpringBoot+Vue项目总结

项目说明

首页
首页
技术栈:SpringBoot,Vue。后端功能相对简单,主要是增删查改,代码有些冗余,算是对之前SpringBoot知识的巩固。

项目配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dql</groupId>
    <artifactId>music</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>music</name>
    <description>音乐网站</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

都是常用配置,使用了阿里巴巴的fastjson。

application.yml:

server:
  port: 8888
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/music?serverTimezone=Asia/Shanghai&userUnicode=true&characterEncoding=utf8&useSSL=false
    username: 
    password: 
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
  servlet:
    multipart:
      max-file-size: 50MB
      max-request-size: 50MB
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.music.domain

使用druid连接池,限制上传文件大小50MB。

业务分析

以歌曲业务为例:新增歌曲,修改歌曲,删除歌曲,根据主键查询整个对象,查询所有歌曲,根据歌名模糊查询歌曲,根据歌手id查询歌曲。

实体类

public class Song implements Serializable {
    /*主键*/
    private int id;
    /*歌手id*/
    private int singerId;
    /*歌名*/
    private String name;

    /*简介*/
    private String introduction;
    /*创建时间*/
    private Date createTime;
    /*更新时间*/
    private Date updateTime;
    /*歌曲图片*/
    private String pic;
    /*歌词*/
    private String lyric;
    /*歌曲地址*/
    private String url;
 }

get set以及toString方法省略,实体类实现Serializable接口是为了对实例对象的状态(State 对象属性而不包括对象方法)进行通用编码(如格式化的字节码)并保存,以保证对象的完整性和可传递性,就是为了在不同时间或不同平台的JVM之间共享实例对象。

DAO层

@Repository
public interface SongMapper {

    /**
     * 增加
     *
     * @param song
     * @return
     */
    public int insert(Song song);

    /**
     * 修改
     *
     * @param song
     * @return
     */
    public int update(Song song);

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int delete(Integer id);

    /**
     * 根据主键查询整个对象
     *
     * @param id
     * @return
     */
    public Song selectByPrimaryKey(Integer id);

    /**
     * 查询所有歌曲
     *
     * @return
     */
    public List<Song> allSong();


    /**
     * 根据歌名模糊查询列表
     *
     * @param name
     * @return
     */
    public List<Song> songOfName(String name);

    /**
     * 根据歌手id查询歌曲
     *
     * @param singerId
     * @return
     */
    public List<Song> songOfSingerId(Integer singerId);
}

@Repository注解加在持久层接口上,用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。

映射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.dql.music.dao.SongMapper">

    <resultMap id="BaseResultMap" type="com.dql.music.domain.Song">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="singer_id" jdbcType="INTEGER" property="singerId"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="url" jdbcType="VARCHAR" property="url"/>
        <result column="pic" jdbcType="VARCHAR" property="pic"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="introduction" jdbcType="VARCHAR" property="introduction"/>
    </resultMap>


    <resultMap id="ResultMapWithBLOBs" type="com.dql.music.domain.Song" extends="BaseResultMap">
        <result column="lyric" jdbcType="LONGVARCHAR" property="lyric"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,singer_id,name,url,pic,create_time,update_time,introduction
    </sql>

    <sql id="Blob_Column_List">
        lyric,id,singer_id,name,url,pic,lyric,create_time,update_time,introduction
    </sql>
    <insert id="insert" parameterType="com.dql.music.domain.Song">
        insert into song
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="singerId != null">
                singer_Id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="url != null">
                url,
            </if>
            <if test="pic != null">
                pic,
            </if>
            <if test="lyric != null">
                lyric,
            </if>
            create_time,update_time,
            <if test="introduction != null">
                introduction,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="singerId != null">
                #{singerId},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="url != null">
                #{url},
            </if>
            <if test="pic != null">
                #{pic},
            </if>
            <if test="lyric != null">
                #{lyric},
            </if>
            now(),now(),
            <if test="introduction != null">
                #{introduction},
            </if>
        </trim>
    </insert>

    <update id="update" parameterType="com.dql.music.domain.Song">
        update song
        <set>
            <if test="singerId != null">
                singer_id=#{singerId},
            </if>
            <if test="name != null">
                name=#{name},
            </if>
            <if test="url != null">
                url=#{url},
            </if>
            <if test="pic != null">
                pic=#{pic},
            </if>
            <if test="lyric != null">
                lyric=#{lyric},
            </if>
            update_time=now(),

            <if test="introduction != null">
                introduction=#{introduction},
            </if>
        </set>
        where id=#{id}
    </update>

    <delete id="delete" parameterType="java.lang.Integer">
        delete
        from song
        where id = #{id}
    </delete>

    <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
        select
        <include refid="Blob_Column_List"></include>
        from song
        where id = #{id}
    </select>

    <select id="allSong" resultMap="ResultMapWithBLOBs">
        select
        <include refid="Blob_Column_List"/>
        from song
    </select>

    <select id="songOfName" resultMap="ResultMapWithBLOBs" parameterType="java.lang.String">
        select
        <include refid="Blob_Column_List"/>
        from song where name = #{name}
    </select>

    <select id="songOfSingerId" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
        select
        <include refid="Blob_Column_List"/>
        from song where singer_id = #{singerId}
    </select>

</mapper>

mybatis映射文件很长,目前还没学习mybatisplus。
mysql中的extends可以避免定义多个resultMap,减少代码冗余。map中左侧为数据库中字段名,右侧为实体类中对应的属性名,注意区分下划线。
SQL 中的 trim 函数用来移除掉一个字串中的字头或字尾。最常见的用途是移除字首或字尾的空白。
写sql语句时使用test判断传参是否为空,同样需要注意数据库字段与实体类属性名的差异。

Controller层

Service层与ServiceImpl类与Mapper接口功能相似,省略。

@RestController
@RequestMapping(value = "/song")
public class SongController {

    @Autowired
    private SongService songService;

    /**
     * 添加歌曲
     *
     * @param request
     * @param mpfile
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addSong(HttpServletRequest request, @RequestParam("file") MultipartFile mpfile) {
        JSONObject jsonObject = new JSONObject();

        /*获取前端传来的参数*/
        String singerId = request.getParameter("singerId").trim();
        String name = request.getParameter("name").trim();
        String introduction = request.getParameter("introduction").trim();
        String pic = "/img/songPic/1618326979608라일락.jpg";
        String lyric = request.getParameter("lyric").trim();
        /*上传歌曲文件*/
        if (mpfile.isEmpty()) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "文件上传失败");
        } else {
            String fileName = System.currentTimeMillis() + mpfile.getOriginalFilename();
            String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "img"
                    + System.getProperty("file.separator") + "songPic";
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdir();
            } else {
                /*实际的文件地址*/
                File dest = new File(filePath + System.getProperty("file.separator") + fileName);
                /*存储到数据库的相对文件地址*/
                String storeUrlPath = "/img/songPic/" + fileName;
                try {
                    mpfile.transferTo(dest);
                    Song song = new Song();
                    song.setSingerId(Integer.parseInt(singerId));
                    song.setName(name);
                    song.setIntroduction(introduction);
                    song.setLyric(lyric);
                    song.setUrl(storeUrlPath);
                    song.setPic(pic);
                    boolean flag = songService.insert(song);
                    if (flag) {
                        jsonObject.put(Consts.CODE, 1);
                        jsonObject.put(Consts.MSG, "文件上传成功");
                        jsonObject.put("pic", storeUrlPath);
                    } else {
                        jsonObject.put(Consts.CODE, 0);
                        jsonObject.put(Consts.MSG, "文件上传失败");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    jsonObject.put(Consts.CODE, 0);
                    jsonObject.put(Consts.MSG, "文件上传失败" + e.getMessage());
                } finally {
                    jsonObject.put("info", 0);
                }

            }
        }
        return jsonObject;
    }


    /**
     * 更新歌曲信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateSong(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id").trim();
        String name = request.getParameter("name").trim();
        String introduction = request.getParameter("introduction").trim();
        String lyric = request.getParameter("lyric").trim();

        Song song = new Song();
        song.setId(Integer.parseInt(id));
        song.setName(name);
        song.setLyric(lyric);
        song.setIntroduction(introduction);
        boolean flag = songService.update(song);

        if (flag) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "修改成功");

        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "修改失败");
        }
        return jsonObject;
    }

    /**
     * 删除歌曲
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object delete(HttpServletRequest request) {
        // TODO: 2021/7/10 先查询到数据库中对应的文件地址,删除掉它再进行下面的代码
        String id = request.getParameter("id").trim();
        boolean flag = songService.delete(Integer.parseInt(id));
        return flag;

    }


    /**
     * 更新歌曲图片
     *
     * @param avatorFile
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateSong", method = RequestMethod.POST)
    public Object updateSingerPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id") int id) {
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "文件上传失败");
        } else {
            String fileName = System.currentTimeMillis() + avatorFile.getOriginalFilename();
            String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "img"
                    + System.getProperty("file.separator") + "songPic";
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdir();
            } else {
                /*实际的文件地址*/
                File dest = new File(filePath + System.getProperty("file.separator") + fileName);
                /*存储到数据库的相对文件地址*/
                String storeAvatorPath = "/img/songPic/" + fileName;
                try {
                    avatorFile.transferTo(dest);
                    Song song = new Song();
                    song.setId(id);
                    song.setPic(storeAvatorPath);
                    boolean flag = songService.update(song);
                    if (flag) {
                        jsonObject.put(Consts.CODE, 1);
                        jsonObject.put(Consts.MSG, "文件上传成功");
                        jsonObject.put("pic", storeAvatorPath);
                    } else {
                        jsonObject.put(Consts.CODE, 0);
                        jsonObject.put(Consts.MSG, "文件上传失败");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    jsonObject.put(Consts.CODE, 0);
                    jsonObject.put(Consts.MSG, "文件上传失败" + e.getMessage());
                } finally {
                    jsonObject.put("info", 0);
                }
            }
        }

        return jsonObject;
    }

    /**
     * 更新歌曲
     *
     * @param avatorFile
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateSongUrl", method = RequestMethod.POST)
    public Object updateSingerUrl(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id") int id) {
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "文件上传失败");
        } else {
            String fileName = System.currentTimeMillis() + avatorFile.getOriginalFilename();
            String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "song";
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdir();
            } else {
                /*实际的文件地址*/
                File dest = new File(filePath + System.getProperty("file.separator") + fileName);
                /*存储到数据库的相对文件地址*/
                String storeAvatorPath = "/song/" + fileName;
                try {
                    avatorFile.transferTo(dest);
                    Song song = new Song();
                    song.setId(id);
                    song.setUrl(storeAvatorPath);
                    boolean flag = songService.update(song);
                    if (flag) {
                        jsonObject.put(Consts.CODE, 1);
                        jsonObject.put(Consts.MSG, "文件上传成功");
                        jsonObject.put("avator", storeAvatorPath);
                    } else {
                        jsonObject.put(Consts.CODE, 0);
                        jsonObject.put(Consts.MSG, "文件上传失败");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    jsonObject.put(Consts.CODE, 0);
                    jsonObject.put(Consts.MSG, "文件上传失败" + e.getMessage());
                } finally {
                    jsonObject.put("info", 0);
                }
            }
        }

        return jsonObject;
    }

    /**
     * 根据id查询歌曲
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        String songId = request.getParameter("songId");
        return songService.songOfSingerId(Integer.parseInt(songId));
    }


    /**
     * 根据id查询歌曲
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/songOfSongName", method = RequestMethod.GET)
    public Object songOfSongName(HttpServletRequest request) {
        String songName = request.getParameter("songName");
        return songService.songOfName(songName);
    }
}

对业务的处理放在了Controller层感觉不合适,应该放在Service实现类中。
System类中有这样的方法getProperties(),file.separator 文件分隔符(在 UNIX 系统中是“/”),用于处理在不同操作系统下分隔符不同的问题。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot Vue项目是一种使用Vue.js作为前端框架,SpringBoot作为后端框架的项目开发方式。在这种项目中,前端使用Vue.js来处理用户界面和交互逻辑,后端使用SpringBoot来处理业务逻辑和数据传输。通过这样的方式,前端和后端可以相对独立地进行开发和部署,并通过API进行数据传输。 在具体实现上,可以通过以下步骤来创建一个SpringBoot Vue项目: 1. 创建一个SpringBoot项目:可以使用Spring Initializr来创建一个基本的SpringBoot项目,选择所需的依赖和配置。 2. 创建前端项目:可以使用Vue CLI来创建一个基本的Vue.js项目,配置相关的开发环境和依赖。 3. 配置前后端通信:在前端项目中,可以使用axios等工具来与后端进行数据交互,发送HTTP请求获取数据。在后端项目中,可以通过编写RESTful API来提供数据给前端。 4. 开发前后端功能:根据项目需求,前端可以开发用户界面、表单验证、数据展示等功能,后端可以开发业务逻辑、数据库操作、数据处理等功能。 5. 打包和部署:前端项目可以通过npm run build命令将代码打包成静态文件,然后将这些文件放入后端项目的静态资源目录中。后端项目可以通过打包成可执行的JAR文件或者WAR文件来部署到服务器上。 通过以上步骤,就可以创建一个基于SpringBootVue.js的项目,并实现前后端的协同开发和交互。希望这些信息对你有所帮助。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [教你如何制作vue+springboot项目](https://blog.csdn.net/xc9711/article/details/121725216)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值