일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- ㅓㅂ
- 출처:구멍가게코딩단-코배스(개정판)
- form.getImageFies 오타났음
- 그럼 int배열의 deefault값은?????
- 행열. 2중반복문..
- 생활코딩
- 마지막에 배열의 foreach구문이 틀린것같은데 ...... 저게왜틀린건지나는잘모르겠슴다.
- bindingresult 쓰니까 에러났다. 어떻게해야하냐;;
- while문이 틀린이유?? math.random()을 사용해서푸는법?
- boolean배열
- (참고로 boolean 배열의 default 값은 false 이다.
- Today
- Total
푸린세스
MyBatis ~ 스프링과 연동처리 본문
SQLSessionFactory 를 이용해서
SQL 어떻게 처리할것인지 별도의 설정을 분리, 자동으로 처리하는 방식을 이용하자: MyBatis의 Mapper
MyBatis-Spring을 이용할 경우 : Mapper를 XML + interface + annotation 형태로 작성가능하다.
1) Mapper 인터페이스 (어노테이션을 이용해서 SQL을 메서드에 추가)
package org.moominzero.mapper;
import org.apache.ibatis.annotations.Select;
public interface TimeMapper {
// ;없어야한다.
@Select("SELECT sysdate From dual")
String getTime();
}
2) Mapper설정 - root-context.xml
<mybatis:scan> 태그 이용
3)Mapper 테스트 ★
MyBatis-Spring: Mapper인터페이스로 실제 SQL처리가 되는 클래스를 자동 생성해준다.
따라서 인터페이스와 SQL만 작성하는 방식으로도 모든 JDBC처리가 가능하다.
package org.moominzero.persistence;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.moominzero.mapper.TimeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class) //테스트시 필요한 클래스 지정 (스프링은 SpringJunit4ClassRunner클래스가 대상이된다.)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml") //어떤 설정정보를 읽어들어야하는지.
@Log4j
public class TimeMapperTests {
@Autowired
private TimeMapper timeMapper;
@Test
public void testTime1() {
log.info(timeMapper.getClass().getName());
log.info(timeMapper.getTime());
}
}
실행결과
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@1f59a598, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1e178745, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@192c3f1e, org.springframework.test.context.support.DirtiesContextTestExecutionListener@26b3fd41, org.springframework.test.context.transaction.TransactionalTestExecutionListener@7494f96a, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@561b6512]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:src/main/webapp/WEB-INF/spring/root-context.xml]
INFO : org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@2abf4075: startup date [Mon May 01 17:34:21 KST 2023]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
INFO : org.moominzero.persistence.TimeMapperTests - com.sun.proxy.$Proxy25
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
INFO : org.springframework.jdbc.support.SQLErrorCodesFactory - SQLErrorCodes loaded: [DB2, Derby, H2, HDB, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@2abf4075: startup date [Mon May 01 17:34:21 KST 2023]; root of context hierarchy
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.
개발시에는 인터페이스만 만들어주었는데
INFO : org.moominzero.persistence.TimeMapperTests - co m.sun.proxy.$Proxy25
내부적으로 적당한 클래스가 생성되었다.
XML매퍼와 같이쓰기
SQL처리시 어노테이션방식, XML방식이 있다.
SQL이 복잡해지는 경우 XML을 이용하는 방식을 선호한다.
XML의 파일위치 : Mapper 인터페이스있는곳에 작성 or src/main/resources구조에 xml을 저장할 폴더생성
XML 파일이름 : Mapper인터페이스와 동일하게
XML파일에 지정하는 namespace속성
[TimeMapper인터페이스]
package org.moominzero.mapper;
import org.apache.ibatis.annotations.Select;
////org.moominzero.mapper.TimeMapper.getTime2
public interface TimeMapper {
// ;없어야한다.
@Select("SELECT sysdate From dual")
String getTime();
String getTime2();
}
getTime2 메서드 -> @select 어노테이션X/ SQL도 존재하지않는다.
--> XML을 이용해서 SQL을 처리할것이다.
[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="org.moominzero.mapper.TimeMapper">
<select id="getTime2" resultType="string">
select sysdate from dual
</select>
<!-- id는 메서드이름 -->
<!-- 인터페이스에 선언된 SQL문이 없으면 -> XML문서를 찾아서 실행한다.-->
</mapper>
mapper의 namespace속성, 인터페이스 이름 가지도고판단한다.
(인터페이스 이름 == XML의 namespace속성의 이름) 이라면 => 이를 병합해서 처리한다.
위의경우: 메서드선언: 인터페이스에 존재 / SQL에 대한 처리 : XML을 이용
<select>태그의 id값 = 메서드이름
resultType속성 = 인터페이스에 선언된 메서드의 리턴타입
[테스트]
@Test
public void testTime2() {
log.info(timeMapper.getTime2());
}
package org.moominzero.persistence;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.moominzero.mapper.TimeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class) //테스트시 필요한 클래스 지정 (스프링은 SpringJunit4ClassRunner클래스가 대상이된다.)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml") //어떤 설정정보를 읽어들어야하는지.
@Log4j
public class TimeMapperTests {
@Autowired
private TimeMapper timeMapper;
@Test
public void testTime1() {
log.info(timeMapper.getClass().getName());
log.info(timeMapper.getTime());
}
@Test
public void testTime2() {
log.info(timeMapper.getTime2());
}
}
testTime1 과 testTime2의 결과는 동일하다.
'spring > 구멍가게코딩단-스프링' 카테고리의 다른 글
p190 MySQL~Oracle (0) | 2023.05.03 |
---|---|
스프링MVC의 기본사상 (0) | 2023.05.02 |
스프링MVC 기본구조 (0) | 2023.05.02 |
커넥션 풀 설정 (0) | 2023.05.01 |
구멍가게코딩단-스프링 p56 (0) | 2023.05.01 |