ํฐ์คํ ๋ฆฌ ๋ทฐ
					Project/Instagram
					
				[spring] ํด๋ก ์ฝ๋ฉ16 Follow - Model, Repository, Mapper, Test, Service, Controller
yeahajeong 2020. 4. 20. 19:52๋ฐ์ํ
    
    
    
  ํ์คํ๊ทธ๋จ์ ๋ง๋ค๋ฉด์ ๊ฐ์ฅ ๋จธ๋ฆฌ๊ฐ ์ํ ๋ ๋ถ๋ถ์ด ํ๋ก์ฐ ๋ถ๋ถ์ด์๋ค. ํ์์ด๋ ๊ฒ์๊ธ ๊ฐ์ ๊ฒฝ์ฐ ๋ค๋ฅธ ๊ฒ์ํ ํ์ด์ง์ ํ์ด ๋น์ทํด์ ์๊ฐ๋ณด๋ค ์์ํ๊ฒ ๋ง๋ค์ ์์์ง๋ง ํ๋ก์ฐ๋ DB๊ตฌ์ฑ๋ ์ด๋ป๊ฒ ํด์ผํ ์ง ๋ง๋งํ๋ค.
Model
FollowVO
@Setter
@Getter
@ToString
public class FollowVO {
	private int followNo;
	private int activeUser;
	private int passiveUser;
	private Date regDate;
	
	private String activeUserId;
	private String passiveUserId;
	
	private String profileName;
	
}์ฐ์ DB์๋ ํ๋ก์ฐ๋ฒํธ, ํ๋ก์ฐ๊ฑด์ ์ , ํ๋ก์ฐ๋นํ์ ์ , ํ๋ก์ฐ๋ฑ๋ก๋ ์ง๋ฅผ ๋ง๋ค์๊ณ , ํ๋ก ํธ์์ ํ๋ก์ฐ๋ฆฌ์คํธ๋ฅผ ํ์ธํ ๋ ์ ์ ์ ์์ด๋์ ํ๋กํ ์ฌ์ง์ ๋ณด์ด๊ธฐ ์ํด ํ๋๋ฅผ ์ถ๊ฐํด ๋์๋ค.
Repository
IFollowDAO
public interface IFollowDAO {
	//ํ๋ก์ฐ ํ๊ธฐ
	void follow(FollowVO follow);
	
	//์ธํ๋ก์ฐ ํ๊ธฐ
	void unfollow(FollowVO follow);
	
	//ํ๋ก์ฐ ์ ๋ฌด
	int isFollow(FollowVO follow);
	
	//ํ๋ก์ฐ ๋ฆฌ์คํธ ์กฐํ
	List<FollowVO> selectActiveUserList(int activeUser);
	
	//ํ๋ก์ ๋ฆฌ์คํธ ์กฐํ
	List<FollowVO> selectPassiveUserList(int passiveUser);
	
	//ํํด์ ํ๋ก์ฐ์ญ์ 
	void deleteUserAllFollow(int activeUser);
}- ํ๋ก์ฐํ๊ธฐ
- ์ธํ๋ก์ฐํ๊ธฐ
- ํ๋ก์ฐ ์ ๋ฌด ํ์ธ
- ํ๋ก์ฐ ๋ฆฌ์คํธ ์กฐํ
- ํ๋ก์ ๋ฆฌ์คํธ ์กฐํ
- ํํด์ ํ๋ก์ฐ ์ ์ฒด ์ญ์ 
Mapper
FollowMapper.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.follow.repository.IFollowDAO">
	<resultMap type="com.hastagram.myapp.follow.model.FollowVO" id="FollowResultMap">
		<id property="followNo" column="follow_no" />
		<result property="activeUser" column="active_user" />
		<result property="passiveUser" column="passive_user" />
		<result property="regDate" column="reg_date"/>
		
		<result property="activeUserId" column="active_user_id" />
		<result property="passiveUserId" column="passive_user_id" />
		
		<result property="profileName" column="profile_name" />
	</resultMap>
	
	<resultMap type="com.hastagram.myapp.users.model.UsersVO" id="UsersResultMap">
		<id property="userNo" column="user_no" />
		<result property="email" column="email"/>
		<result property="id" column="id"/>
		<result property="pw" column="pw"/>
		<result property="name" column="name"/>
		<result property="intro" column="intro"/>
		<result property="phone" column="phone"/>
		<result property="regDate" column="regDate"/>
	</resultMap>
	<!-- ํ๋ก์ฐ ๊ธฐ๋ฅ -->
	<insert id="follow">
		insert into follow(active_user, passive_user) values(#{activeUser},#{passiveUser})
	</insert>
	
	<!-- ์ธํ๋ก์ฐ ๊ธฐ๋ฅ -->
	<delete id="unfollow">
		delete from follow where active_user=#{activeUser} and passive_user=#{passiveUser}
	</delete>
	
	<!-- ํ๋ก์ฐ ์ ๋ฌด ์กฐํ ๊ธฐ๋ฅ -->
	<select id="isFollow" resultType="int">
		select count(*) from follow where active_user=#{activeUser} and passive_user=#{passiveUser}
	</select>
	
	<!-- ํ๋ก์ฐ ๋ฆฌ์คํธ ์กฐํ -->
	<select id="selectActiveUserList" resultMap="FollowResultMap">
		select 
			active_user,
		    passive_user,
		    F.reg_date as reg_date,
		    U.id as passive_user_id,
		    I.profile_name as profile_name
		from follow F 
		left outer join users U 
		on (F.passive_user = U.user_no)
		left outer join user_imgs I
		on (I.user_no = U.user_no)
		where active_user=#{activeUser}
		order by reg_date desc
	</select>
	
	<!-- ํ๋ก์ ๋ฆฌ์คํธ ์กฐํ -->
	<select id="selectPassiveUserList" resultMap="FollowResultMap">
		select 
			active_user,
		    passive_user,
		    F.reg_date as reg_date,
		    U.id as active_user_id,
		    I.profile_name as profile_name
		from follow F 
		left outer join users U 
		on (F.active_user = U.user_no) 
		left outer join user_imgs I
		on (I.user_no = U.user_no)
		where passive_user=#{passiveUser}
		order by reg_date desc;
	</select>
	
	<!-- ํํด์ ํ๋ก์ฐ ์ญ์  -->
	<delete id="deleteUserAllFollow">
		delete from follow where active_user=#{active_user}
	</delete>
	
</mapper>
mvc-config.xml
<mybatis-spring:scan base-package="com.hastagram.myapp.follow.repository"/>
JUnitTest
FollowDAOTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:/spring/mvc-config.xml"})
public class FollowDAOTest {
	@Autowired
	private IFollowDAO followDAO;
	
	//ํ๋ก์ฐ ํ
์คํธ
	@Test
	public void followTest() throws Exception {
		FollowVO vo = new FollowVO();
		
		vo.setActiveUser(2);
		vo.setPassiveUser(4);
		
		followDAO.follow(vo);
		
	}
	
	//์ธํ๋ก์ฐ ํ
์คํธ
	@Test
	public void unfollowTest() throws Exception {
		
		FollowVO vo = new FollowVO();
		
		vo.setActiveUser(2);
		vo.setPassiveUser(4);
		
		followDAO.unfollow(vo);
		
		
	}
	
	//๋งํ ์ ๋ฌด ํ
์คํธ
	@Test
	public void isfollowTest() throws Exception {
		
		FollowVO vo = new FollowVO();
		
		vo.setActiveUser(2);
		vo.setPassiveUser(4);
		
		int result = followDAO.isFollow(vo);
		
		if(result == 0) {
			System.out.println("ํ๋ก์ฐ ๋์ด์์ง ์์");
		} else { 
			System.out.println("ํ๋ก์ฐ ๋์ด์์");
		}
	}
	
	//ํ๋ก์, ํ๋ก์ ๋ฆฌ์คํธ ์กฐํ
	@Test
	public void selectList() throws Exception {
		List<FollowVO> follower = followDAO.selectPassiveUserList(3);
		List<FollowVO> following = followDAO.selectActiveUserList(3);
		
		System.out.println(follower);
		System.out.println(following);
	}
	
}
Service
IFollowService
public interface IFollowService {
	//ํ๋ก์ฐ ํ๊ธฐ
	void follow(FollowVO follow);
	
	//์ธํ๋ก์ฐ ํ๊ธฐ
	void unfollow(FollowVO follow);
	
	//ํ๋ก์ฐ ์ ๋ฌด
	int isFollow(FollowVO follow);
	
	//ํ๋ก์ฐ ๋ฆฌ์คํธ ์กฐํ
	List<FollowVO> selectActiveUserList(int activeUser);
	
	//ํ๋ก์ ๋ฆฌ์คํธ ์กฐํ
	List<FollowVO> selectPassiveUserList(int passiveUser);
	
}
Controller
FollowController
@RestController
public class FollowController {
	private static final Logger logger = LoggerFactory.getLogger(UsersController.class);
	
	@Autowired
	private IFollowService followService;
	@Autowired
	private IUsersService usersService;๊ณตํต URL ์์
๋ฐ์ํ
    
    
    
  'Project > Instagram' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [spring] ํด๋ก ์ฝ๋ฉ 18 Follow - ํ๋ก์, ํ๋ก์ ๋ฆฌ์คํธ ์กฐํ (0) | 2020.04.21 | 
|---|---|
| [spring] ํด๋ก ์ฝ๋ฉ17 Follow - ํ๋ก์ฐ, ์ธํ๋ก์ฐ (0) | 2020.04.20 | 
| [spring] ํด๋ก ์ฝ๋ฉ15 Post - ๊ฒ์๊ธ ์ญ์  (0) | 2020.04.20 | 
| [spring] ํด๋ก ์ฝ๋ฉ14 Post - ๊ฒ์๊ธ ๋ฑ๋ก (0) | 2020.04.20 | 
| [spring] ํด๋ก ์ฝ๋ฉ13 Post - ๊ฒ์๊ธ ์กฐํ (0) | 2020.04.20 | 
					๋๊ธ
						
					
					
					
				
			
										๊ณต์ง์ฌํญ
										
								
							
								
								
									์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
									
							
								
								
									์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
									
							
								
								- Total
- Today
- Yesterday
									๋งํฌ
									
							
								
								
									TAG
									
							
								
								- ๊ฐ๋ฐ
- ์๋ฐ
- ์คํ๋ง๋ถํธ ์๋์์ฑ
- java jdk ์ค์น
- ๋ถํธ ์๋์์ฑ
- ๊ฒ์ํ ์กฐํ
- ๊ฐ๋ฐํ๊ฒฝ๊ตฌ์ถ
- tomcat์ค์น
- ๋ณ๋ช ์ฒ๋ฆฌ
- ๊ฒ์๋ฌผ์กฐํ
- ๊ฒ์ํ ์ญ์ 
- mysql์ค์น
- ์๊ณ ๋ฆฌ์ฆ
- ๊ฒ์ํ๋ง๋ค๊ธฐ
- ์ ์ฒด๊ฒ์๋ฌผ ์กฐํ
- ์ดํด๋ฆฝ์ค ์ค์น
- ์จ๋ฆฌ์์ค
- Java
- ์๋ฃ๊ตฌ์กฐ
- ๊ฒ์๋ฌผ ์ญ์ 
- ์ดํด๋ฆฝ์ค ํ๊ธ ์ธ์ฝ๋ฉ
- typeAliases
- Algorithm
- java ํ๊ฒฝ๋ณ์
| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ | 
|---|---|---|---|---|---|---|
| 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 | 
									๊ธ ๋ณด๊ดํจ