ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

Model


PostsVO ๊ฒŒ์‹œ๊ธ€ ๋ชจ๋ธ ์ƒ์„ฑ

package com.hastagram.myapp.posts.model;

import java.sql.Date;

import org.springframework.web.multipart.MultipartFile;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString(exclude="fileData")
public class PostsVO {

	//๊ธฐ๋ณธ ํ•„๋“œ
	private int postNo;		//๊ฒŒ์‹œ๊ธ€ ๋ฒˆํ˜ธ
	private String caption;		//๋‚ด์šฉ
	private Date regDate;		//๋“ฑ๋ก์ผ
	
	private int userNo;		//์œ ์ € ๋ฒˆํ˜ธ
	private String id;		//์œ ์ € ์•„์ด๋””
	
	//ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ๋„˜์–ด์˜จ ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฝ๊ธฐ์šฉ
	private MultipartFile file;
	
	//์—…๋กœ๋“œ ํŒŒ์ผ์„ ์œ„ํ•œ ํ•„๋“œ
	private String fileName;	//ํŒŒ์ผ ์ด๋ฆ„
	private long fileSize;		//ํŒŒ์ผ ํฌ๊ธฐ
	private String fileContentType;	//ํŒŒ์ผ ํƒ€์ž…
	private byte[] fileData;	//์‹ค์ œ ๋ฐ์ดํ„ฐ
	
	
	private String profileName; 	//ํ”„๋กœํ•„ ์‚ฌ์ง„ ์กฐํšŒ๋ฅผ ์œ„ํ•œ ํ•„๋“œ
}

๊ฒŒ์‹œ๊ธ€์˜ ์ •๋ณด๋ฅผ ๋‹ด์„ VO๊ฐ์ฒด ์ƒ์„ฑ. ๊ฒŒ์‹œ๊ธ€์—๋„ ์ด๋ฏธ์ง€ ํŒŒ์ผ์ด ๋“ค์–ด๊ฐ€๋‹ˆ๊นŒ ์—…๋กœ๋“œ ํŒŒ์ผ์„ ์œ„ํ•œ ํ•„๋“œ๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค. 

 

 

 

Repository


IPostDAO

package com.hastagram.myapp.posts.repository;

import java.util.List;

import com.hastagram.myapp.posts.model.PostsVO;

public interface IPostsDAO {
	
	//๊ฒŒ์‹œ๋ฌผ ๋“ฑ๋ก ๊ธฐ๋Šฅ(์‚ฌ์ง„ ์ฒจ๋ถ€ํŒŒ์ผ ํฌํ•จ)
	void insertPost(PostsVO post) throws Exception;
	
	//๊ฒŒ์‹œ๋ฌผ ์‚ญ์ œ
	void deletePost(int postNo) throws Exception;
	
	//๊ฒŒ์‹œ๋ฌผ ๋ชฉ๋ก ์กฐํšŒ ๊ธฐ๋Šฅ(๋ชจ๋“  ์‚ฌ์ง„)
	List<PostsVO> selectPostLiest() throws Exception;
	
	//์ฒจ๋ถ€ํŒŒ์ผ์„ DB์—์„œ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ธฐ๋Šฅ
	PostsVO getFile(int postNo) throws Exception;
	
	//์•„์ด๋””๋กœ ํšŒ์›๋ฒˆํ˜ธ ์กฐํšŒํ›„ ๋ฒˆํ˜ธ๋กœ ๋ชจ๋“  ๊ฒŒ์‹œ๋ฌผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
	List<PostsVO> selectPostListById(String id) throws Exception;

	//ํšŒ์› ํƒˆํ‡ด์‹œ ์œ ์ €์˜ ๊ฒŒ์‹œ๋ฌผ ์‚ญ์ œ
	void deleteUserAllPost(int userNo) throws Exception;
}
  • ๊ฒŒ์‹œ๋ฌผ ๋“ฑ๋ก ๊ธฐ๋Šฅ
  • ๊ฒŒ์‹œ๋ฌผ ์‚ญ์ œ ๊ธฐ๋Šฅ
  • ๊ฒŒ์‹œ๋ฌผ ๋ชฉ๋ก ์กฐํšŒ ๊ธฐ๋Šฅ
  • ์ฒจ๋ถ€ํŒŒ์ผ์„ DB์—์„œ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ธฐ๋Šฅ
  • ์•„์ด๋””๋กœ ํšŒ์› ๋ฒˆํ˜ธ๋ฅผ ์กฐํšŒ ํ›„ ํšŒ์› ๋ฒˆํ˜ธ๋กœ ๋ชจ๋“  ๊ฒŒ์‹œ๋ฌผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
  • ํŠน์ •์œ ์ €์˜ ๊ฒŒ์‹œ๋ฌผ ๋ชจ๋‘ ์‚ญ์ œ (ํšŒ์›ํƒˆํ‡ด์‹œ ์‚ฌ์šฉ)

 

 

 

Mapper


PostsMapper

<?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.hastagram.myapp.posts.repository.IPostsDAO">

	<!-- ResultMap ์ž‘์„ฑ -->
	<resultMap type="com.hastagram.myapp.posts.model.PostsVO" id="PostResultMap">
		<id property="postNo" column="post_no"/>
		<result property="caption" column="caption"/>
		<result property="regDate" column="reg_date"/>
		<result property="userNo" column="user_no"/>
		<result property="id" column="id"/>
		
	   	<result property="fileName" column="file_name" />
	   	<result property="fileSize" column="file_size" />
	   	<result property="fileContentType" column="file_content_type" />
	   	<result property="fileData" column="file_data" />
	   	
	   	<result property="profileName" column="profile_name" />
	</resultMap>
	
	<resultMap type="com.hastagram.myapp.users.model.UserImgsVO" id="UserImgsResultMap">
 		<id property="userImgNo" column="user_img_no" />
 		<result property="userNo" column="user_no" />
 		<result property="profileName" column="file_name" />
 		<result property="profileSize" column="file_size" />
 		<result property="profileContentType" column="file_content_type" />
 		<result property="profileData" column="file_data" />
 	</resultMap>
	
	
	<!-- ๊ฒŒ์‹œ๋ฌผ ๋“ฑ๋ก๊ณผ ํŒŒ์ผ ์ฒจ๋ถ€ SQL -->
	<insert id="insertPost">
		insert into posts(caption, user_no, id, file_name, file_size, file_content_type, file_data) values(#{caption}, #{userNo}, #{id}, #{fileName}, #{fileSize}, #{fileContentType}, #{fileData})
	</insert>
	
	<!-- ๊ฒŒ์‹œ๋ฌผ ์‚ญ์ œ SQL -->
	<delete id="deletePost">
		delete from posts where post_no=#{postNo}
	</delete>
	
	<!-- ๋ชจ๋“  ๊ฒŒ์‹œ๋ฌผ ์กฐํšŒ SQL -->
	<select id="selectPostLiest" resultMap="PostResultMap">
		<![CDATA[
		select 
			post_no, 
			caption, 
			reg_date, 
			P.user_no as user_no, 
			id, file_name, 
			file_size, 
			file_content_type, 
			file_data, 
			I.profile_name as profile_name
		from posts P 
		left outer join user_imgs I 
		on (P.user_no = I.user_no) 
		where post_no > 0 
		order by post_no desc;
		]]>
	</select>
	
	<!-- ํŒŒ์ผ ์กฐํšŒ SQL -->
	<select id="getFile" resultMap="PostResultMap">
		select * from posts where post_no = #{postNo}
	</select>
	
	<!-- ์•„์ด๋””๋กœ ์œ ์ € ๋ฒˆํ˜ธ๋ฅผ ์กฐํšŒํ•ด์„œ ๊ทธ ๋ฒˆํ˜ธ๋กœ ๊ฒŒ์‹œ๋ฌผ ๋ชจ๋‘ ์กฐํšŒ SQL -->
	<select id="selectPostListById" resultMap="PostResultMap">
		select * from posts where user_no = (select user_no from users where id=#{id}) order by post_no desc
	</select>
	
	<!-- ํšŒ์› ํƒˆํ‡ด์‹œ ๊ฒŒ์‹œ๋ฌผ ์‚ญ์ œ -->
	<delete id="deleteUserAllPost">
		delete from posts where user_no=#{userNo}
	</delete>
	
	
</mapper>

 

mvc-config.xml ์— ๋นˆ๊ฐ์ฒด๋กœ ๋“ฑ๋ก

<mybatis-spring:scan base-package="com.hastagram.myapp.posts.repository"/>

 

 

 

Test


JunitTest

package com.hastagram.myapp.poststest;

import javax.inject.Inject;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.hastagram.myapp.posts.model.PostsVO;
import com.hastagram.myapp.posts.repository.IPostsDAO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:/spring/mvc-config.xml"})
public class PostsDAOTest {

	//DAO ๊ฐ์ฒด
	@Inject
	private IPostsDAO postDAO;
	
	//๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก ํ…Œ์ŠคํŠธ
	@Test
	public void insertTest() throws Exception{
		for(int i=1; i<=10; i++) {
			
			byte[] fileData = new byte[1024];
			
			PostsVO post = new PostsVO();
			post.setCaption(i + "๋ฒˆ์งธ post");
			post.setUserNo(i);
			post.setFileName("care.jpg");
			post.setFileSize(379228);
			post.setFileContentType("image/jpeg");
			post.setFileData(fileData);
			
			postDAO.insertPost(post);
		}
	}
	
	//๊ฒŒ์‹œ๋ฌผ ์‚ญ์ œ ํ…Œ์ŠคํŠธ
	@Test
	public void deleteTest() throws Exception {
		postDAO.deletePost(7);
	}
	
	
}

 

 

 

Service


IPostsService

package com.hastagram.myapp.posts.service;

import java.util.List;

import com.hastagram.myapp.posts.model.PostsVO;

public interface IPostsService {

	//๊ฒŒ์‹œ๋ฌผ ๋“ฑ๋ก ๊ธฐ๋Šฅ(์‚ฌ์ง„ ์ฒจ๋ถ€ํŒŒ์ผ ํฌํ•จ)
	void insertPost(PostsVO post) throws Exception;
	
	//๊ฒŒ์‹œ๋ฌผ ์‚ญ์ œ
	void deletePost(int postNo) throws Exception;
	
	//๊ฒŒ์‹œ๋ฌผ ๋ชฉ๋ก ์กฐํšŒ ๊ธฐ๋Šฅ(๋ชจ๋“  ์‚ฌ์ง„)
	List<PostsVO> selectPostLiest() throws Exception;
	
	//์ฒจ๋ถ€ํŒŒ์ผ์„ DB์—์„œ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ธฐ๋Šฅ
	PostsVO getFile(int postNo) throws Exception;
	
	//์•„์ด๋””๋กœ ํšŒ์›๋ฒˆํ˜ธ ์กฐํšŒํ›„ ๋ฒˆํ˜ธ๋กœ ๋ชจ๋“  ๊ฒŒ์‹œ๋ฌผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
	List<PostsVO> selectPostListById(String id) throws Exception;

}

 

 

 

 

Controller


PostsController

@Controller
@RequestMapping("/post")
public class PostsController {
	
	private static final Logger logger = LoggerFactory.getLogger(PostsController.class);
	
	
	@Autowired
	private IPostsService postsService;
	@Autowired
	private IUsersService usersService;
	

๊ณตํ†ต URL -> /post

postsService์™€ userService๋ฅผ ์ž๋™์ฃผ์ž…

 

 

 

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€