ํฐ์คํ ๋ฆฌ ๋ทฐ
Project/Instagram
[spring] ํด๋ก ์ฝ๋ฉ12 Post - Model, Repository, Mapper, Test, Service, Controller
yeahajeong 2020. 4. 13. 12:28๋ฐ์ํ
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๋ฅผ ์๋์ฃผ์
๋ฐ์ํ
'Project > Instagram' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[spring] ํด๋ก ์ฝ๋ฉ14 Post - ๊ฒ์๊ธ ๋ฑ๋ก (0) | 2020.04.20 |
---|---|
[spring] ํด๋ก ์ฝ๋ฉ13 Post - ๊ฒ์๊ธ ์กฐํ (0) | 2020.04.20 |
[spring] ํด๋ก ์ฝ๋ฉ11 User - ํ์ ํํด (0) | 2020.04.12 |
[spring] ํด๋ก ์ฝ๋ฉ10 User - ๋น๋ฐ๋ฒํธ ์ฐพ๊ธฐ (0) | 2020.04.09 |
[spring] ํด๋ก ์ฝ๋ฉ09 User - ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ (0) | 2020.04.09 |
๋๊ธ
๊ณต์ง์ฌํญ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
- Total
- Today
- Yesterday
๋งํฌ
TAG
- tomcat์ค์น
- Java
- ์จ๋ฆฌ์์ค
- ์ดํด๋ฆฝ์ค ์ค์น
- ์ดํด๋ฆฝ์ค ํ๊ธ ์ธ์ฝ๋ฉ
- ๊ฒ์ํ๋ง๋ค๊ธฐ
- ์๋ฃ๊ตฌ์กฐ
- ๊ฐ๋ฐํ๊ฒฝ๊ตฌ์ถ
- Algorithm
- java jdk ์ค์น
- ๋ถํธ ์๋์์ฑ
- mysql์ค์น
- ๊ฐ๋ฐ
- java ํ๊ฒฝ๋ณ์
- ๊ฒ์ํ ์กฐํ
- ์๋ฐ
- ์ ์ฒด๊ฒ์๋ฌผ ์กฐํ
- ์๊ณ ๋ฆฌ์ฆ
- typeAliases
- ๊ฒ์๋ฌผ ์ญ์
- ๊ฒ์ํ ์ญ์
- ์คํ๋ง๋ถํธ ์๋์์ฑ
- ๊ฒ์๋ฌผ์กฐํ
- ๋ณ๋ช ์ฒ๋ฆฌ
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
๊ธ ๋ณด๊ดํจ