ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐฑ์—”๋“œ/JSP

[JSP] MyBatis๋ž€ ?

doeunnkimm 2023. 4. 19. 12:20

๐Ÿš€ MyBatis๋ž€?

๊ธฐ์กด JSP์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ํ†ต์‹ ์„ ํ•˜๋ ค๋ฉด JDBC๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž๋ฐ”๋ฅผ ๋ฌด์กฐ๊ฑด ๊ฑฐ์ณ์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ž๋ฐ”์—์„œ ๋ถˆ๊ฐ€ํ”ผํ•˜๊ฒŒ SQL ์ฝ”๋“œ๊ฐ€ ์ž‘์„ฑ๋˜์—ˆ๊ณ , ์ž๋ฐ”์™€ SQL์ด ์„ž์ด๋‹ค ๋ณด๋ฉด ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๊ณ  ์„ž์—ฌ ์œ ์ง€๋ณด์ˆ˜์™€ ๋ถ„์—…์ด ์‰ฝ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ถˆํŽธํ•จ์„ ํ•ด์†Œํ•˜๊ธฐ ์œ„ํ•ด myBatis๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

MyBatis ๊ตฌ์กฐ

myBatis๋Š” ๊ธฐ์กด์˜ JDBC ๋ฐฉ์‹๊ณผ๋Š” ๋‹ฌ๋ฆฌ SQL๋ฌธ์„ XML ํŒŒ์ผ์— ์ž‘์„ฑํ•จ์œผ๋กœ์จ ์ฝ”๋“œ๊ฐ€ ์ค„์–ด๋“ค๊ณ , SQL๋ฌธ ์ˆ˜์ •์ด ํŽธํ•ด์ง‘๋‹ˆ๋‹ค. ๋˜ํ•œ DBCP(Databse Connection Pool)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปค๋„ฅ์…˜์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— JDBC๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์ž‘์—… ํšจ์œจ๊ณผ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง‘๋‹ˆ๋‹ค.

DBCP(Database Connection Pool)๋กœ DB์—ฐ๊ฒฐ

์‚ฌ์šฉ์ž ์š”์ฒญ์ด ์žˆ์„ ๋•Œ๋งˆ๋‹ค DB์—ฐ๊ฒฐ์„ ํ•œ๋‹ค๋ฉด, ๋งŽ์€ ์š”์ฒญ์ด ์žˆ์„ ๋•Œ ์—ฐ๊ฒฐ ์†๋„๊ฐ€ ์ €ํ•˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฏธ๋ฆฌ Connection์„ ๋งŒ๋“ค์–ด ๋‘๊ณ , ํ•„์š”์‹œ ์ €์žฅ๋œ ๊ณต๊ฐ„์—์„œ ๊ฐ€์ ธ๋‹ค ์“ด ํ›„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค.

MyBatis ์ž‘๋™์ˆœ์„œ

์‹œ์ž‘

1. ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด SqlSessionFactoryBuilder๋ฅผ ํ†ตํ•ด SqlSessionFactory๋ฅผ ๋นŒ๋“œํ•˜๋„๋ก ์š”์ฒญํ•œ๋‹ค

2. SqlSessionFactoryBuilder๋Š” SqlSessionFactory๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ MyBatis ๊ตฌ์„ฑํŒŒ์ผ์„ ์ฝ๋Š”๋‹ค

3. SqlSessionFactoryBuilder๋Š” MyBatis ๊ตฌ์„ฑ ํŒŒ์ผ์˜ ์ •์˜์— ๋”ฐ๋ผ SqlSessionFactory๋ฅผ ์ƒ์„ฑํ•œ๋‹ค

์š”์ฒญ

4. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์— ๋Œ€ํ•œ ํ”„๋กœ์„ธ์Šค(์‹คํ–‰๋œ ํ”„๋กœ๊ทธ๋žจ, ์“ฐ๋ ˆ๋“œํ’€์— ์š”์ฒญ)์„ ์š”์ฒญํ•œ๋‹ค.

5. ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์€ SqlSessionFactoryBuilder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋นŒ๋“œ๋œ SqlSessionFactory์—์„œ SqlSession์„ ๊ฐ€์ ธ์˜จ๋‹ค(๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹นํ•œ๋‹ค)

6. SqlSessionFactory๋Š” SqlSession์„ ์ƒ์„ฑํ•˜๊ณ  ์ด๋ฅผ Application์— ๋ฐ˜ํ™˜ํ•œ๋‹ค

7. ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด SqlSession์—์„œ ๋งคํผ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„ ๊ฐœ์ฒด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค

8. ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด ๋งคํผ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค

9. ๋งคํผ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„ ๊ฐœ์ฒด๊ฐ€ SqlSession์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  SQL์‹คํ–‰์„ ์š”์ฒญํ•œ๋‹ค

10. SqlSession์€ ๋งคํ•‘ ํŒŒ์ผ์—์„œ ์‹คํ–‰ํ•œ SQL์„ ๊ฐ€์ ธ์™€ SQL์„ ์‹คํ–‰ํ•œ๋‹ค

MyBatis ์ž‘์—…์ˆœ์„œ ์ •๋ฆฌ

1. ํ…Œ์ด๋ธ” ์ƒ์„ฑ

2. VO ๊ฐ์ฒด ์ƒ์„ฑ

3. mapper.xml์—์„œ ์ฟผ๋ฆฌ๋ฌธ ์ž‘์„ฑ

4. config.xml์— datasource ์„ค์ •

5. config.xml์— mapper.xml ๊ฒฝ๋กœ ๋“ฑ๋ก

6. SqlSessionFactory์— config.xml ์ „๋‹ฌ

7. DAO์—์„œ mapper.xml์— ์ž‘์„ฑํ•œ ์ฟผ๋ฆฌ ์‹คํ–‰

 

์œ„์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ํ•œ๋ฒˆ ์ž‘์—…์„ ํ•ด๋ด…์‹œ๋‹ค!

1. ํ…Œ์ด๋ธ” ์ƒ์„ฑ

์ €๋Š” DBeaver๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ๋„๊ตฌ๋ฅผ ํ†ตํ•ด MySQL์„ ์—ฐ๊ฒฐํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํ•œ ํ…Œ์ด๋ธ”์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

create database mybatis;
use mybatis;

create table tbl_member(
    member_id int unsigned auto_increment primary key,
    member_name varchar(500),
    member_age tinyint unsigned
);

2. VO๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ

1์—์„œ ๋งŒ๋“  ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ๊ณผ ๋™์ผํ•˜๊ฒŒ ํ•„๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ์ด๋•Œ VO(Value Object)๋Š” ์–ด๋– ํ•œ ๋กœ์ง๋„ ๊ฐ€์ง€์ง€ ์•Š์€ ์ˆœ์ˆ˜ํ•œ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ์œ„์—์„œ ํšŒ์›์„ ๊ด€๋ฆฌํ•  ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ง€๊ธˆ ์ž‘์„ฑํ•˜๋Š” VO์ด๋ฆ„๋„ MemberVO๋กœ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“œ src/com.mybatis.vo (package) / MemberVO.java ํŒŒ์ผ

package com.mybatis.vo;

public class MemberVO {
	private Long memberId;
	private String memberName;
	private int memberAge;
	
	public MemberVO() {;}

	public Long getMemberId() {
		return memberId;
	}

	public void setMemberId(Long memberId) {
		this.memberId = memberId;
	}

	public String getMemberName() {
		return memberName;
	}

	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}

	public int getMemberAge() {
		return memberAge;
	}

	public void setMemberAge(int memberAge) {
		this.memberAge = memberAge;
	}

	@Override
	public String toString() {
		return "MemberVO [memberId=" + memberId + ", memberName=" + memberName + ", memberAge=" + memberAge + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((memberId == null) ? 0 : memberId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MemberVO other = (MemberVO) obj;
		if (memberId == null) {
			if (other.memberId != null)
				return false;
		} else if (!memberId.equals(other.memberId))
			return false;
		return true;
	}
}

3. mapper.xml์—์„œ ์ฟผ๋ฆฌ๋ฌธ ์ž‘์„ฑ

mapper.xml์—์„œ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•  ๊ฑด๋ฐ, ๋ณดํ†ต ์„œ๋น„์Šค ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„๋กœ mapper ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ €๋Š” ์ด๋ฒˆ์— ํšŒ์›๊ด€๋ฆฌ ์„œ๋น„์Šค์™€ ๊ด€๋ จ๋œ ๋กœ์ง ๋ฐ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•  ๊ฒƒ์ด๋ฏ€๋กœ memberMapper.xml ์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ mapper ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์„œ ์ž‘์—…ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“œ src/com.mybatis.mapper (package) / memberMapper.xml ํŒŒ์ผ

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="member">
    <insert id="insert" parameterType="memberVO">
        insert into tbl_member(member_name, member_age)
        values(#{memberName}, #{memberAge})
    </insert>
    
    <select id="select" resultType="long">
        select member_id from tbl_member where member_name=#{memberName} and member_age=#{memberAge}
    </select>
    
    <select id="selectAll" resultType="memberVO">
        select member_id, member_name, member_age from tbl_member
    </select>
</mapper>

4, 5. config.xml์— datasource ์„ค์ • & mapper.xml ๊ฒฝ๋กœ ๋“ฑ๋ก

๐Ÿ“œ src/com.mybatis.config (package) / 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>
		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>

	<typeAliases>
		<typeAlias type="com.mybatis.vo.MemberVO" alias="memberVO"/>
	</typeAliases>

	<environments default="mysql">
		<environment id="mysql">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
				<property name="username" value="root"/>
				<property name="password" value="1234"/>
			</dataSource>
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="com/mybatis/mapper/memberMapper.xml"/>
	</mappers>
</configuration>

6. SqlSessionFactory์— config.xml ์ „๋‹ฌ

๐Ÿ“œ src/com.mybatis.config (package) / MyBatisConfig.java ํŒŒ์ผ

package com.mybatis.config;

import java.io.IOException;
import java.io.Reader;

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

public class MyBatisConfig {
	private static SqlSessionFactory sqlSessionFactory;
	
	static {
		String resource = "com/mybatis/config/config.xml";
		try {
			Reader reader = Resources.getResourceAsReader(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}
}

7. DAO์—์„œ mapper์— ์ž‘์„ฑํ•œ ์ฟผ๋ฆฌ ์‹คํ–‰ํ•˜๋Š” ๋กœ์ง ํ•จ์ˆ˜ ์ž‘์„ฑ

๐Ÿ“œ src/com.mybatis.dao (package) / MemberDAO.java ํŒŒ์ผ

package com.mybatis.dao;

public class MemberDAO {
    public SqlSession sqlSession;
    
    public MemberDAO() {
        sqlSession = MyBatisConfig.getSqlSessionFactory().openSession(true);
    }
    
    // ํšŒ์› ๊ฐ€์ž…
    public void insert(MemberVO memberVO) {
        sqlSession.insert("member.insert", memberVO);
    }
    
    // ์ „์ฒด ์กฐํšŒ
    public List<MemberVO> selectAll() {
        return sqlSession.selectAll("member.selectAll");
    }
}

8. ์‹คํ–‰ํ•ด๋ณด์ž

๐Ÿ“œ src/ com.mybatis.dao (package) / Test.java ํŒŒ์ผ

package com.mybatis.dao;

public class Test {
    public static void main(String[] args) {
        MemberDAO memberDAO = new MemberDAO();
        MemberVO memberVO = new MemberVO();
        
        memberVO.setMemberName("์ด์ˆœ์‹ ");
        memberVO.setMemberAge(20);
        
        memberDAO.insert(memberVO);
    }
}

์‹คํ–‰ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ํ™•์ธํ•˜๋ฉด insert ๋˜์–ด ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค :)

728x90
LIST