๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JAVA/MyBatis

[JAVA] MyBatis ์‹œ์ž‘ํ•˜๊ธฐ (with Delegation Pattern)

by soy๋ฏธ๋‹ˆ 2021. 9. 23.

 

Mybatis ๋ž€?

  • ์ž๋ฐ” ๊ฐ์ฒด์™€ SQL ์„ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” SQL ๋งคํผ ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค.
  • JDBC ๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•ด์„œ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ SQL ๋ฌธ๊ณผ ์ž๋ฐ” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ตฌ๋ถ„ํ•ด์„œ ์ž‘์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋…์„ฑ์ด ๋†’๋‹ค.
  • ์ž๋ฐ” ํŒŒ์ผ์ด ์•„๋‹Œ XML์— ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฟผ๋ฆฌ๋ฌธ์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ปดํŒŒ์ผ์„ ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

 

 

1. maven project ์ƒ์„ฑ, Mybatis dependency(pom.xml) ์ž‘์„ฑ

<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>Mybatis01</groupId>
	<artifactId>Mybatis01</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.26</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.5.7</version>
		</dependency>

		<dependency>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.8.1</version>
		</dependency>
	</dependencies>
</project>

 

 

 

2. src/main/resources ์— mybatis-config.xml ์ž‘์„ฑ

์ฐธ๊ณ  : https://mybatis.org/mybatis-3/ko/getting-started.html

<?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.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/db01"/>
        <property name="username" value="mysql ์•„์ด๋””"/>
        <property name="password" value="mysql ๋น„๋ฒˆ"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="Mapper.xml"/>
  </mappers>
</configuration>
  • mysql ๊ณผ ์—ฐ๋™ํ•  Connection ์ •๋ณด๋ฅผ property์— ์ž…๋ ฅ
  • Mapper.xml ์— ์ฟผ๋ฆฌ๋ฌธ ์ž‘์„ฑ

 

 

 

3. src/main/resources ์— Mapper.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="Mapper">
	<insert id="test01" parameterType="map">
		insert into table01(age, fullname) values (#{age}, #{fullname})
	</insert>
	<insert id="test011" parameterType="int">
		insert into table02 values (#{age})
	</insert>
	<delete id="test02" parameterType="int">
		delete from table01 where age=#{age}
	</delete>
	<update id="test03" parameterType="map">
		update table01 set age=#{age} where fullname=#{fullname}
	</update>
	<update id="test033" parameterType="int">
		update table02 set age=age+10 where age=#{age}
	</update>
	<select id="test04" resultType="map">
		select * from table01
	</select>
	<select id="test044" resultType="int">
		select * from table02
	</select>
</mapper>
  • insert ํƒœ๊ทธ ์•ˆ์— insert ์ฟผ๋ฆฌ๋ฌธ ์ž‘์„ฑ
  • id ๋Š” ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ๋ถ€๋ฅผ ์ด๋ฆ„, ๋‚˜์ค‘์— ๋ฉ”์ธ ํŒŒ์ผ์—์„œ ํ•ด๋‹น ์ฟผ๋ฆฌ๋ฌธ์„ ์‹คํ–‰ํ•  ๋•Œ ์ž…๋ ฅํ•œ๋‹ค.
  • ์ „๋‹ฌํ•  ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ 2๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ parameterType ์„ map ์œผ๋กœ ์„ค์ •
  • select ๋Š” ์ „๋‹ฌํ•  ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๊ณ  db ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— parameterType์ด ์•„๋‹Œ resultType ์œผ๋กœ ์„ค์ •

 

 

 

4. Hello.java ์ž‘์„ฑ

package Pack01;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Hello {
	public static void main(String[] args) {
		SqlSessionFactory ssf = null;
		SqlSession session = null;
		InputStream is = null;
		
		System.out.println(1);
		try {
			is = Resources.getResourceAsStream("mybatis-config.xml");
		} catch (Exception e) {
			e.printStackTrace();
		}
		ssf = new SqlSessionFactoryBuilder().build(is);
		
		System.out.println(2);
		session = ssf.openSession();
		
		try {
//			Map<String, Object> param = new HashMap(); 
//			param.put("age", 20); 
//			param.put("fullname", "์ Š์€์ด");
//			int result = session.insert("test01", param);
//			int result = session.delete("test02", 9999);
//			Map<String, Object> param = new HashMap();
//			param.put("age", 23);
//			param.put("fullname", "ํ™๊ธธ๋™");
//			int result = session.update("test03", param);
			List<Integer> mm = session.selectList("test04");
			for(Integer item : mm) {
				System.out.println(item+" ");
			}
			System.out.println();
//			System.out.println("result: "+result);
//			if(result > 0) {
//				session.commit();
//			}
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println(3);
	}

}
  • Resources.getResourceAsStream("mybatis-config.xml") ์œผ๋กœ mysql db ์—ฐ๊ฒฐ
  • ssf = new SqlSessionFactoryBuilder().build(is); ์œผ๋กœ db ์—ฐ๋™ํ•œ ์„ธ์…˜ ์—ด์–ด์ฃผ๊ธฐ
  • session.insert("test01", 9999); ์œผ๋กœ ์•ž์„œ ๋งŒ๋“ค์–ด๋‘” insert ํ•จ์ˆ˜์ธ test01์— ์ ‘๊ทผํ•˜๊ณ  insert ๋ฌธ ์ˆ˜ํ–‰
  • insert ๋์œผ๋ฉด session.commit() ํ•˜๊ณ  close
  • session.commit() ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์šฉ๋„์ด๋ฏ€๋กœ select ์‹คํ–‰ ํ•  ๋•Œ๋Š” ํ•„์š” ์—†๋‹ค.

 

๋‹จ์ผ ๋งค๊ฐœ๋ณ€์ˆ˜, insert / select / update ํ•จ์ˆ˜๋กœ ๊ตฌ๋ถ„

package Pack01;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

class UserDao{
	SqlSessionFactory ssf;
	
	UserDao(){
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			ssf = new SqlSessionFactoryBuilder().build(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//session = ssf.openSession();
	}
	
	void insert(int num) {
		SqlSession session = ssf.openSession();
		try {
			int result = session.insert("test011", num);
			System.out.println("result: "+result);
			if(result > 0) {
				session.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			session.close();
		}
	}
	
	void update(int num) {
		SqlSession session = ssf.openSession();
		try {
			int result = session.insert("test033", num);
			System.out.println("result: "+result);
			if(result > 0) {
				session.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			session.close();
		}
	}
	
	void showAge() {
		SqlSession session = ssf.openSession();
		try {
			List<Integer> mm = session.selectList("test044");
			for(Integer item : mm) {
				System.out.println(item+" ");
			}
			System.out.println();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			session.close();
		}
	}
	
}

public class Hello {
	public static void main(String[] args) {
		UserDao dao = new UserDao();
//		dao.insert(1234);
		dao.update(80);
		dao.showAge();
	}
}
  • UserDao ๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ๊ทธ ๋‚ด๋ถ€์— insert, update, select ์™€ ๊ฐ™์€ SQL ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์‚ฌ์šฉํ•  ๋•Œ๋งˆ๋‹ค main ํ•จ์ˆ˜์—์„œ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

 

 

์—ฌ๋Ÿฌ ๋งค๊ฐœ๋ณ€์ˆ˜

package Pack01;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

class UserDao{
	SqlSessionFactory ssf;

	UserDao(){
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			ssf = new SqlSessionFactoryBuilder().build(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//session = ssf.openSession();
	}

	void insert(int num) {
		SqlSession session = ssf.openSession();
		try {
			Map<String, Object> param = new HashMap();
			param.put("age", 10);
			param.put("fullname","๋ƒฅ์ด");
			int result = session.insert("test01", param);
			System.out.println("result: "+result);
			if(result > 0) {
				session.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			session.close();
		}
	}

	void update(int num) {
		SqlSession session = ssf.openSession();
		try {
			Map<String, Object> param = new HashMap();
			param.put("age", 28);
			param.put("fullname","์ฒญ๋…„");
			int result = session.update("test03", param);
			System.out.println("result: "+result);
			if(result > 0) {
				session.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			session.close();
		}
	}

	void showAge() {
		SqlSession session = ssf.openSession();
		try {
			Map<String, Object> mm = session.selectMap("test04","fullname","age");
			System.out.println(mm);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			session.close();
		}
	}

}

public class Hello {
	public static void main(String[] args) {
		UserDao dao = new UserDao();
		//		dao.insert(1234);
		//dao.update(80);
		dao.showAge();
	}
}
  • ๋‹จ์ผ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์•„๋‹Œ map ์„ ์ด์šฉํ•ด์„œ ์—ฌ๋Ÿฌ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ›์•„์˜ค๊ธฐ

 

 

 

Delegation ํŒจํ„ด์œผ๋กœ ๋ฆฌํŒฉํ† ๋ง

 

Delegation Pattern ์ด๋ž€?

  • ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ง์ ‘ ์ผ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ๊ทธ ์ผ์„ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  • ์ฒ˜๋ฆฌ๋˜๋Š” ๋ฉ”์„œ๋“œ ์ค‘ ํŠน์ • ๋ถ€๋ถ„์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์œ ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ Delegation Pattern ์€ ์šฉ์ดํ•˜๋‹ค.
  • ์ฝ”๋“œ ์œ ์ง€ ๋ณด์ˆ˜์— ์šฉ์ดํ•˜๋‹ค.
  • ๋ฐ˜๋ณตํ•ด์„œ ์‚ฌ์šฉ๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์ค„์ด๊ณ  ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
package Pack01;


class Tiger{
	void m1() { System.out.println(1); }
	void m2() { System.out.println(2); }
	void m3() { System.out.println(3); }
	void m4() { System.out.println(4); }
	void m5() { System.out.println(5); }
}

interface Delegate {
	void delegate(Tiger t);
}

class Proxy {
	void command(Delegate d) {
		Tiger t = new Tiger();
		t.m1();
		d.delegate(t);
		t.m5();
	}
}

public class World {
	public static void main(String[] args) {
		Proxy proxy = new Proxy();
		proxy.command(new Delegate() {
			public void delegate(Tiger t) {
				t.m3();
			}
		});
		System.out.println("-------------------------------");
		proxy.command(new Delegate() {
			public void delegate(Tiger t) {
				t.m2();
			}
		});
		System.out.println("-------------------------------");
		proxy.command(new Delegate() {
			public void delegate(Tiger t) {
				t.m4();
			}
		});
	}
}

 

 

Delegation Pattern ์‹ค์Šต

package Pack01;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

class Tiger{
	SqlSessionFactory ssf;
	SqlSession session;
	void m1(){
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			ssf = new SqlSessionFactoryBuilder().build(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
		session = ssf.openSession();
	}
	
	void m5(int r) {
		try {
			int result = r;
			System.out.println("result: "+result);
			if(result > 0) {
				session.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			session.close();
		}
	}
	
	int insert() { 	
		int result = session.insert("test011", 1234);
		System.out.println("insert");
		return result;
	}
	int update() { 
		int result = session.update("test033", 7777);
		System.out.println("update");
		return result;
	}
}

interface Delegate {
	int delegate(Tiger t);
}

class Proxy {
	void command(Delegate d) {
		Tiger t = new Tiger();
		t.m1();
		int result = d.delegate(t);
		t.m5(result);
	}
}

public class World {
	public static void main(String[] args) {
		Proxy proxy = new Proxy();
		proxy.command(new Delegate() {
			public int delegate(Tiger t) {
				return t.insert();
			}
		});
		System.out.println("-------------------------------");
		
		proxy.command(new Delegate() {
			public int delegate(Tiger t) {
				return t.update();
			}
		});

	}
}
  • Tiger ํด๋ž˜์Šค์˜ m1 ๋ฉ”์„œ๋“œ์™€ m5 ๋ฉ”์„œ๋“œ๋Š” ๊ณ ์ •์ด๋‹ค.
  • m1()์€ ์ฒซ ๋ฒˆ์งธ๋กœ ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ์ด๊ณ  m5() ์€ ๋งˆ์ง€๋ง‰์œผ๋กœ ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
  • insert() ์™€ update() ๋Š” int ํƒ€์ž…์„ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์„œ๋“œ๋กœ main ํ•จ์ˆ˜์—์„œ Proxy ๊ฐ์ฒด ๋‚ด์—์„œ ์ธํ„ฐํŽ˜์ด์Šค์ธ Delegate์˜ delegate ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜์–ด t.insert() ์™€ t.update() ๋ฅผ ๋ฆฌํ„ดํ•  ๊ฒฝ์šฐ Proxy ๊ฐ์ฒด์˜ command ํ•จ์ˆ˜์—์„œ ๊ทธ ๋ฆฌํ„ด๊ฐ’์„ ๋ฐ›์•„์„œ m1 ๊ณผ m5 ๋ฉ”์„œ๋“œ ์‚ฌ์ด์— insert์™€ update ๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

 

Delegation Pattern ๋ฆฌํŒฉํ† ๋ง

package Pack01;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

class UserDao{
	SqlSession session;
	SqlSessionFactory ssf;
	
	UserDao(){
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			ssf = new SqlSessionFactoryBuilder().build(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.session = ssf.openSession();
	}

	void insert(final int num) { 	
		Proxy.command(this, new Delegate() {
			public int delegate(SqlSession session) {
				return session.insert("test011", num);
			}
		});
	}

	void update(final int num) {
		Proxy.command(this, new Delegate() {
			public int delegate(SqlSession session) {
				return session.update("test033", num);
			}
		});
	}
} 

interface Delegate {
	int delegate(SqlSession session);
}

class Proxy {
	static void command(UserDao dao, Delegate d) {
		SqlSession session = dao.ssf.openSession();
		try {
			int result = d.delegate(session);
			System.out.println("result: "+result);
			if(result > 0) {
				session.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
}

public class World {
	public static void main(String[] args) {
		UserDao dao = new UserDao();
		dao.insert(40);
		System.out.println("-------------------------------");
		dao.update(20);


	}
}

 

ํ—ค๋งธ๋˜ ๋ถ€๋ถ„

  • main() ๋ฉ”์„œ๋“œ์—์„œ dao.insert() ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰์ด ๋˜๋ฉด UserDao ํด๋ž˜์Šค ๋‚ด์˜ insert() ๋ฉ”์„œ๋“œ๊ฐ€ ์ˆ˜ํ–‰๋œ๋‹ค. ์ด๋•Œ Proxy.command() ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ delegate๊ฐ€ ์ˆ˜ํ–‰๋˜๋ฉด SqlSession ๊ฐ์ฒด๋ฅผ delegate() ๋ฉ”์„œ๋“œ์— ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•ด์„œ session ์˜ open / close ์ƒํƒœ๋ฅผ ๋„˜๊ธฐ๋Š” ๋ฐฉ์‹์œผ๋กœ session์˜ ์ƒํƒœ ์œ ์ง€๋ฅผ ํ•ด์•ผ ์ •์ƒ์ ์œผ๋กœ open๊ณผ close ๊ฐ€ ๋˜๋Š”๋ฐ ๊ทธ ๋ถ€๋ถ„์„ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•˜๊ณ  ํ•˜๋‚˜์˜ session ๊ฐ์ฒด๋ฅผ ๊ณ„์† ๋ถˆ๋Ÿฌ์˜ค๋‹ค๋ณด๋‹ˆ insert ์™€ update ๋ฉ”์„œ๋“œ ์ค‘์—์„œ ๋จผ์ € ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ๋งŒ ์ •์ƒ ์‹คํ–‰์ด ๋˜๊ณ  ๋’ค์— ๋ถˆ๋Ÿฌ์ง€๋Š” ๋ฉ”์„œ๋“œ๋Š” ์‹คํ–‰์ด ๋˜์ง€ ์•Š์•˜๋‹ค. executor was closed ์—๋Ÿฌ๊ฐ€ ์ž๊พธ ๋ฐœ์ƒํ•ด์„œ ๋ญ๊ฐ€ ๋ฌธ์ œ์ธ๊ฐ€ ๋ณด๋‹ค๊ฐ€ session ์ƒํƒœ ์œ ์ง€๊ฐ€ ๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๋•Œ๋ฌธ์— SqlSession session ์„ delegate() ๋ฉ”์„œ๋“œ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ค˜์„œ insert()๊ฐ€ ์‹คํ–‰๋˜๋ฉด session ์„ ๋ถ€์—ฌํ•ด์ฃผ๊ณ  open ๋ถ€ํ„ฐ close ๊นŒ์ง€ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜๊ฒŒ๋” ํ•œ ๋‹ค์Œ update()๊ฐ€ ์‹คํ–‰๋˜๋ฉด ๋‹ค์‹œ session ์„ ๋ถ€์—ฌํ•ด์ค˜์„œ open ๋ถ€ํ„ฐ close ๊นŒ์ง€ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

'JAVA > MyBatis' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Java] MyBatis xml ์ž‘์„ฑ  (0) 2023.09.18
[JAVA] Spring์—์„œ Mybatis ์—ฐ๋™  (0) 2021.09.24

๋Œ“๊ธ€