Search

MyBatis

MyBatis

MyBatis λŒ€ν•΄μ„œ μ•Œμ•„λ³΄κ³  κ°„λ‹¨ν•˜κ²Œ κ²Œμ‹œνŒ κΈ°λŠ₯을 κ΅¬ν˜„ν•˜λŠ” μ‹€μŠ΅κΉŒμ§€ 진행해 보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€.
β€’
Mybatis
β€’
ν”„λ‘œμ νŠΈ ꡬ쑰
β€’
μŠ€ν”„λ§ λΆ€νŠΈμ—μ„œ MyBatis μ‚¬μš©
β€’
μ˜μ‘΄μ„± μΆ”κ°€
β—¦
build.gradle
β—¦
pom.xml
β€’
λ§ˆμ΄λ°”ν‹°μŠ€ μ„€μ • 파일 - mybatis-config.xml
β€’
ν”„λ‘œμ νŠΈ μ„€μ • - application.properties
β€’
DB ꡬ좕
β€’
둜직 μž‘μ„±
β—¦
DTO μž‘μ„±
β—¦
SQL Mapper μž‘μ„±
β—¦
Mapper μΈν„°νŽ˜μ΄μŠ€ μž‘μ„±
β—¦
Service μž‘μ„±
β—¦
Controller μž‘μ„±
β—¦
View μž‘μ„±

MyBatis

λ§ˆμ΄λ°”ν‹°μŠ€λŠ” κ°œλ°œμžκ°€ μ§€μ •ν•œ SQL, μ €μž₯ν”„λ‘œμ‹œμ € 그리고 λͺ‡κ°€μ§€ κ³ κΈ‰ 맀핑을 μ§€μ›ν•˜λŠ” νΌμ‹œμŠ€ν„΄μŠ€ ν”„λ ˆμž„μ›Œν¬μ΄λ‹€.
μžλ°” ν”„λ‘œκ·Έλž¨μ—μ„œ λ°μ΄ν„°λ² μ΄μŠ€ 연동을 μœ„ν•΄μ„œ, JDBC λ“œλΌμ΄λ²„λ₯Ό μ‚¬μš©ν•˜λ©΄, Java μ½”λ“œμ™€ SQL μ½”λ“œκ°€ 같이 μ‚¬μš©λ˜λ‹ˆκΉŒ, μœ μ§€λ³΄μˆ˜μ„±μ΄ μ €ν•˜λ©λ‹ˆλ‹€.
κ·Έλž˜μ„œ Mybatis λ₯Ό μ‚¬μš©ν•˜λ©΄, Java μ½”λ“œμ—μ„œ SQL 을 λΆ„λ¦¬ν•΄μ„œ XML 관리λ₯Ό ν•˜κ³ , μžλ°”μ˜ μΈν„°νŽ˜μ΄μŠ€ ν˜ΈμΆœμ— λ”°λΌμ„œ XML 의 νŠΉμ • id 에 ν•΄λ‹Ήν•˜λŠ” 쿼리λ₯Ό μ‹€ν–‰μ‹œν° SQL Mapper λ°©μ‹μœΌλ‘œ DB 연동을 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ν”„λ‘œμ νŠΈ ꡬ쑰

πŸ“¦Β spring-mybatis β”‚ β””β”€β”€β”€πŸ“‚java β”‚ β””β”€β”€β”€πŸ“‚com.aloha.springmybatis β”‚ β”‚ β”‚ β”œβ”€β”€β”€πŸ“‚controller β”‚ β”‚ β””β”€β”€β”€πŸ“„BoardController.java β”‚ β”‚ β”‚ β”œβ”€β”€β”€πŸ“‚dto β”‚ β”‚ β””β”€β”€β”€πŸ“„Board.java β”‚ β”‚ β”‚ β”œβ”€β”€β”€πŸ“‚service β”‚ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€β”€πŸ“„BoardService.java β”‚ β”‚ β””β”€β”€β”€πŸ“„BoardServiceImpl.java β”‚ β”‚ β”‚ β””β”€β”€β”€πŸ“‚mapper β”‚ β””β”€β”€β”€πŸ“„BoardMapper.java β”‚ β””β”€β”€β”€πŸ“‚resources β”‚ πŸ“„application.properties β”‚ πŸ“„mybatis-config.xml β”‚ β””β”€β”€β”€πŸ“‚com.aloha.springmybatis.mapper β””β”€β”€β”€β”€πŸ“„BoardMapper.xml
Plain Text
볡사

μŠ€ν”„λ§ λΆ€νŠΈμ—μ„œ MyBatis μ‚¬μš©

SpringBoot μ—μ„œ MyBatisλ₯Ό μ‚¬μš©ν•  λ•Œμ—λŠ” 주둜 Gradle λΉŒλ“œλ„κ΅¬μ˜ μ„€μ •νŒŒμΌμ€ build.gradle λ˜λŠ” Maven λΉŒλ“œλ„κ΅¬μ˜ μ„€μ • 파일인 pom.xml 에 μ˜μ‘΄μ„±μ„ μΆ”κ°€ν•˜μ—¬ μ‚¬μš©ν•©λ‹ˆλ‹€

μ˜μ‘΄μ„± μΆ”κ°€

β€’
build.gradle
β€’
pom.xml

build.gradle

β€’
mysql λ“œλΌμ΄λ²„ μ˜μ‘΄μ„±
β€’
MyBatis μ˜μ‘΄μ„±
β€’
build.gradle μ „μ²΄μ½”λ“œ

mysql λ“œλΌμ΄λ²„ μ˜μ‘΄μ„±

runtimeOnly 'com.mysql:mysql-connector-j'
Java
볡사

MyBatis μ˜μ‘΄μ„±

implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3' testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.3'
Java
볡사
springboot v3 버전을 κΈ°μ€€μœΌλ‘œ μ‚¬μš©ν•œ μ˜μ‘΄μ„±μž…λ‹ˆλ‹€.

build.gradle

plugins { id 'java' id 'war' id 'org.springframework.boot' version '3.3.5' id 'io.spring.dependency-management' version '1.1.6' } group = 'com.aloha' version = '0.0.1-SNAPSHOT' java { toolchain { languageVersion = JavaLanguageVersion.of(17) } } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.3' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } tasks.named('test') { useJUnitPlatform() }
Java
볡사

pom.xml

β€’
mysql λ“œλΌμ΄λ²„ μ˜μ‘΄μ„±
β€’
MyBatis μ˜μ‘΄μ„±
β€’
build.gradle μ „μ²΄μ½”λ“œ

mysql λ“œλΌμ΄λ²„ μ˜μ‘΄μ„±

<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> <scope>runtime</scope> </dependency>
XML
볡사

MyBatis μ˜μ‘΄μ„±

<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter-test</artifactId> <version>3.0.3</version> <scope>test</scope> </dependency>
XML
볡사
springboot v3 버전을 κΈ°μ€€μœΌλ‘œ μ‚¬μš©ν•œ μ˜μ‘΄μ„±μž…λ‹ˆλ‹€.

pom.xml

<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>mybatis</name> <description>Demo project for Spring Boot</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter-test</artifactId> <version>3.0.3</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
XML
볡사

λ§ˆμ΄λ°”ν‹°μŠ€ μ„€μ • 파일 - mybatis-config.xml

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- μ„€μ • --> <settings> <!-- μ–Έλ”μŠ€μ½”μ–΄ μΌ€μ΄μŠ€μΈ μ»¬λŸΌμ„ 카멜 μΌ€μ΄μŠ€λ‘œ λ³€ν™˜ν•˜λŠ” μ„€μ • --> <!-- board_no - boardNo --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- νƒ€μž… 별칭 μ„€μ • --> <typeAliases> <!-- ν…Œμ΄λΈ”κ³Ό 맀핑할 DTOκ°€ μžˆλŠ” νŒ¨ν‚€μ§€ 경둜 지정 --> <package name="com.aloha.mybatis.dto"/> </typeAliases> </configuration>
XML
볡사

mapUnderscoreToCamelCase

<settings> <!-- μ–Έλ”μŠ€μ½”μ–΄ μΌ€μ΄μŠ€μΈ μ»¬λŸΌμ„ 카멜 μΌ€μ΄μŠ€λ‘œ λ³€ν™˜ν•˜λŠ” μ„€μ • --> <!-- board_no - boardNo --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
XML
볡사
μ–Έλ”μŠ€μ½”μ–΄ μΌ€μ΄μŠ€(μŠ€λ„€μ΄ν¬ μΌ€μ΄μŠ€)λ₯Ό 카멜 μΌ€μ΄μŠ€λ‘œ μžλ™ λ³€ν™˜ν•΄μ£ΌλŠ” κΈ°λŠ₯ ν™œμ„±ν™”
μ΄λ ‡κ²Œ μž‘μ„±ν•˜λ©΄ λ°μ΄ν„°λ² μ΄μŠ€μ˜ μ–Έλ”μŠ€μ½”μ–΄ μΌ€μ΄μŠ€λ₯Ό λ³„λ„λ‘œ μžλ°”μ˜ 카멜 μΌ€μ΄μŠ€λ‘œ λ³€ν™˜ν•˜μ§€ μ•Šμ•„λ„ μžλ™μœΌλ‘œ ν…Œμ΄λΈ”μ˜ 컬럼λͺ…을 객체의 λ³€μˆ˜λ‘œ 맀핑 ν•  수 있게 λ³€ν™˜ν•΄μ€λ‹ˆλ‹€.

typeAliases - package

<typeAliases> <!-- ν…Œμ΄λΈ”κ³Ό 맀핑할 DTOκ°€ μžˆλŠ” νŒ¨ν‚€μ§€ 경둜 지정 --> <package name="com.aloha.mybatis.dto"/> </typeAliases>
XML
볡사
resultType μ†μ„±μ—μ„œ dto 객체 (νŒ¨ν‚€μ§€.클래슀λͺ…) (클래슀λͺ…)
이 섀정을 κ΅¬μ„±ν•˜λ©΄ Mapper νŒŒμΌμ—μ„œ resultType 속성이 νŒ¨ν‚€μ§€λΆ€ν„° ν΄λž˜μŠ€κΉŒμ§€ λ‹€ μ§€μ •ν•˜μ§€ μ•Šμ•„λ„, 맀핑할 객체가 μžˆλŠ” νŒ¨ν‚€μ§€λ₯Ό μ—¬κΈ° μ„€μ •ν•¨μœΌλ‘œμ¨, 객체 μ΄λ¦„λ§Œ μž‘μ„±ν•΄λ„ 쑰회의 κ²°κ³Όλ₯Ό 객체의 λ°”λ‘œ 맀핑할 수 μžˆμŠ΅λ‹ˆλ‹€.
<!-- κ²Œμ‹œκΈ€ 쑰회 --> <select id="select" resultType="com.aloha.mybatis.dto.Board">
XML
볡사
이 섀정을 μ‚¬μš©ν•˜μ§€ μ•ŠμœΌλ©΄ Β λ‹€μŒκ³Ό 같이 νŒ¨ν‚€μ§€λΆ€ν„° 클래슀λͺ…κΉŒμ§€ resultType 속성에 λ‹€ μž‘μ„±ν•΄μ•Ό λ˜μ§€λ§Œ, 이 섀정을 μ‚¬μš©ν•˜λ©΄ 객체λͺ…λ§Œ μž‘μ„±ν•΄λ„ λ©λ‹ˆλ‹€.
<!-- κ²Œμ‹œκΈ€ 쑰회 --> <select id="select" resultType="Board">
XML
볡사
λ”°λΌμ„œ 쑰회 κ²°κ³Όλ₯Ό 맀핑할 객체듀이 μžˆλŠ” νŒ¨ν‚€μ§€λ“€μ„ 미리 지정해 λ†“μœΌλ©΄ . λ‹€μŒκ³Ό 같이 κ°„κ²°ν•˜κ²Œ 속성 값을 μž‘μ„±ν•  수 μžˆμŠ΅λ‹ˆλ‹€ .

ν”„λ‘œμ νŠΈ μ„€μ • - application.properties

DB λ“œλΌμ΄λ²„μ™€ MyBatis의 섀정을 ν”„λ‘œμ νŠΈμ— μ•Œλ €μ£ΌκΈ° μœ„ν•΄μ„œλŠ” Β application.properties νŒŒμΌμ— λ‹€μŒκ³Ό 같이 데이터 μ†ŒμŠ€μ™€ Mybatis κ΄€λ ¨ 섀정을 μž‘μ„±ν•΄μ•Ό ν•©λ‹ˆλ‹€.
application.properties λŠ” μŠ€ν”„λ§ λΆ€νŠΈμ˜ ν”„λ‘œμ νŠΈ μ„€μ • νŒŒμΌμž…λ‹ˆλ‹€.
β€’
데이터 μ†ŒμŠ€ μ„€μ •
β€’
mybatis-config.xml μ„€μ •

데이터 μ†ŒμŠ€ μ„€μ •

# 데이터 μ†ŒμŠ€ - MySQL spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/aloha?serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&useSSL=false&autoReconnection=true&autoReconnection=true spring.datasource.username=aloha spring.datasource.password=123456
Plain Text
볡사

mybatis-config.xml μ„€μ •

# Mybatis μ„€μ • # Mybatis μ„€μ • 경둜 : ~/resources/mybatis-config.xml mybatis.config-location=classpath:mybatis-config.xml # Mybatis 맀퍼 파일 경둜 : ~/λ©”μΈνŒ¨ν‚€μ§€/mapper/**Mapper.xml mybatis.mapper-locations=classpath:mybatis/mapper/**/**.xml
Plain Text
볡사
β€’
classpath: 라고 μ§€μ •ν•˜λ©΄, ~/resources 경둜 ν•˜μœ„λΆ€ν„° 경둜λ₯Ό 지정할 수 μžˆλ‹€.
β€’
classpath:mybatis/mapper/**/**.xml 라고 μ§€μ •ν•˜λ©΄, ~/resources κ²½λ‘œλΆ€ν„° λͺ¨λ“  경둜의 λͺ¨λ“  ν•˜μœ„ 파일의 ν™•μž₯μžκ°€ .xml 이면 λ§ˆμ΄λ°”ν‹°μŠ€ 맀퍼(SQL) 파일둜 μΈμ‹ν•˜λ„λ‘ μ§€μ •ν•œλ‹€.

mybatis-config.xml μ„€μ • 파일 없이 μ μš©ν•˜κΈ°

# 컬럼λͺ…을 λ³€μˆ˜λͺ…μœΌλ‘œ μžλ™λ§€ν•‘ : board_no ➑ boardNo mybatis.configuration.map-underscore-to-camel-case=true # resultType μ†μ„±μ—μ„œ dto 객체 (νŒ¨ν‚€μ§€.클래슀λͺ…) ➑ (클래슀λͺ…) mybatis.type-aliases-package=com.aloha.mybatis.dto # Mybatis 맀퍼 파일 경둜 : ~/λ©”μΈνŒ¨ν‚€μ§€/mapper/**Mapper.xml mybatis.mapper-locations=classpath:mybatis/mapper/**/**.xml
Plain Text
볡사
MyBatis에 μ„€μ • νŒŒμΌμ„ μ‚¬μš©ν•΄μ„œ λ§ˆμ΄λ°”ν‹°μŠ€μ˜ 섀정을 관리할 μˆ˜λ„ μžˆμ§€λ§Œ . 기본적인 섀정듀을 μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ ν”„λ‘œν¬ν‹°μŠ€μ—μ„œ λ°”λ‘œ 섀정을 ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

application.properties

spring.application.name=spring-mybatis # 데이터 μ†ŒμŠ€ - MySQL spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/aloha?serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&useSSL=false&autoReconnection=true&autoReconnection=true spring.datasource.username=aloha spring.datasource.password=123456 # Mybatis μ„€μ • # Mybatis μ„€μ • 경둜 : ~/resources/mybatis-config.xml # mybatis.config-location=classpath:mybatis-config.xml # 컬럼λͺ…을 λ³€μˆ˜λͺ…μœΌλ‘œ μžλ™λ§€ν•‘ : board_no ➑ boardNo mybatis.configuration.map-underscore-to-camel-case=true # resultType μ†μ„±μ—μ„œ dto 객체 (νŒ¨ν‚€μ§€.클래슀λͺ…) ➑ (클래슀λͺ…) mybatis.type-aliases-package=com.aloha.mybatis.dto # Mybatis 맀퍼 파일 경둜 : ~/λ©”μΈνŒ¨ν‚€μ§€/mapper/**Mapper.xml mybatis.mapper-locations=classpath:mybatis/mapper/**/**.xml
Plain Text
볡사

DB ꡬ좕

이번 μ‹€μŠ΅μ—μ„œλŠ” MySQL의 λ“œλΌμ΄λ²„λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€ . MySQL의 μ„€μΉ˜κ°€ ν•„μš”ν•˜κ±°λ‚˜ . 기본적인 μ‚¬μš© 방법을 μ•Œκ³  μ‹Άλ‹€λ©΄ μœ„μ˜ νŽ˜μ΄μ§€λ₯Ό μ°Έμ‘°ν•΄ μ£Όμ„Έμš” .
β€’
μŠ€ν‚€λ§ˆ : aloha
β€’
ν…Œμ΄λΈ” 생성
β—¦
board

board

CREATE TABLE `board` ( `no` int NOT NULL AUTO_INCREMENT, `title` varchar(100) NOT NULL, `writer` varchar(100) NOT NULL, `content` text, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `views` int NOT NULL DEFAULT '0', PRIMARY KEY (`no`) ) COMMENT='κ²Œμ‹œνŒ';
SQL
볡사

둜직 μž‘μ„±

β€’
DTO μž‘μ„±
β€’
SQL Mapper μž‘μ„±
β€’
Mapper μΈν„°νŽ˜μ΄μŠ€ μž‘μ„±
β€’
Service μž‘μ„±
β€’
Controller μž‘μ„±
β€’
View μž‘μ„±

DTO μž‘μ„±

β€’
Board.java

Board.java

Board dtoλŠ” λ°μ΄ν„°λ² μ΄μŠ€μ˜ board ν…Œμ΄λΈ”κ³Ό λ§€ν•‘ν•˜κΈ° μœ„ν•œ 데이터 전솑 κ°μ²΄μž…λ‹ˆλ‹€
/** * Board * - κ²Œμ‹œκΈ€ 정보 */ @Data public class Board { private int no; private String title; private String writer; private String content; private Date createdAt; private Date updatedAt; private int views; }
Java
볡사

SQL Mapper μž‘μ„±

SQL Mapperμ—λŠ” λ°μ΄ν„°λ² μ΄μŠ€μ˜ μš”μ²­μ„ 보내기 μœ„ν•œ SQL 쿼리듀을 xml νƒœκ·Έ ν˜•μ‹μœΌλ‘œ μ •μ˜ν•©λ‹ˆλ‹€
β€’
κΈ°λ³Έ Mapper 파일 ν˜•μ‹
β€’
κΈ°λ³Έ CRUD Mapper 파일 μ½”λ“œ
β€’
BoardMapper.xml

κΈ°λ³Έ Mapper 파일 ν˜•μ‹

Mybatis Mapper νŒŒμΌμ—λŠ” μ΅œμƒμœ„ νƒœκ·Έλ‘œ <mapper> νƒœκ·Έλ₯Ό μž‘μ„±ν•˜κ³  λ„€μž„μŠ€νŽ˜μ΄μŠ€ 속성을 μž‘μ„±ν•΄μ„œ 래퍼 μΈν„°νŽ˜μ΄μŠ€μ˜ 경둜λ₯Ό μ§€μ •ν•©λ‹ˆλ‹€.
namespace="맀퍼 μΈν„°νŽ˜μ΄μŠ€ 경둜"
namespace의 μž‘μ„±ν•œ Mapper μΈν„°νŽ˜μ΄μŠ€μ˜ κ²½λ‘œκ°€ μΈν„°νŽ˜μ΄μŠ€ 파일의 κ²½λ‘œμ™€ 일치 ν•΄μ•Όν•©λ‹ˆλ‹€.
<?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"> <!-- namespace="맀퍼 μΈν„°νŽ˜μ΄μŠ€ 경둜" --> <mapper namespace="com.aloha.mybatis.mapper.XXXMapper"> </mapper>
XML
볡사

κΈ°λ³Έ CRUD Mapper 파일 μ½”λ“œ

SQL νƒœκ·Έ <select id="select" resultType="base"> Mapper μΈν„°νŽ˜μ΄μŠ€ λ©”μ†Œλ“œ public Base select(int baseNo) throws Exception;
각 SQL νƒœκ·Έμ˜ id 속성값이 Mapper μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ†Œλ“œ λͺ…κ³Ό μΌμΉ˜ν•΄μ•Όν•©λ‹ˆλ‹€.
λ‘œμ§μ„ ν†΅ν•΄μ„œ Mapper μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ†Œλ“œκ°€ 호좜이 되면, 이와 λ§€ν•‘λ˜μ–΄ μžˆλŠ” SQL mapper 파일의 ν•΄λ‹Ή λ©”μ†Œλ“œλͺ…κ³Ό μΌμΉ˜ν•˜λŠ” 아이디λ₯Ό 가진 SQL νƒœκ·Έκ°€ μ‹€ν–‰λ©λ‹ˆλ‹€
<?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"> <!-- namespace="맀퍼 μΈν„°νŽ˜μ΄μŠ€ 경둜" --> <mapper namespace="com.aloha.mybatis.mapper.BaseMapper"> <!-- κΈ°λ³Έ 등둝 --> <insert id="insert"> INSERT INTO base( base_id, base_name ) VALUES ( #{baseId}, #{baseName} ) </insert> <!-- κΈ°λ³Έ 쑰회 --> <select id="select" resultType="base"> SELECT * FROM base WHERE base_no = #{baseNo} </select> <!-- κΈ°λ³Έ μˆ˜μ • --> <update id="update"> UPDATE base SET base_id = #{baseId} ,base_name = #{baseName} WHERE base_no = #{baseNo} </update> <!-- κΈ°λ³Έ μˆ˜μ • --> <delete id="delete"> DELETE FROM base WHERE base_no = #{baseNo} </delete> </mapper>
XML
볡사

BoardMapper.xml

이번 μ‹€μŠ΅μ—μ„œλŠ” κ²Œμ‹œκΈ€ 데이터λ₯Ό 기본적으둜 등둝 쑰회 μˆ˜μ • μ‚­μ œν•˜λŠ” 쿼리듀을 λ‹€μŒκ³Ό 같이 SQL νƒœκ·Έλ‘œ μž‘μ„±ν•΄ λ΄…λ‹ˆλ‹€.
<?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"> <!-- namespace="맀퍼 μΈν„°νŽ˜μ΄μŠ€ 경둜" --> <mapper namespace="com.aloha.mybatis.mapper.BoardMapper"> <!-- κ²Œμ‹œκΈ€ λͺ©λ‘ --> <select id="list" resultType="Board"> SELECT * FROM board ORDER BY created_at DESC </select> <!-- κ²Œμ‹œκΈ€ 쑰회 --> <select id="select" resultType="Board"> SELECT * FROM board WHERE no = #{no} </select> <!-- κ²Œμ‹œκΈ€ 등둝 --> <insert id="insert"> INSERT INTO board( title, writer, content) VALUES ( #{title}, #{writer}, #{content} ) </insert> <!-- κ²Œμ‹œκΈ€ μˆ˜μ • --> <update id="update"> UPDATE board SET title = #{title} ,writer = #{writer} ,content = #{content} WHERE no = #{no} </update> <!-- κ²Œμ‹œκΈ€ μ‚­μ œ --> <delete id="delete"> DELETE FROM board WHERE no = #{no} </delete> <!-- κ²Œμ‹œκΈ€ 번호 μ΅œλŒ“κ°’ --> <select id="maxPk" resultType="int"> SELECT MAX(no) FROM board </select> </mapper>
XML
볡사

Mapper μΈν„°νŽ˜μ΄μŠ€ μž‘μ„±

λ§ˆμ΄λ°”ν‹°μŠ€ 맀퍼 μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ†Œλ“œλͺ…은 맀퍼 파일의 id 속성값과 μΌμΉ˜ν•΄μ•Όν•œλ‹€. - ν•΄λ‹Ή λ©”μ†Œλ“œκ°€ 호좜되면, 맀퍼 XML 파일의 SQLλ₯Ό μ‹€ν–‰ν•œλ‹€.
β€’
BoardMapper.java

BoardMapper.java

@Mapper public interface BoardMapper { // κ²Œμ‹œκΈ€ λͺ©λ‘ public List<Board> list() throws Exception; // κ²Œμ‹œκΈ€ 쑰회 public Board select(int no) throws Exception; // κ²Œμ‹œκΈ€ 등둝 public int insert(Board board) throws Exception; // κ²Œμ‹œκΈ€ μˆ˜μ • public int update(Board board) throws Exception; // κ²Œμ‹œκΈ€ μ‚­μ œ public int delete(int no) throws Exception; // κ²Œμ‹œκΈ€ 번호(κΈ°λ³Έν‚€) μ΅œλŒ“κ°’ public int maxPk() throws Exception; }
Java
볡사
맀퍼 파일의 id와 μΌμΉ˜ν•˜λŠ” λ©”μ†Œλ“œλͺ…을 μž‘μ„±ν•˜κ³ , 맀퍼 파일의 SQL νƒœκ·Έμ— 전달해 쀄 νŒŒλΌλ―Έν„°λ“€μ„ λ§€κ°œλ³€μˆ˜λ‘œ μž‘μ„±ν•©λ‹ˆλ‹€.
λ˜ν•œ 쑰회 쿼리의 κ²½μš°μ—λŠ” 쑰회 κ²°κ³Όλ₯Ό 맀핑 받을 객체 ν˜Ήμ€ μ»¬λ ‰μ…˜μ„ λ°˜ν™˜ νƒ€μž…μœΌλ‘œ μž‘μ„±ν•˜κ³ , 등둝, μˆ˜μ •, μ‚­μ œ, 쿼리의 κ²½μš°μ—λŠ” λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ 처리된 ν–‰μ˜ 수λ₯Ό λ°˜ν™˜λ°›κΈ° μœ„ν•˜μ—¬ int νƒ€μž…μœΌλ‘œ μž‘μ„±ν•©λ‹ˆλ‹€

Service μž‘μ„±

이제 μ„œλΉ„μŠ€ μΈν„°νŽ˜μ΄μŠ€μ™€ κ΅¬ν˜„ 클래슀λ₯Ό μž‘μ„±ν•©λ‹ˆλ‹€.
β€’
BoardService.java
β€’
BoardServiceImpl.java

BoardService.java

μ„œλΉ„μŠ€ μΈν„°νŽ˜μ΄μŠ€λ₯Ό ν†΅ν•΄μ„œ λΉ„μ¦ˆλ‹ˆμŠ€ λ‘œμ§μ—μ„œ μ‚¬μš©ν•  κΈ°λŠ₯에 λŒ€ν•œ λ©”μ†Œλ“œμ˜ ν˜•νƒœλ₯Ό μ •μ˜ν•©λ‹ˆλ‹€. μ΄λ ‡κ²Œ μΈν„°νŽ˜μ΄μŠ€λ‘œ λ©”μ†Œλ“œ ν˜•νƒœλ₯Ό μ •μ˜ν•¨μœΌλ‘œμ¨, μ‹€μ œλ‘œ κ΅¬ν˜„ν•  μ½”λ“œμ— λŒ€ν•΄μ„œ λ‹€ν˜•μ„±κ³Ό ν™•μž₯성을 μ œκ³΅ν•©λ‹ˆλ‹€.
public interface BoardService { // κ²Œμ‹œκΈ€ λͺ©λ‘ public List<Board> list() throws Exception; // κ²Œμ‹œκΈ€ 쑰회 public Board select(int no) throws Exception; // κ²Œμ‹œκΈ€ 등둝 public int insert(Board board) throws Exception; // κ²Œμ‹œκΈ€ μˆ˜μ • public int update(Board board) throws Exception; // κ²Œμ‹œκΈ€ μ‚­μ œ public int delete(int no) throws Exception; }
Java
볡사

BoardServiceImpl.java

κ΅¬ν˜„ ν΄λž˜μŠ€μ—μ„œλŠ” 맀퍼 μΈν„°νŽ˜μ΄μŠ€μ˜ μ μ ˆν•œ λ©”μ†Œλ“œλ‘œ ν˜ΈμΆœν•¨μœΌλ‘œμ¨ 데이터에 λŒ€ν•˜μ—¬ 쑰회, 등둝, μˆ˜μ •, μ‚­μ œ μš”μ²­μ„ 보낼 수 μžˆλ„λ‘ μ½”λ“œλ₯Ό μ™„μ„±ν•©λ‹ˆλ‹€. λ˜ν•œ 각 λ©”μ†Œλ“œμ—μ„œλŠ” μ„œλΉ„μŠ€μ˜ 각 κΈ°λŠ₯에 λŒ€ν•œ μΆ”κ°€ λ‘œμ§μ„ ν™•μž₯ν•˜κ±°λ‚˜ κ΄€λ ¨λœ 둜그 및 μ²˜λ¦¬λ“€μ„ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
@Service // μ„œλΉ„μŠ€ μ—­ν• μ˜ μŠ€ν”„λ§ 빈 public class BoardServiceImpl implements BoardService { @Autowired private BoardMapper boardMapper; /** * κ²Œμ‹œκΈ€ λͺ©λ‘ 쑰회 */ @Override public List<Board> list() throws Exception { // TODO : boardMapper 둜 list() 호좜 /* * ➑ List<Board> boardList 둜 λ°›μ•„μ˜΄ * ➑ return boardList */ List<Board> boardList = boardMapper.list(); return boardList; } /** * κ²Œμ‹œκΈ€ 쑰회 * - no λ§€κ°œλ³€μˆ˜λ‘œ κ²Œμ‹œκΈ€ 번호λ₯Ό μ „λ‹¬λ°›μ•„μ„œ * λ°μ΄ν„°λ² μ΄μŠ€μ— 쑰회 μš”μ²­ */ @Override public Board select(int no) throws Exception { // TODO : boardMapper 둜 select(no) 호좜 /* * ➑ Board board 둜 λ°›μ•„μ˜΄ * ➑ return board */ Board board = boardMapper.select(no); return board; } /** * κ²Œμ‹œκΈ€ 등둝 */ @Override public int insert(Board board) throws Exception { // TODO : boardMapper 둜 insert(Board) 호좜 /* * ➑ int result 둜 데이터 처리 ν–‰(개수) λ°›μ•„μ˜΄ * ➑ return result */ int result = boardMapper.insert(board); return result; } /** * κ²Œμ‹œκΈ€ μˆ˜μ • */ @Override public int update(Board board) throws Exception { // TODO : boardMapper 둜 update(Board) 호좜 /* * ➑ int result 둜 데이터 처리 ν–‰(개수) λ°›μ•„μ˜΄ * ➑ return result */ int result = boardMapper.update(board); return result; } /** * κ²Œμ‹œκΈ€ μ‚­μ œ */ @Override public int delete(int no) throws Exception { // TODO : boardMapper 둜 delete(no) 호좜 /* * ➑ int result 둜 데이터 처리 ν–‰(개수) λ°›μ•„μ˜΄ * ➑ return result */ int result = boardMapper.delete(no); return result; } }
Java
볡사

Controller μž‘μ„±

μ»¨νŠΈλ‘€λŸ¬μ—μ„œλŠ” μš”μ²­ 경둜λ₯Ό λ§€ν•‘ν•˜μ—¬ ν΄λΌμ΄μ–ΈνŠΈμ— μš”μ²­μ— λŒ€ν•˜μ—¬ μ μ ˆν•œ μ„œλΉ„μŠ€ 둜직 및 데이터λ₯Ό μ²˜λ¦¬ν•˜κ³ , ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ 응닡할 λ·° νŽ˜μ΄μ§€λ₯Ό μ§€μ •ν•©λ‹ˆλ‹€.
μš”μ²­ 경둜 맀핑
데이터 μš”μ²­
λ·° νŽ˜μ΄μ§€ 지정

BoardController.java

package com.aloha.mybatis.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.aloha.springmybatis.dto.Board; import com.aloha.springmybatis.service.BoardService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestBody; /** * /board 경둜둜 μš”μ²­ 왔을 λ•Œ 처리 * [GET] - /board/list : κ²Œμ‹œκΈ€ λͺ©λ‘ ν™”λ©΄ * [GET] - /board/read : κ²Œμ‹œκΈ€ 쑰회 ν™”λ©΄ * [GET] - /board/insert : κ²Œμ‹œκΈ€ 등둝 ν™”λ©΄ * [POST] - /board/insert : κ²Œμ‹œκΈ€ 등둝 처리 * [GET] - /board/update : κ²Œμ‹œκΈ€ μˆ˜μ • ν™”λ©΄ * [POST] - /board/update : κ²Œμ‹œκΈ€ μˆ˜μ • 처리 * [POST] - /board/delete : κ²Œμ‹œκΈ€ μ‚­μ œ 처리 */ @Slf4j // 둜그 μ–΄λ…Έν…Œμ΄μ…˜ @Controller // 컨트둀러 μŠ€ν”„λ§ 빈으둜 등둝 @RequestMapping("/board") // 클레슀 레벨 μš”μ²­ 경둜 맀핑 // - /board/~ 경둜의 μš”μ²­μ€ 이 μ»¨νŠΈλ‘€λŸ¬μ—μ„œ 처리 public class BoardController { // ⭐데이터 μš”μ²­κ³Ό ν™”λ©΄ 좜λ ₯ // Controller --> Service (데이터 μš”μ²­) // Controller <-- Service (데이터 전달) // Controller --> Model (λͺ¨λΈ 등둝) // View <-- Model (데이터 좜λ ₯) @Autowired // μ˜μ‘΄μ„± μžλ™ μ£Όμž… private BoardService boardService; // @Serviceλ₯Ό --Impl 에 등둝 /** * κ²Œμ‹œκΈ€ λͺ©λ‘ 쑰회 ν™”λ©΄ * @return * @throws Exception */ @GetMapping("/list") public String list(Model model) throws Exception { // 데이터 μš”μ²­ List<Board> boardList = boardService.list(); // λͺ¨λΈ 등둝 model.addAttribute("boardList", boardList); // λ·° νŽ˜μ΄μ§€ 지정 return "/board/list"; // resources/templates/board/list.html } /** * κ²Œμ‹œκΈ€ 쑰회 ν™”λ©΄ * - /board/read?no=πŸ’Ž * @param no * @return * @throws Exception */ // @RequestParam("νŒŒλΌλ―Έν„°λͺ…") // - μŠ€ν”„λ§ λΆ€νŠΈ 3.2버전 μ΄ν•˜, μƒλž΅ν•΄λ„ μžλ™ λ§€ν•‘λœλ‹€. // - μŠ€ν”„λ§ λΆ€νŠΈ 3.2버전 이상, ν•„μˆ˜λ‘œ λͺ…μ‹œν•΄μ•Ό λ§€ν•‘λœλ‹€. @GetMapping("/read") public String read(@RequestParam("no") int no, Model model) throws Exception { // 데이터 μš”μ²­ Board board = boardService.select(no); // λͺ¨λΈ 등둝 model.addAttribute("board", board); // λ·°νŽ˜μ΄μ§€ 지정 return "/board/read"; } /** * κ²Œμ‹œκΈ€ 등둝 ν™”λ©΄ * @return */ @GetMapping("/insert") public String insert() { return "/board/insert"; } /** * κ²Œμ‹œκΈ€ 등둝 처리 * @param board * @return * @throws Exception */ @PostMapping("/insert") public String insertPro(Board board) throws Exception { // 데이터 μš”μ²­ int result = boardService.insert(board); // λ¦¬λ‹€μ΄λ ‰νŠΈ // β­• 데이터 처리 성곡 if( result > 0 ) { return "redirect:/board/list"; } // ❌ 데이터 처리 μ‹€νŒ¨ return "redirect:/board/insert?error"; } /** * κ²Œμ‹œκΈ€ μˆ˜μ • ν™”λ©΄ * @param no * @param model * @return * @throws Exception */ @GetMapping("/update") public String update(@RequestParam("no") int no, Model model) throws Exception { Board board = boardService.select(no); model.addAttribute("board", board); return "/board/update"; } /** * κ²Œμ‹œκΈ€ μˆ˜μ • 처리 * @param board * @return * @throws Exception */ @PostMapping("/update") public String updatePro(Board board) throws Exception { int result = boardService.update(board); if( result > 0 ) { return "redirect:/board/list"; } int no = board.getNo(); return "redirect:/board/update?no="+ no + "&error"; } /** * κ²Œμ‹œκΈ€ μ‚­μ œ 처리 * @param no * @return * @throws Exception */ @PostMapping("/delete") public String delete(@RequestParam("no") int no) throws Exception { int result = boardService.delete(no); if( result > 0 ) { return "redirect:/board/list"; } return "redirect:/board/update?no=" + no + "&error"; } }
Java
볡사

View μž‘μ„±

β€’
board
β—¦
list.html
β—¦
insert.html
β—¦
read.html
β—¦
update.html
β€’
index.html

index.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>메인 ν™”λ©΄</title> </head> <body> <h1>SpringBoot x MyBatis</h1> <h3>κ²Œμ‹œνŒ ν”„λ‘œμ νŠΈ</h3> <ul> <li>Spring Boot 3.x</li> <li>Thymeleaf</li> <li>MySQL 8.x</li> <li>MyBatis 3.0</li> </ul> <div> <a href="/board/list">κ²Œμ‹œνŒ</a> </div> </body> </html>
HTML
볡사
β€’
board

list.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>κ²Œμ‹œκΈ€ λͺ©λ‘</title> </head> <body> <h1>κ²Œμ‹œκΈ€ λͺ©λ‘</h1> <a href="/board/insert">κΈ€μ“°κΈ°</a> <table border="1"> <tr> <th width="50">번호</th> <th width="300">제λͺ©</th> <th width="100">μž‘μ„±μž</th> <th width="200">λ“±λ‘μΌμž</th> <th width="200">μˆ˜μ •μΌμž</th> <th widht="100">쑰회수</th> </tr> <th:block th:each="board : ${boardList}"> <tr> <td align="center" th:text="${board.no}"></td> <td> <!-- th:???="|λ¬Έμžμ—΄+${ν‘œν˜„μ‹}|" --> <a th:href="|/board/read?no=${board.no}|" th:text="${board.title}"></a> </td> <td align="center" th:text="${board.writer}"></td> <!-- μœ ν‹Έλ¦¬ν‹°κ°μ²΄.λ©”μ†Œλ“œ() #dates.format( λ‚ μ§œκ°μ²΄λͺ…, 'λ‚ μ§œν¬λ§·' ) - #dates.format( board.regDate, 'yyyy-MM-dd HH:mm:ss' ) --> <td align="center"> <span th:text="${ #dates.format( board.createdAt, 'yyyy-MM-dd HH:mm:ss' ) }"></span> </td> <td align="center"> <span th:text="${ #dates.format( board.updatedAt, 'yyyy-MM-dd HH:mm:ss' ) }"></span> </td> <td align="center" th:text="${board.views}"></td> </tr> </th:block> </table> </body> </html>
HTML
볡사

insert.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>κ²Œμ‹œκΈ€ 등둝</title> </head> <body> <h1>κ²Œμ‹œκΈ€ 등둝</h1> <form action="/board/insert" method="post"> <input type="text" name="title" id="title"> <input type="text" name="writer" id="writer"> <input type="text" name="content" id="content"> <input type="submit" value="등둝"> </form> </body> </html>
HTML
볡사

read.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>κ²Œμ‹œκΈ€ 쑰회</title> </head> <body> <h1>κ²Œμ‹œκΈ€ 쑰회</h1> <form action="/board/delete" method="post"> <input type="hidden" name="no" th:value="${board.no}"> <table> <tr> <td>제λͺ©</td> <td> <input type="text" name="title" th:value="${board.title}"> </td> </tr> <tr> <td>μž‘μ„±μž</td> <td> <input type="text" name="writer" th:value="${board.writer}"> </td> </tr> <tr> <td colspan="2"> <textarea name="content" id="content" cols="40" rows="5" th:text="${board.content}"></textarea> </td> </tr> </table> <div> <button type="button" onclick="moveUpdate()">μˆ˜μ •</button> <button type="button" onclick="moveList()">λͺ©λ‘</button> </div> </form> <script> // πŸ‘©β€πŸ’» λͺ¨λΈ 객체λ₯Ό μžλ°”μŠ€ν¬λ¦½νŠΈλ‘œ κ°€μ Έμ˜€λŠ” 방법 let no = "[[${board.no}]]" // μˆ˜μ • ν™”λ©΄ 이동 function moveUpdate() { location.href = '/board/update?no=' + no } // λͺ©λ‘ ν™”λ©΄ 이동 function moveList() { location.href = '/board/list' } </script> </body> </html>
HTML
볡사

update.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>κ²Œμ‹œκΈ€ μˆ˜μ •</title> </head> <body> <h1>κ²Œμ‹œκΈ€ μˆ˜μ •</h1> <form id="form" action="/board/update" method="post"> <input type="hidden" name="no" th:value="${board.no}"> <table> <tr> <td>제λͺ©</td> <td> <input type="text" name="title" th:value="${board.title}"> </td> </tr> <tr> <td>μž‘μ„±μž</td> <td> <input type="text" name="writer" th:value="${board.writer}"> </td> </tr> <tr> <td colspan="2"> <textarea name="content" id="content" cols="40" rows="5" th:text="${board.content}"></textarea> </td> </tr> </table> <div> <button type="submit">μˆ˜μ •</button> <button type="button" onclick="actionDelete()">μ‚­μ œ</button> <button type="button" onclick="moveList()">λͺ©λ‘</button> </div> </form> <script> let form = document.getElementById('form') // πŸ‘©β€πŸ’» λͺ¨λΈ 객체λ₯Ό μžλ°”μŠ€ν¬λ¦½νŠΈλ‘œ κ°€μ Έμ˜€λŠ” 방법 let no = "[[${board.no}]]" // μˆ˜μ • ν™”λ©΄ 이동 function moveUpdate() { location.href = '/board/update?no=' + no } // μ‚­μ œ μš”μ²­ function actionDelete() { let check = confirm('μ •λ§λ‘œ μ‚­μ œν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?') if( check ) { form.action = '/board/delete' form.submit() } } // λͺ©λ‘ ν™”λ©΄ 이동 function moveList() { location.href = '/board/list' } </script> </body> </html>
HTML
볡사
κΈ°λŠ₯ λͺ…μ„Έμ„œ
β€’
κ²Œμ‹œκΈ€ 등둝
β€’
κ²Œμ‹œκΈ€ λͺ©λ‘ 쑰회
β€’
κ²Œμ‹œκΈ€ 쑰회
β€’
κ²Œμ‹œκΈ€ μˆ˜μ •
β€’
κ²Œμ‹œκΈ€ μ‚­μ œ

κ²Œμ‹œκΈ€ 등둝

κ²Œμ‹œκΈ€ 등둝 μ‹œ, μ‚¬μš©μžκ°€ /board/insert.html μ—μ„œ κ²Œμ‹œκΈ€ 정보 (제λͺ©, μž‘μ„±μž, λ‚΄μš©)λ₯Ό μž…λ ₯ν•˜κ³ , μ—¬λŸ¬ νŒŒμΌμ„ μ²¨λΆ€ν•˜μ—¬ μš”μ²­ν•œλ‹€.
β€’
κ²Œμ‹œκΈ€ 정보 등둝
β€’
파일 μ—…λ‘œλ“œ

κ²Œμ‹œκΈ€ 정보 등둝

β€’
Model
β—¦
BoardService.java
β–ͺ
insert(Board)
β—¦
BoardServiceImpl.java
β–ͺ
insert(Board)
β€’
κ²Œμ‹œκΈ€ 정보 등둝
β—¦
BoardMapper 의 insert(Board) 호좜
β€’
파일 μ—…λ‘œλ“œ
β—¦
FileService 의 upload(List<Files>) 호좜
β—¦
BoardMapper.java
β–ͺ
insert(Board)
β€’
BoardMapper.xml 의 id=”insert” 쿼리와 μ—°κ²°
β—¦
BoardMapper.xml
β–ͺ
<insert id=”insert” … >
β€’
적용된 데이터 ν–‰μ˜ 수λ₯Ό λ°˜ν™˜ ( 0 λ˜λŠ” 1 )
β€’
View
β—¦
~/board/insert.html
β–ͺ
[POST] λ°©μ‹μœΌλ‘œ FORM μš”μ²­
β€’
νŒŒλΌλ―Έν„°
β—¦
제λͺ© : title
β—¦
μž‘μ„±μž : writer
β—¦
λ‚΄μš© : content
β—¦
파일 : file
β€’
Controller
β—¦
BoardController.java
β–ͺ
@GetMapping(”/board/insert”)
β€’
응닡
β—¦
/board/insert
β–ͺ
@PostMapping(”/board/insert”)
β€’
@RequestBody Board board
β€’
BoardService 의 insert() λ©”μ†Œλ“œ μš”μ²­
β€’
응닡
β—¦
μš”μ²­ 성곡 : (redirect) /board/list
β—¦
μš”μ²­ μ‹€νŒ¨ : (redirect) /board/insert?error

κ²Œμ‹œκΈ€ λͺ©λ‘ 쑰회

κ²Œμ‹œκΈ€ 의 전체 λͺ©λ‘μ„ μ‘°νšŒν•©λ‹ˆλ‹€.
β€’
κ²Œμ‹œκΈ€ λͺ©λ‘ 전체 쑰회

κ²Œμ‹œκΈ€ λͺ©λ‘ - MVC

β€’
Model : BoardService
β€’
View : /board/list.html
β€’
Controller : BoardController.java

κ²Œμ‹œκΈ€ λͺ©λ‘ 쑰회

β€’
Model
β—¦
BoardService.java
β–ͺ
list()
β—¦
BoardServiceImpl.java
β–ͺ
list()
β—¦
BoardMapper.java
β–ͺ
list()
β€’
BoardMapper.xml 의 id=”list” 쿼리와 μ—°κ²°
β—¦
BoardMapper.xml
β–ͺ
<seledct id=”list” resultType=”Board” … >
β€’
resultType=”Board”
β—¦
쑰회된 board ν…Œμ΄λΈ”μ„ Board 객체둜 맀핑
β€’
View
β—¦
~/board/list.html
β€’
Controller
β—¦
BoardController.java
β–ͺ
@GetMapping(”/board/list”)
β€’
Model boardList
β€’
응닡
β—¦
/board/list

κ²Œμ‹œκΈ€ 쑰회

κ²Œμ‹œκΈ€ 정보 ν•œ 건을 μ‘°νšŒν•©λ‹ˆλ‹€.
β€’
κ²Œμ‹œκΈ€ 정보 쑰회
β€’
첨뢀 파일 λͺ©λ‘ 쑰회

κ²Œμ‹œκΈ€ 정보 쑰회

β€’
Model
β—¦
BoardService.java
β–ͺ
select(no)
β—¦
BoardServiceImpl.java
β–ͺ
select(no)
β—¦
BoardMapper.java
β–ͺ
select(no)
β€’
BoardMapper.xml 의 id=”select” 쿼리와 μ—°κ²°
β—¦
BoardMapper.xml
β–ͺ
<seledct id=”select” resultType=”Board” … >
β€’
resultType=”Board”
β—¦
쑰회된 board ν…Œμ΄λΈ”μ„ Board 객체둜 맀핑
β€’
View
β—¦
~/board/read.html
β€’
Controller
β—¦
BoardController.java
β–ͺ
@GetMapping(”/board/read”)
β€’
Model board
β€’
응닡
β—¦
/board/read

κ²Œμ‹œκΈ€ μˆ˜μ •

κ²Œμ‹œκΈ€ 정보 ν•œ 건을 μˆ˜μ •ν•©λ‹ˆλ‹€.
β€’
κ²Œμ‹œκΈ€ 정보 μˆ˜μ •
β€’
파일 μ‚­μ œ (AJAX)

κ²Œμ‹œκΈ€ 정보 μˆ˜μ •

β€’
Model
β—¦
BoardService.java
β–ͺ
update(board)
β—¦
BoardServiceImpl.java
β–ͺ
update(board)
β—¦
BoardMapper.java
β–ͺ
update(board)
β€’
BoardMapper.xml 의 id=”update” 쿼리와 μ—°κ²°
β—¦
BoardMapper.xml
β–ͺ
<update id=”update” … >
β€’
적용된 데이터 ν–‰μ˜ 수λ₯Ό λ°˜ν™˜ ( 0 λ˜λŠ” 1 )
β€’
View
β—¦
~/board/update.html
β€’
Controller
β—¦
BoardController.java
β–ͺ
@GetMapping(”/board/update”)
β€’
Model board
β€’
응닡
β—¦
/board/update
β–ͺ
@PostMapping(”/board/update”)
β€’
@RequestBody Board board
β€’
BoardService 의 update() λ©”μ†Œλ“œ μš”μ²­
β€’
응닡
β—¦
μš”μ²­ 성곡 : (redirect) /board/list
β—¦
μš”μ²­ μ‹€νŒ¨ : (redirect) /board/update?error

κ²Œμ‹œκΈ€ μ‚­μ œ

κ²Œμ‹œκΈ€ 정보 ν•œ 건을 μ‚­μ œν•©λ‹ˆλ‹€.
β€’
κ²Œμ‹œκΈ€ μ‚­μ œ
β€’
첨뢀 파일 μ‚­μ œ

κ²Œμ‹œκΈ€ μ‚­μ œ

β€’
Model
β—¦
BoardService.java
β–ͺ
delete(no)
β—¦
BoardServiceImpl.java
β–ͺ
delete(no)
β—¦
BoardMapper.java
β–ͺ
delete(no)
β€’
BoardMapper.xml 의 id=”delete” 쿼리와 μ—°κ²°
β—¦
BoardMapper.xml
β–ͺ
<delete id=”delete” … >
β€’
적용된 데이터 ν–‰μ˜ 수λ₯Ό λ°˜ν™˜ ( 0 λ˜λŠ” 1 )
β€’
View
β—¦
~/board/update.html μ—μ„œ [μ‚­μ œ] λ²„νŠΌμœΌλ‘œ μš”μ²­
β€’
Controller
β—¦
BoardController.java
β–ͺ
@GetMapping
β€’
β–ͺ
@PostMapping(”/board/delete”)
β€’
@RequestBody Board board
β€’
BoardService 의 delete() λ©”μ†Œλ“œ μš”μ²­
β€’
응닡
β—¦
μš”μ²­ 성곡 : (redirect) /board/list
β—¦
μš”μ²­ μ‹€νŒ¨ : (redirect) /board/update?error