누구나 한번 쯤 

sqlSessionFactory.openSession()

sqlSession.쿼리

sqlSession.close() 

이 중복코드가 귀찮다고 생각해봤을 것이다.


이를 위해서 MyBatis에서는 interface를 이용한 자동 매퍼 설정을 만들어났다.

@MapperScan이라는 애노테이션을 이용하면 된다.


1. 일단 @Configuration 클래스를 만든다.

@Configuration @MapperScan(basePackages="test.mapping", annotationClass=Mapper.class) public class MapperConfigurer { }

2. 매퍼 인터페이스를 만든다.

@Mapper public interface DynamicMapper { @Select("select * from lcate where seq = #{num }") public List<Map> selectList(Map num);

@Insert("insert into lcate(code, name) values('003', '김성욱')") public int insertListMember(); }

편의상 xml 파일을 만들지 않고 애노테이션을 사용해서 쿼리문을 만들었다.

3. @Autowired로 다음과 같이 Injection을 한다.

@Autowired
DynamicMapper dynamicMapper;

4. 메서드를 호출한다

dynamicMapper.insertListMeber(); 와 같이 호출하면 된다.


XML설정

advice 클래스

public class Advisor {

public String returner(ProceedingJoinPoint joinPoint, HttpServletRequest req) throws Throwable  {

String URI = req.getRequestURI();

String contextPath = req.getContextPath();

joinPoint.proceed();

System.out.println("after");

System.out.println(URI);

System.out.println(contextPath);

String returnURI = URI.substring(contextPath.length()); 

return returnURI;

}

}

advice를 등록한다.

<bean id="myAdvice" class="com.zxy.Advisor" >

AOP-Config설정을 한다

<aop:config>

<aop:aspect ref="myAdvice">

<aop:pointcut id="myPointCut" expression="execution(* test..*Controller.*(..)) and args(req)" />

<aop:around method="returner" pointcut-ref="myPointCut" />

</aop:aspect>

<aop:aspect ref="myAdvice">

<aop:pointcut id="myPointCut2" 

expression="execution(* test..*(javax.servlet.http.HttpServletRequest, String)) and args(req, ctg)" />

<aop:around method="returner3" pointcut-ref="myPointCut2" />

</aop:aspect>

</aop:config>

<aop:aspect>태그의 ref는 참조할 advice bean id

<aop:pointcut> aspect적용 지점
     expression : 적용할 메서드 경로
execution은 메서드 전체 형식으로 지정한다.
접근제한자 반환타입 패키지를 포함한 메서드명 (인자)
<aop:around> method는 aspect로 동작할 메서드
   pointcut-ref는 참조할 pointcut id


HttpServletRequest를 받아서 URI를 받아 쓰고 싶은 경우가 생길 것이다.

파라메터를 받아 쓰고 싶은 경우는

execution에서 지정한 메서드 인자를 (..) 대신 타입을 패키지를 포함해서 써주고 args()도 추가해 주어야 한다.

(javax.servlet.http.HttpServletRequest, String) and args(req, ctg)

args()의 인자는 변수명을 쓴다.


전통적인 폭포수 개발방법 부터  최근의 애자일 방법론 등 소프트웨어 공학으로 이미 정립된 방법론이 많이 있지만

이미 있을지도 모르지만 나만의 개발 방법론을 정립해야겠다.


viewController


필요한 정보를 뿌려줄 경로 매핑


필요한 정보

title : 페이지 이름
model : 혹은 맵, 요청 정보가 담긴 객체로 주로 맵을 사용
     페이징 처리를 한다면 페이징 객체도 담음

메서드 이름을 어떻게 정해서 구분할 것인가?
DB하나의 튜플만 보여주는 경우와 list를 보여주는 경우
하나의 투플만 보여줄 경우 - 페이징 처리가 필요 없다.
메서드를 구분할 것인가 컨트롤러를 나눌 것인가?
예) 회원 목록을과 회원 정보, member 테이블에 member객체들
selectMember, selectMemberOne, selectOneMember
selectMemberList, selectListMember
아무래도 메서드 구분을 쉽게 하려면 one이나 list를 select다음에 붙이는게 좋지만
회화적으로는 뭔가 어색
정규식 표현
select*List, select*One, selectList*,selectOne*



+ Recent posts