어장 Develop/어장 JAVA
[SpringBoot] mybatis를 활용한 DB Connection
김생선
2024. 2. 21. 17:03
반응형
몇년만에 밑바닥부터 SpringBoot를 설정해서 개발하는지 모르겠다. 이하는 추후 활용을 위한 개인 기록용.
1. pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
(중략)
<!-- Spring Boot 2.7.8 버전으로 활용 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- Mybatis Maven -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.2</version>
</dependency>
<!-- Maria DB -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.11</version>
</dependency>
|
cs |
maven dependency는 대략적으로 위와 같이 잡아주었다.
2. TestMapper.java / TestService.java / TestServiceImpl.java / TestController.java / TestSql.xml
1
2
3
4
5
6
7
8
9
10
|
package com.test.common.mapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TestMapper {
public String getSysDate();
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.test.comm.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.comm.service.TestService;
import com.test.common.mapper.TestMapper;
@Service
public class TestServiceImpl implements TestService {
@Autowired
TestMapper testMapper;
@Override
public String getSysdate() {
String result = testMapper.getSysDate();
return result;
}
}
|
cs |
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
|
package com.test.comm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.test.comm.service.TestService;
@RestController
public class testController {
@Autowired
TestService testService;
@PostMapping("/test")
public String test(@RequestBody String a) {
String result = "";
result = testService.getSysdate();
System.out.println("============= :::: " +result);
return null;
}
}
|
cs |
1
2
3
4
5
6
7
8
|
<?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.test.common.mapper.TestMapper">
<select id="getSysDate" resultType="java.lang.String">
select sysdate() from dual
</select>
</mapper>
|
cs |
1
|
mybatis.mapper-locations=mapper/**/*.xml
|
cs |
패키지 구조는 다음과 같다.
com.test.comm.TestController.java
com.test.comm.service.TestService.java
com.test.comm.service.impl.TestServiceImpl.java
com.test.common.mapper.TestMapper.java
src.main.resources.mapper.TestSql.xml
application.properties 에는 mybatis.mapper-locations 을 설정해준다.
오류가 난 상황으로는 SpringBoot 2.7.8 버전에서 mybatis-spring-boot-starter를 3.x 버전으로 설정해주었더니, TestServiceImpl 에서 @Autowired로 설정한 TestMapper.java를 인식할 수 없다는 오류가 발생했었다.
여러 시행착오 끝에 SpringBoot 2.7.8과 mybatis-spring-boot-starter 3.x과 호환이 안되는것을 인지하고, 현재는 2.x 버전대로 맞추어주었다.
에혀 힘들었다.
반응형