SMALL
MVC 구조 알아보기
DB 구축
MySQL에 DB CREATE하고, 데이터 INSERT
* aid는 artist 고유 번호 (1=IU, 2=BTS, 3=오마이걸)
DTO 구현
[VideoDTO.java]
import lombok.Data;
@Data
public class VideoDTO {
private int aid;
private int vidid;
private String vid_url;
private String vid_name;
private String thumb;
}
* lombok
jione-e.tistory.com/120?category=1005369
DAO 인터페이스
[VideoDAO.java]
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import com.genie.demo.dto.VideoDTO;
public interface VideoDAO {
public abstract List<VideoDTO> GetVideoList(SqlSessionTemplate session,int aid);
}
DAO 구현
[VideoDAOImpl.java]
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import com.genie.demo.dto.VideoDTO;
@Repository
public class VideoDAOImpl implements VideoDAO {
@Override
public List<VideoDTO> GetVideoList(SqlSessionTemplate session,int aid) {
System.out.println("dao");
return session.selectList("getVideo",aid);
}
}
Mybatis Mapper
[mybatis-config.xml]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="mappers/userMapper.xml"/>
<mapper resource="mappers/PhotoMapper.xml"/>
<mapper resource="mappers/VideoMapper.xml"/>
</mappers>
</configuration>
[VideoMaper.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 = "VideoMapper">
<!--데이터 조회 -->
<select id="getVideo" parameterType="int" resultType="com.genie.demo.dto.VideoDTO">
SELECT *
from media_vid
WHERE aid = #{aid}
</select>
</mapper>
Service 인터페이스
[VideoService.java]
import java.util.List;
import com.genie.demo.dto.VideoDTO;
public interface VideoService {
public abstract List<VideoDTO> GetVideoList(int aid);
}
Service 구현
[VideoServiceImpl.java]
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.genie.demo.dao.VideoDAO;
import com.genie.demo.dto.VideoDTO;
@Service
public class VideoServiceImpl implements VideoService {
@Autowired
VideoDAO dao;
@Autowired
SqlSessionTemplate session;
@Override
public List<VideoDTO> GetVideoList(int aid) {
System.out.println("service");
return dao.GetVideoList(session,aid);
}
}
Cotroller 구현
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.genie.demo.dto.VideoDTO;
import com.genie.demo.service.VideoService;
@RequestMapping("/")
@Controller
public class MediaController {
@Autowired
private VideoService videoService;
@GetMapping("/{aid}/media")
public String media(@PathVariable(value="aid") int aid, Model model) throws Exception{
List<VideoDTO> videoList = videoService.GetVideoList(aid);
for(VideoDTO item2 : videoList) {
System.out.println(item2);
}
model.addAttribute("videoList", videoList);
return "";
}
}
Postman에서 URL 입력해 테스트
'Programming > Spring' 카테고리의 다른 글
[Spring] Jsoup을 이용한 지니 뮤직비디오 웹 크롤링 (Jsoup select 문법) (0) | 2021.02.26 |
---|---|
[Spring] Spring Framework MVC - Controller, Service, DTO(=VO), DAO (0) | 2021.02.23 |
[Spring] STS에 Lombok 설치하기 (0) | 2021.02.23 |
[Spring] 스프링으로 게시판 만들기 (2) | 2021.02.22 |
[Spring] 맥(Mac)에 STS(Spring Tool Suite) 설치하기 (0) | 2021.02.22 |
댓글