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

๋ฐ˜์‘ํ˜•

PostMapper.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="com.hastagram.myapp.post.repository.IPostDAO">

	<!-- ResultMap ์ž‘์„ฑ -->
	<resultMap type="com.hastagram.myapp.post.model.PostVO" 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="fileId" column="file_id" />
	   <result property="fileName" column="file_name" />
	   <result property="fileSize" column="file_size" />
	   <result property="fileContentType" column="file_content_type" />
	</resultMap>
	
	<resultMap type="com.hastagram.myapp.users.model.UsersVO" id="UsersResultMap">
		<id property="userNo" column="user_no"/>
		<result property="id" column="id"/>
	</resultMap>
	
	
	<!-- ๊ฒŒ์‹œ๋ฌผ ๋“ฑ๋ก๊ณผ ํŒŒ์ผ ์ฒจ๋ถ€ -->
	<insert id="insertPost">
		insert into posts(caption, user_no, file_name, file_size, file_content_type, file_data) 
		values(#{caption}, #{userNo}, #{fileName}, #{fileSize}, #{fileContentType}, #{fileData})
	</insert>
	
</mapper>

 

IPostDAO.java

package com.hastagram.myapp.post.repository;

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

public interface IPostDAO {
	
	//๊ฒŒ์‹œ๋ฌผ ๋“ฑ๋ก(์‚ฌ์ง„ ์ฒจ๋ถ€ํŒŒ์ผ ํฌํ•จ)
	void insertPost(PostsVO post) throws Exception;

}

 

IPostsDAOTest.java

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);
		}
	}
	
}

 

ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๋Š”๋ฐ ์ด๋Ÿฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ๊ทธ ์ „์— ๋‹ค๋ฅธ ์˜ค๋ฅ˜๋„ ์žˆ์—ˆ๋Š”๋ฐ ์˜คํƒ€๋กœ ์ธํ•œ ์˜ค๋ฅ˜๋“ค์ด์–ด์„œ ์ˆ˜์ •ํ–ˆ๋‹ค.

Cannot add or update a child row: ์–ด์ฉŒ๊ณ ์ €์ฉŒ๊ณ 

์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ์— ๋”ฐ๋ผ ์ฐธ์กฐํ‚ค๋Š” ํ•ญ์ƒ ๋ถ€๋ชจํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’๋งŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. ์ฐธ์กฐ ๋ฐ›๋Š” ํ…Œ์ด๋ธ”(posts)์— ๋จผ์ € ๊ฐ’์„ ๋„ฃ์–ด์„œ ์ƒ๊ธฐ๋Š” ์˜ค๋ฅ˜๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์šฐ์„  ์ฐธ์กฐ ์ œ์•ฝ ์กฐ๊ฑด์„ ์ง€์šฐ๊ณ  ํ…Œ์ŠคํŠธ๋ฅผ ํ–ˆ๋”๋‹ˆ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌด์‚ฌํžˆ ๋“ค์–ด๊ฐ”๋‹ค. ๊ทผ๋ฐ ์ƒ๊ฐ์„ ํ•ด๋ณด๋‹ˆ ๊ตณ์ด ๋‘ ํ…Œ์ด๋ธ”๊ฐ„์— ์ฐธ์กฐ ๊ด€๊ณ„๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์„ ํ–ˆ๋‹ค. ํšŒ์›์˜ ๋ชจ๋“  ์ •๋ณด๊ฐ€ ํ•„์š”ํ•œ๊ฒŒ ์•„๋‹ˆ๊ณ  ํšŒ์› ์•„์ด๋””๋งŒ ํ•„์š”ํ•˜๋‹ˆ๊นŒ ๊ทธ๋ƒฅ user_no๋กœ ์•„์ด๋””๋งŒ ์กฐํšŒํ•ด๋„ ๋˜์ง€ ์•Š์„๊นŒ.

JUnit์œผ๋กœ Test
๋“ค์–ด๊ฐ€๊ธดํ–ˆ๋Š”๋ฐ post_no์ด ์™œ 4๋ฒˆ๋ถ€ํ„ฐ ๋“ค์–ด๊ฐ”์ง€;

 

 

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