๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/Web

[Spring Boot] mybatis ์ ์šฉํ•˜๊ธฐ

by ynzu๐Ÿค 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
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€