본문 바로가기
Programming/Spring

[Spring] XML Mapper로 DB에서 정보 조회하는 기능 구현 (Controller, DAO, DTO, Service, Mybatis)

by z.1nee 2021. 3. 2.
SMALL

MVC 구조 알아보기 

jione-e.tistory.com/122

 

[Spring] Spring Framework MVC - Controller, Service, DTO(=VO), DAO

Sping Framework MVC 모델 Controller : 클라이언트(Client)에서 요청(Request)이 들어올 때, 해당 요청을 수행할 비즈니스 로직을 제어(Handling)하는 객체 Service : 서비스 레이어 단에서 세분화된 비즈니스..

jione-e.tistory.com

 

 

 

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 입력해 테스트

 

댓글