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 |
๋๊ธ