본문 바로가기
개발/Web

[Spring Boot] mybatis 적용하기

by zuzuu 2021. 12. 6.
반응형

1.gradle 혹은 maven에 'mybatis-spring-boot-starter' 추가

compile("org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4")
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>

 

2. application.yml에 mybatis 설정 파일(mybatis-config.xml) 및 xml 파일 세팅

mybatis: 
  config-location: classpath:mybatis-config.xml 
  mapper-locations: classpath:mapper/postgresql/*.xml

 

3. mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE configuration 
    PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration> 
	<!-- http://www.mybatis.org/mybatis-3/configuration.html#settings 참고 --> 
	<settings> 
		<!-- query 결과 컬럼의 값이 null일 경우 result에 null로 setting할지 여무 --> 
		<setting name="callSettersOnNulls" value="true" /> 
		<!-- null parameter 허용 --> 
		<setting name="jdbcTypeForNull" value="NULL" /> 
	</settings> 
	<!-- vo type aliases --> 
	<typeAliases> 
		<typeAlias type="com.xxx.xxx.xxx.xxx.vo.job.BatchJobVo" alias="batchJobVo"/> 
	</typeAliases> 
</configuration>

 

4. postgresql/*.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.xxx.xxx.xxx.xxx.dao.BatchDao"> 
	<!-- batch 대상 select --> 
	<select id="selectBatchList"  resultType="batchJobVo"> 
		SELECT REQ_ID 
		FROM BATCH 
		WHERE 1=1 
			AND STATUS = '2' 
	</select> 
</mapper>

 

5. BatchDao.java

@Mapper 
public interface BatchDao { 
    List<BatchJobVo> selectBatchList(); 
}
728x90
반응형

댓글