Chapter 1. 목적 및 개요
Goldilocks JDBC Driver를 이용해 maven 기반의 Spring Legacy framework 을 사용한 환경에서
GOLDILOCKS DB와 연동하는 과정을 설명한다.
JAVA(JDK)가 설치되어 있어야 한다.
[테스트 환경 - DB]
OS : CentOS Linux release 7.9.2009
GOLDILOCKS : 22c.1.4
[테스트 환경 - Spring]
OS : Ubuntu 16.04.7 LTS
GOLDILOCKS Client : 22c.1.4
Spring : 5.3.15
Maven : 3.3.9
JAVA : 1.8.0_171
Chapter 2. Maven 설치
다음은 apt-get 으로 maven 을 설치하는 예시이다.
$ sudo apt-get install maven
다음과 같은 명령어로 maven 버전 확인을 할 수 있다.
$ mvn -v
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_171, vendor: Oracle Corporation
Java home: /opt/jdk1.8.0_171/jre
Default locale: ko_KR, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-142-generic", arch: "amd64", family: "unix"
Chapter 3. Spring 예제 생성
Spring 예제 프로젝트는 maven 명령어(mvn) 로 생성할 수 있다.
다음은 예제 프로젝트 생성 예시이다.
$ mvn archetype:generate -DgroupId=com.example -DartifactId=goldilocks-spring -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
..
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.327 s
[INFO] Finished at: 2024-10-28T17:42:05+09:00
[INFO] Final Memory: 21M/387M
[INFO] ------------------------------------------------------------------------
- archetype 플러그인으로 초기 템플릿 생성
- com/example 패키지 구조
- 프로젝트 아티팩트 ID 는 goldilocks-spring
다음과 같은 구조로 프로젝트가 생성된다.
goldilocks-spring/
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── example
│ └── App.java
└── test
└── java
└── com
└── example
└── AppTest.java
Chapter 4. 연동 환경 설정 및 sample 예제
GOLDILOCKS 는 JVM 버전에 따라 JDBC Driver 파일을 제공한다.
($GOLDILOCKS_HOME/lib/goldilocks*.jar)
프로젝트 디렉토리에서 lib 디렉토리를 생성해 이 goldilocks8.jar 파일을 복사한다.
$ pwd
/home/spring/goldilocks-spring
$ mkdir lib
$ cp goldilocks8.jar /home/spring/goldilocks-spring/lib/
$ ls -al /home/spring/goldilocks-spring/lib
goldilocks8.jar
pom.xml 에 디펜던시 및 예제에 필요한 plugin 들을 등록한다.
다음은 pom.xml 전체 파일의 예제이다.
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>goldilocks-spring</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>goldilocks-spring</name>
<dependencies>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.15</version>
</dependency>
<!-- Spring Context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.15</version>
</dependency>
<!-- Spring Beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.15</version>
</dependency>
<!-- Spring JDBC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.15</version>
</dependency>
<!-- Goldilocks JDBC Driver -->
<dependency>
<groupId>GOLDILOCKS</groupId>
<artifactId>goldilocks_test</artifactId>
<version>22.1.4</version>
<scope>system</scope>
<systemPath>/home/spring/goldilocks-spring/lib/goldilocks8.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin> <!-- Maven Surefire Plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin> <!-- Maven Compiler Plugin: Java 버전 설정 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin> <!-- Maven Jar Plugin: Main-Class 지정 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin> <!-- Maven Assembly Plugin: 모든 의존성을 포함한 실행 가능한 JAR 생성 -->
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
DB 접속 정보 및 필요한 bean ( jdbcTemplate) 를 설정한다.
(src/main/resources/applicationContext.xml)
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Component Scan 설정 -->
<context:component-scan base-package="com.example" />
<!-- DataSource 설정 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="sunje.goldilocks.jdbc.GoldilocksDriver"/>
<property name="url" value="jdbc:goldilocks://192.168.0.120:9311/test"/>
<property name="username" value="test"/>
<property name="password" value="test"/>
</bean>
<!-- JdbcTemplate 설정 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
sample 예제 코드
간단한 C/R/U/D 용 예제 java 파일은 다음과 같다.
모두 src/main/java/com/example/ 에 위치시킨다.
tree 는 다음과 같다.
goldilocks-spring
├── lib
│ └── goldilocks8.jar
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ ├── Main.java
│ │ ├── controller
│ │ │ └── TestController.java
│ │ ├── dao
│ │ │ └── TestDao.java
│ │ └── service
│ │ └── TestService.java
│ └── resources
│ └── applicationContext.xml
└── test
└── java
└── com
└── example
└── AppTest.java
src/main/java/com/example/Main.java
package com.example;
import com.example.controller.TestController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
TestController testController = context.getBean(TestController.class);
/*
* 다음과 같은 테이블이 존재한다고 가정
* CREATE TABLE T_TEST
* (
* ID INTEGER PRIMARY KEY,
* NAME VARCHAR(20)
* )
* ;
*/
/* C/R/U/D 동작 수행 */
testController.insertData(1004, "anbo");
testController.selectData();
testController.updateData(1004, "SUNJE SOFT");
testController.selectData();
testController.deleteData(1004);
}
}
src/main/java/com/example/controller/TestController.java
package com.example.controller;
import com.example.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class TestController {
@Autowired
private TestService testService;
public void insertData(int id, String name) {
testService.insertData(id, name);
System.out.println("[INSERT] row inserted: (" + id + ", " + name + ")");
}
public void selectData() {
System.out.println("[SELECT] Name: " + testService.selectData());
}
public void updateData(int id, String newName) {
testService.updateData(id, newName);
System.out.println("[UPDATE] Name updated to: " + newName);
}
public void deleteData(int id) {
testService.deleteData(id);
System.out.println("[DELETE] row with ID " + id + " is deleted.");
}
}
src/main/java/com/example/dao/TestDao.java
package com.example.dao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class TestDao {
private final JdbcTemplate jdbcTemplate;
public TestDao(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void insertData(int id, String name) {
String sql = "INSERT INTO T_TEST (id, name) VALUES (?, ?)";
jdbcTemplate.update(sql, id, name);
}
public List<String> selectData() {
String sql = "SELECT name FROM T_TEST";
return jdbcTemplate.queryForList(sql, String.class);
}
public void updateData(int id, String newName) {
String sql = "UPDATE T_TEST SET name = ? WHERE id = ?";
jdbcTemplate.update(sql, newName, id);
}
public void deleteData(int id) {
String sql = "DELETE FROM T_TEST WHERE id = ?";
jdbcTemplate.update(sql, id);
}
}
src/main/java/com/example/service/TestService.java
package com.example.service;
import com.example.dao.TestDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TestService {
@Autowired
private TestDao testDao;
public void insertData(int id, String name) {
testDao.insertData(id, name);
}
public List<String> selectData() {
return testDao.selectData();
}
public void updateData(int id, String newName) {
testDao.updateData(id, newName);
}
public void deleteData(int id) {
testDao.deleteData(id);
}
}
Chapter 5. 프로젝트 실행
컴파일 명령어는 다음과 같다.
$ mvn package
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.477 s
[INFO] Finished at: 2024-11-08T21:20:17+09:00
[INFO] Final Memory: 37M/403M
[INFO] ------------------------------------------------------------------------
실행 명령어는 다음과 같다.
$ java -cp target/goldilocks-spring-1.0-SNAPSHOT-jar-with-dependencies.jar:/home/spring/goldilocks-spring/lib/goldilocks8.jar com.example.Main
[INSERT] row inserted: (1004, anbo)
[SELECT] Name: [anbo]
[UPDATE] Name updated to: SUNJE SOFT
[SELECT] Name: [SUNJE SOFT]
[DELETE] row with ID 1004 is deleted.
'IT > Installation Guides' 카테고리의 다른 글
[설치 가이드] :: Spring Boot & Hibernate Goldilocks 연동 가이드 (0) | 2023.06.23 |
---|---|
[설치 가이드] :: 전자정부프레임워크(eGov) Goldilocks 연동 가이드 (0) | 2023.06.22 |
[설치 가이드] :: How to install Oracle DB 12c R2 on CentOS 7 (CentOS 7에 Oracle DB 12c R2 설치하기) (0) | 2018.05.11 |