Java项目实战系列——我的b站(IEDA抽取方法快捷键真的强!!!)

157 阅读4分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第1天,点击查看活动详情

视频链接

www.bilibili.com/video/BV1ur…

1.数据类设计

包结构以及前端资源

image.png

Java Bean:

Video和Play

Video:

package com.miles.module5.dto;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : Wzq
 * @version : 1.0
 * @Project : HeiMa-Project1
 * @Package : com.miles.module5.dto
 * @ClassName : Video.java
 * @createTime : 2022/8/6 11:54
 * @Email : zqwang21@163.com
 * @Description :
 */
public class Video {
    private String bv;//视频编号
    private String title;
    private LocalDateTime publishTime;
    private String cover;//封面
    private String introduction;
    private List<String> tagList;
    private List<Play> playList;
    private String type;//转载or原创
    private String category;//分区


    public Video(String bv, String title, LocalDateTime publishTime, String cover, String introduction, List<String> tagList, List<Play> playList, String type, String category) {
        this.bv = bv;
        this.title = title;
        this.publishTime = publishTime;
        this.cover = cover;
        this.introduction = introduction;
        this.tagList = tagList;
        this.playList = playList;
        this.type = type;
        this.category = category;
    }

    public String getBv() {
        return bv;
    }

    public void setBv(String bv) {
        this.bv = bv;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public LocalDateTime getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(LocalDateTime publishTime) {
        this.publishTime = publishTime;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public List<String> getTagList() {
        return tagList;
    }

    public void setTagList(List<String> tagList) {
        this.tagList = tagList;
    }

    public List<Play> getPlayList() {
        return playList;
    }

    public void setPlayList(List<Play> playList) {
        this.playList = playList;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }
}

Play:

public class Play {
    private String id;//视频编号
    private String title;
    private LocalTime duration;
    private String url;

    public Play() {
    }

    public Play(String id, String title, LocalTime duration, String url) {
        this.id = id;
        this.title = title;
        this.duration = duration;
        this.url = url;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public LocalTime getDuration() {
        return duration;
    }

    public void setDuration(LocalTime duration) {
        this.duration = duration;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

相关知识点

日期时间类: LocalDateTime-年月日时分秒,LocalTime-时分秒,LocalDate-年月日

1.获取当前时间

2.方便加减

3.把字符串表示的时间转换为对象时间

代码Demo:

public class TestDateTime {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.of(2022,8,6,12,12,35);
        LocalTime localTime = LocalTime.of(11,22,33);
        System.out.println(localDateTime.plusDays(21));
        System.out.println(localTime.plusHours(10));
        System.out.println(LocalDateTime.now());
        String dateTime = "2022-08-27T12:12:35";
        LocalDateTime l = LocalDateTime.parse(dateTime);
        System.out.println(l.getYear());
        System.out.println(l.getMonth());
        System.out.println(l.getDayOfMonth());
    }
}

2.数据展示

playList: image.png

代码实现:

List<Play> plays = List.of(
        new Play("P1", "二分查找-演示", LocalTime.parse("00:05:46"), "1_1.mp4"),
        new Play("P2", "二分查找-实现", LocalTime.parse("00:06:47"), "1_2.mp4")
);
List<Play> plays = List.of(
        new Play("P1", "Jva中的线程技术", LocalTime.parse("00:09:45"), "2_1.mp4"),
        new Play("P2", "代码演示1", LocalTime.parse("00:07:05"), "2_2.mp4"),
        new Play("P3", "代码演示2", LocalTime.parse("00:05:01"), "2_3.mp4")
);

VideoController:

package com.miles.module6.controller;


import com.miles.module6.dto.Play;
import com.miles.module6.dto.Video;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : Wzq
 * @version : 1.0
 * @Project : HeiMa-Project1
 * @Package : com.miles.module5.controller
 * @ClassName : VideoController.java
 * @createTime : 2022/8/6 12:22
 * @Email : zqwang21@163.com
 * @Description :
 */
@Controller
public class VideoController {
    @RequestMapping("/video/1")
    @ResponseBody
    public Video t1(){
        List<Play> plays = List.of(
                new Play("P1", "二分查找-演示", LocalTime.parse("00:05:46"), "1_1.mp4"),
                new Play("P2", "二分查找-实现", LocalTime.parse("00:06:47"), "1_2.mp4")
        );
        return new Video("1","面试专题-基础篇", LocalDateTime.now(),"1.png","祝你面试游刃有余",
                List.of("面试","Java","计算机技术"),plays,"自制","科技-计算机技术");
    }
    @RequestMapping("/video/2")
    @ResponseBody
    public Video t2(){
        List<Play> plays = List.of(
                new Play("P1", "Jva中的线程技术", LocalTime.parse("00:09:45"), "2_1.mp4"),
                new Play("P2", "代码演示1", LocalTime.parse("00:07:05"), "2_2.mp4"),
                new Play("P3", "代码演示2", LocalTime.parse("00:05:01"), "2_3.mp4")
        );
        return new Video("2","面试专题-并发篇", LocalDateTime.now(),"2.png","祝你面试游刃有余",
                List.of("面试","Java","计算机技术"),plays,"自制","科技-计算机技术");
    }
}

重启服务器,查看页面效果:

image.png

image.png

3.路径参数

image.png

多个路径映射到一个方法

@RequestMapping("/video/{bv}")

2.@PathVariable:表示该方法参数要从路径中获得

@PathVariable String bv

代码改进:

@Controller
public class VideoController {

    @RequestMapping("/video/{bv}")
    @ResponseBody
    public Video t(@PathVariable String bv) {
        if (bv.equals("1")) {
            List<Play> plays = List.of(
                    new Play("P1", "二分查找-演示", LocalTime.parse("00:05:46"), "1_1.mp4"),
                    new Play("P2", "二分查找-实现", LocalTime.parse("00:06:47"), "1_2.mp4")
            );
            return new Video("1", "面试专题-基础篇", LocalDateTime.now(), "1.png", "祝你面试游刃有余",
                    List.of("面试", "Java", "计算机技术"), plays, "自制", "科技-计算机技术");

        }
        if (bv.equals("2")) {
            List<Play> plays = List.of(
                    new Play("P1", "Jva中的线程技术", LocalTime.parse("00:09:45"), "2_1.mp4"),
                    new Play("P2", "代码演示1", LocalTime.parse("00:07:05"), "2_2.mp4"),
                    new Play("P3", "代码演示2", LocalTime.parse("00:05:01"), "2_3.mp4")
            );
            return new Video("2", "面试专题-并发篇", LocalDateTime.now(), "2.png", "祝你面试游刃有余",
                    List.of("面试", "Java", "计算机技术"), plays, "自制", "科技-计算机技术");
        }
        return null;
    }
}

3.读取文件数据

目前代码缺点:

1.数据是写死的

2.把现有代码转到service中

数据文件:

image.png image.png

image.png

相关知识点

读取文件数据 Files

代码Demo:

public class TestFile {
    public static void main(String[] args) throws IOException {
        List<String> data = Files.readAllLines(Path.of("data", "p.csv"));
        for (String line : data) {
            System.out.println(line);
        }
    }
}

运行结果:

image.png

分割字符串 split后存到数组

public class TestFile {
    public static void main(String[] args) throws IOException {
        List<String> data = Files.readAllLines(Path.of("data", "p.csv"));
        for (String line : data) {
            //System.out.println(line);
            String[] s = line.split(",");
            System.out.println(s[0]);
        }
    }
}

业务逻辑类设计

VideoService:

查询:根据视频编号,查询Video对象

@Service
public class VideoService1 {
    //查询:根据视频编号,查询Video对象
    public Video find(String bv){
        try {
            List<String> data = Files.readAllLines(Path.of("data", "p.csv"));
            for (String line : data) {
                String[] s = line.split(",");
                if (s[0].equals(bv)) {
                    String[] tags = s[7].split("_");
                    //读取选集文件
                    List<String> vdata = Files.readAllLines(Path.of("data", "v_" + bv + ".csv"));
                    List<Play> list = new ArrayList<>();

                    for (String vline : vdata) {
                        String[] ss = vline.split(",");
                        list.add(new Play(ss[0],ss[1], LocalTime.parse(ss[3]),ss[2]));
                    }
                    return new Video(s[0], s[3], LocalDateTime.parse(s[6]), s[4], s[5], Arrays.asList(tags), list, s[1], s[2]);
                }
            }
            return null;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

相关知识点

1.读取文件

2.slpit方法

3.字符串拼接

VideoController:

public class VideoController {

    @RequestMapping("/video/{bv}")
    @ResponseBody
    public Video t(@PathVariable String bv) throws IOException {
       
        return videoService1.find(bv);
    }
    @Autowired
    private VideoService1 videoService1;
}

相关知识点

1.@PathVariable

2.@Autowired 依赖注入

页面效果:

image.png

IDEA抽取方法快捷键

ctrl+Alt+M

文件读取时机

当前问题:

每次请求服务时,都会重新读一遍所有文件

改进:编写一个初始化函数,把需要读取的文件预先读取,这样只需读取一遍即可

关键字:@PostConstructor

改进后代码:

@Service
public class VideoService1 {
    @PostConstruct
    public void init(){
        try {
            List<String> data = Files.readAllLines(Path.of("data", "p.csv"));
            for (String line : data) {
                String[] s = line.split(",");
                    String[] tags = s[7].split("_");
                    List<Play> list = getPlayList(s[0]);
                    Video video =new Video(s[0], s[3], LocalDateTime.parse(s[6]), s[4], s[5], Arrays.asList(tags), list, s[1], s[2]);
                    map.put(s[0],video);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    Map<String,Video> map = new HashMap<>();
    //查询:根据视频编号,查询Video对象
    public Video find(String bv){
        Video video=map.get(bv);
        return video;
    }
    //读取选集文件
    private List<Play> getPlayList(String bv) {
        try {
            List<String> vdata = Files.readAllLines(Path.of("data", "v_" + bv + ".csv"));
            List<Play> list = new ArrayList<>();

            for (String vline : vdata) {
                String[] ss = vline.split(",");
                list.add(new Play(ss[0],ss[1], LocalTime.parse(ss[3]),ss[2]));
            }
            return list;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

相关知识点

1.@PostConstructor

2.用map作为桥梁,声明一个<String,Video>类型的map,在获取文件数据后,存入到map中,然后通过map的get方法,实现根据视频编号,查询Video对象的效果