MyBatis을 직접적으로 사용할수도 있지만 Spring에서 Mybatis를 사용하려면
>> Mybatis
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
>>Mybatis-Spring
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
2개가 필요로 하다.
pom.xml 에 dependency 를 추가해야 한다.
또한 Mybatis를 위한 mybatis-config.xml (mybatis 설정파일) 과 Mapper 파일을 지정해줘야 한다.
>> root-context.xml에
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
SqlSessionFactory와 SqlSession은 SqlSessionFactoryBean, SqlSessionTemplate을 통해서 만들어진다.
형태로는
@Controller --> @Service ---> @Repository --> DB <-- 역순 으로 진행된다.
우선적으로
DTO(==VO) 를 생성해준다
DTO (Data Transfer Object) 로써 데이터를 갖는 객체이다.
>> getter, setter, constructor, toString 등을 추가해준다.
>> DTO를 통해 데이터를 담을 객체를 생성하였다면 Mapper를 만들어준다,
>> Mapper의 위치는 resources 하위 mapper 폴더 하위에 위치한다.
>> 이 위치는 Root-Context.xml 에서 mapperLocations 를 통해서 지정한다.
>> mapper 내부의 모습이다.
>> 여기서 중요한것은 맨위에 3줄 xml 설정을 추가하고
>> mapper에서 namespace를 작성해줘야 한다.
>> 이때 namespace는 아무거나 작성하는게 아닌 DAO interface를 지정하거나 마지막에 Mapper이름을 지정해준다.
>> com.fastcampus.ch4.dao.BoardDao(DAO interface) // com.fastcampus.ch4.dao.b(B)oardMapper (boardMapper이름)
>> mybatis-config.xml 을통해서 mybatis설정을 지정할수 있다.
위의 방법은
@Controller
@Service(service, serviceimpl()
DAO, @Repository(DAOImpl[@Autowired SqlSession , namespace지정])
Service와 DAO를 모두 interface와 impl로 구현해놨으며
SqlSession을 통해서 selectOne, update, delete, insert, selectMap, List 등의 메서드를 사용한다.
============================================================================================
차이점은 DAO를 구현하는 Impl 만드는것과 SqlSession 을 사용하냐 마냐의 차이같다.
============================================================================================
@controller / @Service (Service(interface), ServiceImpl) / DAO(interface) /
이방법은 DAO를 구현하는 Class가 없으며,
Root-Context.xml에
<mybatis-spring:sacn base-package="DAO 경로"> 를 지정해주면
DAO를 BEAN등록하게되고,
Mapper.xml 에서는 namespace에 해당 DAO의 경로를 작성해주면된다.(풀경로)
============================================================================================
Mybatis ${} vs #{} 의 차이는
#{} 는 PrepareStatement 로써 ? 에 들어가는 값들이다. 즉 value 값들에 '' 다옴표가 자동으로 맞춰서 들어가고
${} 는 Statement 로써 값 그자체이다 . 즉 state -> state 그대로 들어가게 된다. (다옴표를 붙여줘야함)
또한 XML 작성시에 특수문자를 사용할 떄는
<![CDATA[ <<~~ 내용 들어감 >> ]]> 으로 감싸준다.
'dev > Spring' 카테고리의 다른 글
[Spring] URLDecode.decode 안뜨는 현상 수정 (0) | 2022.07.13 |
---|---|
[Spring] Paging (0) | 2022.07.13 |
[Spring] @Transactional-2 (0) | 2022.07.12 |
[Spring] @Transactional-1 (0) | 2022.07.12 |
[Spring] AOP-2 (0) | 2022.07.12 |