ν‹°μŠ€ν† λ¦¬ λ·°

λ°˜μ‘ν˜•

ν•˜μŠ€νƒ€κ·Έλž¨μ„ λ§Œλ“€λ©΄μ„œ κ°€μž₯ 머리가 μ•„νŒ λ˜ 뢀뢄이 νŒ”λ‘œμš° λΆ€λΆ„μ΄μ—ˆλ‹€. νšŒμ›μ΄λ‚˜ κ²Œμ‹œκΈ€ 같은 경우 λ‹€λ₯Έ κ²Œμ‹œνŒ νŽ˜μ΄μ§€μ™€ 틀이 λΉ„μŠ·ν•΄μ„œ 생각보닀 μˆ˜μ›”ν•˜κ²Œ λ§Œλ“€μˆ˜ μžˆμ—ˆμ§€λ§Œ νŒ”λ‘œμš°λŠ” 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 μ—†μŒ

λ°˜μ‘ν˜•
λŒ“κΈ€