✅ 순서
Mapper
적용💡 주의점
X) userId 랑 user_id (X)
user_Id 랑 user_id (O)
✅ mapper 인터페이스
@Mapper
public interface MemberMapper {
@Select("SELECT * FROM MEMBER") // 매개변수가 없는 경우
List<MemberDto> selectMemberAll();
@Select("SELECT * FROM MEMBER WHERE USER_ID=#{id}") // 매개변수가 있는경우
MemberDto selectMemberById(String id);
// @INSERT, @DELETE, @UPDATE 도 가능
}
✅ DAO
@Repository
public class MemberDaoImpl implements MemberDao {
private MemberMapper mapper;
public MemberDaoImpl(MemberMapper mapper) { // mybatis가 mapper기준으로 처리
this.mapper = mapper;
}
@Override
public List<MemberDto> selectMemberAll(SqlSession session) {
//return session.selectList("member.selectMemberAll");
// session으로 mapper.xml파일을 굳이 만들어서 처리할 필요 없음
return mapper.selectMemberAll();
}
@Override
public MemberDto selectMemberById(String userId) {
return mapper.selectMemberById(userId);
}
✅ @SelectProvider
어노테이션
type=클래스이름.class, method="메소드명"
✅ mapper 인터페이스
@Mapper
public interface MemberMapper {
@Select("SELECT * FROM MEMBER")
List<MemberDto> selectMemberAll();
@Select("SELECT * FROM MEMBER WHERE USER_ID=#{id}")
MemberDto selectMemberById(String id);
@SelectProvider(type=MemberSelectBuilder.class, method="selectMemberByWhere")
List<MemberDto> selectMemberByWhere(Map<String,Object> param);
}
✅ 문자열로 sql문을 반환하는 클래스 하나 만들기
package com.joo.usedmarket1.comm.mapper;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;
public class MemberSelectBuilder {
public static String selectMemberByWhere(Map<String,Object> param) {
return new SQL() {{
SELECT("*");
FROM("MEMBER");
if(param.get("userName")!=null && !param.get("userName").equals("")) {
WHERE("USERNAME like '%'||#{userName}||'%'");
}
}}.toString();
}
}