반응형
위와 같이 프로젝트가 구성되있다고 할 때, pom.xml에 mybatis와 database driver를 설치해줍니다.
1. pom.xml (Maven기준)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>abc</artifactId>
<version>1.0-SNAPSHOT</version>
<name>javafx03_mybatis</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.10.0</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.6</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17.0.6</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.4.2.jre11</version>
<scope>runtime</scope>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>org.example.abc.HelloApplication</mainClass>
<launcher>app</launcher>
<jlinkZipName>app</jlinkZipName>
<jlinkImageName>app</jlinkImageName>
<noManPages>true</noManPages>
<stripDebug>true</stripDebug>
<noHeaderFiles>true</noHeaderFiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2. module-info.java
스프링처럼 사용하려면 module-info.java 파일이 필요함. 아래의 추가부분을 설정해준다. (이 부분을 몰라서 한참 헤멤)
module org.example.abc {
requires javafx.controls;
requires javafx.fxml;
requires java.sql;
requires org.mybatis; // <-- Add this
opens org.example.abc to javafx.fxml;
exports org.example.abc;
exports org.example.abc.mapper;
opens org.example.abc.mapper to javafx.fxml;
exports org.example.abc.dto;
opens org.example.abc.dto to javafx.fxml;
opens mapper; // <-- 이거 추가해야 mapper 인식
}
3. MyBatisUtil.java
package org.example.abc;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
public static SqlSession getSession() {
return sqlSessionFactory.openSession();
}
}
4. mybatis-config.xml
특별한 건 없습니다. DB 접속정보를 적어주고, Mapper의 위치를 적어줍니다.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://<databaseURL>:<port_number>;databaseName=<DB>;encrypt=true;trustServerCertificate=true;"/>
<property name="username" value="<username>"/>
<property name="password" value="<password>"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/<매퍼이름>.xml"/>
</mappers>
</configuration>
Mapper.xml 상세와 DTO, UI상세 등은 Spring과 동일함.
반응형
'Programming > Java_Etc' 카테고리의 다른 글
Java 폴더 하위폴더 하위 파일 복사 (0) | 2024.11.13 |
---|---|
Java Mybatis설정에서 MS-SQL연결 설정방법 (1) | 2024.09.05 |
Java Swing프로젝트에서 Mybatis 연동하기 (0) | 2024.09.02 |
자바GUI(Swing) - Simple text editor (0) | 2022.07.11 |
Linux용 음악 프로그램 QMMP - 설치부터 스킨 변경까지 (0) | 2022.07.03 |