Mybatis cursor map으로 리턴받기

공부는 혼자하는 거·2021년 9월 8일
0

DB

목록 보기
2/7

프로시저 생성

create or replace PROCEDURE GETEMP5

(
  m_patientUserId IN VARCHAR2,
  EMP_CURSOR OUT SYS_REFCURSOR

) IS 

BEGIN

  OPEN EMP_CURSOR FOR

  select * from patient where patientUserId LIKE '%'||m_patientUserId||'%';

END GETEMP5;

|| 가 concat과 같은 역할을 한다고 생각하면 된다. 주의할 점은 프로시저의 인자를 선언할 때. 반드시 "문자_변수명"으로 선언해줘야 된다는 거다. 아니면 프로시저에서 인자를 정상적으로 전달해줘도 인식을 못하더라. 3시간 소모했으니까 꼭 기억하자..

VARIABLE RC REFCURSOR;
EXEC GETEMP5('patient 100', :RC);
PRINT RC;

요런 식으로 sql developer에서 확인할 수 있다.

VO 생성

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Patient { 
	
	private Integer pid;
	private String patientUserId;
	private String firstname;
	private String lastname;
	//private GenderType gender;
	private Integer gender;
	private Integer age;
	private Double height;
	private Double weight;
	private String lastSession; //sessionId
	private String comment;

}

Mapper 생성


@Mapper
public interface PatientMapper {
	public List<Patient> findByContainPatientUserId(Map<String,Object> map); //프로시저 호출

}

mapper에 대응할 XML 생성

	
     <resultMap id="patientMap" type="com.patient.Patient">
    </resultMap>
    
    <select id="findByContainPatientUserId" statementType="CALLABLE">
		{
			CALL GETEMP5(
				#{patientUserId, mode=IN, jdbcType=VARCHAR}, 
				#{key, jdbcType=CURSOR, mode=OUT, javaType=java.sql.ResultSet, resultMap=patientMap}
			)
		} 
	</select>

인자를 Map으로 전달하면 key 값을 "key"로 해서 리턴한다.

호출

HashMap<String,Object> map = new HashMap<String,Object>();

				map.put("patientUserId", patientUserId);
		
				
				oraclePatientMapper.findByContainPatientUserId(map);
				
				
				log.info(map.toString());
				
				
				List<Patient> bilabPatients = (List<Patient>)map.get("key"); 
				
				log.info("확인... " + bilabPatients);

https://micropilot.tistory.com/2626

profile
시간대비효율

0개의 댓글