Back-End/Java

[Spring] Legacy Project mybatis 설정 및 실행

CJun 2021. 6. 4. 11:57
반응형
mybatis 설정

 

mybatis 실행해보기

 

TimeMapper.java

package com.example.mapper;

import org.apache.ibatis.annotations.Select;

public interface TimeMapper {

	@Select("select sysdate from dual")
	String getTime();
}

 

TimeMapperTests.java
package com.example.sample4;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.example.mapper.TimeMapper;

import lombok.extern.log4j.Log4j;

//테스트 코드가 스프링을 실행해서 동작하도록 함
@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class TimeMapperTests {

	@Autowired
	private TimeMapper timeMapper;
	
	@Test
	public void testGetTime() {
		log.info(timeMapper.getTime());
	}
	
}

 

 

실행결과

 

더욱 간편하게 Select문 쓰는방법
xml 파일로 만들어준다.

똑같이 com => example => mapper => TimeMapper.xml 대소문자 구별하여 만들어준다.

 

TimeMapper.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.example.mapper.TimeMapper">
  <select id="getTime2" resultType="string">
    select sysdate from dual
  </select>
</mapper>

 

실행결과

반응형