๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP/Library·Framework

[JAVA/WEB] ์›น ํ”„๋กœ์ ํŠธ์— MyBatis ์„ธํŒ… ๋ฐ ์ ์šฉํ•˜๊ธฐ

by Leica 2020. 2. 23.
๋ฐ˜์‘ํ˜•
  • ๊ด€๋ จ ๊ธ€

- MyBatis ์„ค์ • ํŒŒ์ผ - SQL Mapper ์ž‘์„ฑ ๋ฐฉ๋ฒ•

- MyBatis ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ•


[JAVA/WEB] ์›น ํ”„๋กœ์ ํŠธ์— MyBatis ์„ธํŒ… ๋ฐ ์ ์šฉํ•˜๊ธฐ

ํ™˜๊ฒฝ ๋ฐ ๋ฒ„์ „ ์ •๋ณด

IntelliJ IDEA 2019.3 Ultimate, JDK 11, Tomcat 9, MyBatis 3

 

MyBatis

Persistence framework ์ค‘ ํ•˜๋‚˜์ธ MyBatis๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—†์ด DB์™€ ์—ฐ๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐœ๋ฐœ์ž๊ฐ€ JDBC API๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  MyBatis๊ฐ€ ๋Œ€์‹  ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

MyBatis์˜ ํ•ต์‹ฌ์€ ๊ฐœ๋ฐœ๊ณผ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์‰ฝ๋„๋ก ์†Œ์Šค ์ฝ”๋“œ์— ๋‚ด์žฅ๋ผ์žˆ๋Š” SQL์„ ๋ณ„๋„์˜ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ ๋‹จ์ˆœํ•˜๊ณ  ๋ฐ˜๋ณต์ ์ธ JDBC ์ฝ”๋“œ๋ฅผ ์บก์Šํ™”ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค.

 

1. MyBatis ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค€๋น„

https://github.com/mybatis ์— ์ ‘์†ํ•œ๋‹ค.

 

Pinned repositories์—์„œ mybatis-3์„ ํด๋ฆญํ•œ๋‹ค.

 

README.md์—์„œ Download Latest๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

Assets์—์„œ mybatis-3.5.4.zip์„ ํด๋ฆญํ•œ๋‹ค.

 

์••์ถ•์„ ํ’€๋ฉด JAR ํŒŒ์ผ๊ณผ ์‚ฌ์šฉ ์„ค๋ช… PDF ํŒŒ์ผ, ์˜์กด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ JARํŒŒ์ผ์ด ์žˆ๋‹ค.

 

mybatis-3.5.4.jar ํŒŒ์ผ๊ณผ ์˜์กด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ๋“ค์„ ์›น ํ”„๋กœ์ ํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค.

 

IntelliJ IDEA Project Structure - dependencies

 

2. DAO ๋ณ€๊ฒฝ

MyBatis ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์ด์ œ DB ์—ฐ๋™์€ MyBatis๊ฐ€ ๋Œ€์‹ ํ•œ๋‹ค.

JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋Œ€์‹ , MyBatis์˜ SqlSessionFactory ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝํ•˜๊ณ  DAO์˜ SQL๋ฌธ์„ ๋ณ„๋„์˜ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•  ์ฐจ๋ก€์ด๋‹ค.

 

1) SqlSessionFactory

1
2
3
4
5
6
// myBatis - SqlSessionFactory: SqlSession ๊ฐ์ฒด ์ƒ์„ฑ
private SqlSessionFactory sqlSessionFactory;
 
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
    this.sqlSessionFactory = sqlSessionFactory;
}
cs

์šฐ์„  DAO ํด๋ž˜์Šค์— SqlSessionFactory ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜์™€ setter๋ฅผ ์„ ์–ธํ•œ๋‹ค.

MyBatis์—์„œ๋Š” SqlSession ๊ฐ์ฒด๊ฐ€ ์‹ค์ œ SQL์„ ์‹คํ–‰ํ•˜๋Š”๋ฐ, SqlSession ๊ฐ์ฒด๋Š” SqlSessionFactory๋กœ๋ถ€ํ„ฐ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

DAO๊ฐ€ ํ•„์š”๋กœํ•˜๋Š” SqlSessionFactory๋Š” ์™ธ๋ถ€์—์„œ ์ฃผ์ž…๋  ์ˆ˜ ์žˆ๋„๋ก setter๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

2) Select

๋จผ์ € MyBatis๋ฅผ ์ด์šฉํ•œ select ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

 

selectList()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public List<Project> selectList() throws SQLException {
 
    // myBatis - SqlSession: SQL ์‹คํ–‰
    // openSession(): SqlSession ์–ป๊ธฐ
    SqlSession sqlSession = sqlSessionFactory.openSession();
 
    try {
        // selectList(): SELECT
        // ํŒŒ๋ผ๋ฏธํ„ฐ: SQL ๋งตํผ์˜ ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค(com.atoz_develop.spms.dao.ProjectDao) + SQL๋ฌธ ID(selectList)
        return sqlSession.selectList("com.atoz_develop.spms.dao.ProjectDao.selectList");
    } finally {
        sqlSession.close();
    }
}
cs

SqlSessionFactory ๊ฐ์ฒด์˜ openSession()์„ ํ˜ธ์ถœํ•ด์„œ SqlSession์„ ์–ป๋Š”๋‹ค.

SqlSession์€ MyBatis์—์„œ ์‹ค์ œ SQL ์‹คํ–‰์„ ๋‹ด๋‹นํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์ด๋‹ค. ์ฆ‰ SQL์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด SqlSession์ด ํ•„์š”ํ•˜๋ฉฐ, ์ด ๊ฐ์ฒด๊ฐ€ JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

SqlSession ๊ฐ์ฒด์˜ selectList()๋ฅผ ํ˜ธ์ถœํ•ด์„œ select๋ฌธ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ๋Š” SQL mapper์˜ ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค์™€ SQL๋ฌธ id๋ฅผ ํ•ฉ์นœ ๋ฌธ์ž์—ด์„ ๋„˜๊ธด๋‹ค.

์œ„ ์ฝ”๋“œ์—์„œ com.atoz_develop.spms.dao.ProjectDao๊ฐ€ ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค, selectList๊ฐ€ SQL๋ฌธ id์— ํ•ด๋‹นํ•œ๋‹ค.

 

๋งˆ์ง€๋ง‰์œผ๋กœ finally ๋ธ”๋ก์—์„œ SqlSession ๊ฐ์ฒด์˜ close()๋ฅผ ํ˜ธ์ถœํ•ด์„œ SQL๋ฌธ์„ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉํ•œ ์ž์›์„ ํ•ด์ œํ•ด์ค€๋‹ค.

 

selectOne()

1
2
3
4
5
6
7
8
9
10
@Override
public Project selectOne(int no) throws SQLException {
 
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        return sqlSession.selectOne("com.atoz_develop.spms.dao.ProjectDao.selectOne", no);
    } finally {
        sqlSession.close();
    }
}
cs

ํ•œ ๊ฐœ ๋ฐ์ดํ„ฐ๋ฅผ select ํ•ด์˜ฌ ๋•Œ๋Š” selectOne() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

selectList()์™€ ๋‹ฌ๋ฆฌ ๋‘ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์กฐ๊ฑด์ ˆ์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธด๋‹ค.

์ด ๋‘ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ํƒ€์ž…์€ Object, ์ฆ‰ ๊ฐ์ฒด๋ฅผ ๋„˜๊ฒจ์•ผ ํ•˜์ง€๋งŒ int์™€ ๊ฐ™์€ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋„˜๊ฒจ๋„ ์ปดํŒŒ์ผ ์‹œ auto boxing์ด ์ˆ˜ํ–‰๋ผ์„œ ๋ณ€ํ™˜ ์—†์ด ๊ทธ๋Œ€๋กœ ๋„˜๊ฒจ๋„ ๋œ๋‹ค.

 

3) Insert

insert()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public int insert(Project project) throws SQLException {
//        SqlSession sqlSession = sqlSessionFactory.openSession(true);  // auto commit
    SqlSession sqlSession = sqlSessionFactory.openSession();
 
    try {
        // insert(): INSERT
        int count = sqlSession.insert("com.atoz_develop.spms.dao.ProjectDao.insert", project);
        sqlSession.commit();
 
        return count;
    } finally {
        sqlSession.close();
    }
}
cs

Insert๋ฌธ์€ SqlSession ๊ฐ์ฒด์˜ insert()๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

selectList()์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” SQL mapper์˜ ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค์™€ SQL id๋ฅผ ํ•ฉ์นœ ๋ฌธ์ž์—ด์„ ๋„˜๊ธด๋‹ค.

๋‘ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ๋Š” insert๋ฌธ์„ ์‹คํ–‰ํ•  ๋•Œ ํ•„์š”ํ•œ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง„ VO ๊ฐ์ฒด๋ฅผ ๋„˜๊ธด๋‹ค.

 

insert() ํ˜ธ์ถœ ํ›„ commit()์„ ํ˜ธ์ถœํ•œ๋‹ค.

SqlSession ๊ฐ์ฒด๋ฅผ ์–ป์„ ๋•Œ openSession(true)์™€ ๊ฐ™์ด ํ˜ธ์ถœํ•˜๋ฉด INSERT, UPDATE, DELETE๋ฌธ ์‹คํ–‰ ์‹œ auto commit์„ ์ˆ˜ํ–‰ํ•˜๋Š” SqlSession ๊ฐ์ฒด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

4) Update

update()

1
2
3
4
5
6
7
8
9
10
11
12
@Override
public int update(Project project) throws SQLException {
 
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        int count = sqlSession.update("com.atoz_develop.spms.dao.ProjectDao.update", project);
        sqlSession.commit();
        return count;
    } finally {
        sqlSession.close();
    }
}
cs

UPDATE๋ฌธ์€ SqlSession ๊ฐ์ฒด์˜ update()๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฌธ๋ฒ•์€ ์ด์ „๊ณผ ๋™์ผํ•˜๋ฏ€๋กœ ์ƒ๋žตํ•œ๋‹ค.

 

5) Delete

delete()

1
2
3
4
5
6
7
8
9
10
11
12
@Override
public int delete(int no) throws SQLException {
 
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        int count = sqlSession.delete("com.atoz_develop.spms.dao.ProjectDao.delete", no);
        sqlSession.commit();
        return count;
    } finally {
        sqlSession.close();
    }
}
cs

DELETE๋ฌธ์€ SqlSession ๊ฐ์ฒด์˜ delete()๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฌธ๋ฒ•์€ ์ด์ „๊ณผ ๋™์ผํ•˜๋ฏ€๋กœ ์ƒ๋žตํ•œ๋‹ค.

 

์ด๋ ‡๊ฒŒ SQL๋ฌธ ๋ช…๋ น์–ด์™€ SqlSession ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ช…์ด ๊ฑฐ์˜ ๋™์ผํ•ด์„œ ํ˜ธ์ถœ ์‹œ ํŒŒ๋ผ๋ฏธํ„ฐ๋งŒ ์œ ์˜ํ•ด์„œ ๋„˜๊ฒจ์ฃผ๋ฉด ํฌ๊ฒŒ ์–ด๋ ค์›€ ์—†์ด DAO๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

 

3. SQL Mapper ํŒŒ์ผ ์ž‘์„ฑ

SqlSession์ด SQL๋ฌธ์„ ์‹คํ–‰ํ• ๋•Œ ์ฐธ์กฐํ•˜๋Š” SQL mapper ํŒŒ์ผ์„ ์ž‘์„ฑํ•œ๋‹ค.

์ฆ‰ ์›๋ž˜ DAO ํด๋ž˜์Šค์— ๋‚ด์žฅ๋ผ์žˆ๋˜ SQL๋ฌธ์„ ์ด SQL mapper ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

DAO ํŒจํ‚ค์ง€์— xml ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

ํ•„์š”ํ•œ resultMap๊ณผ SQL๋ฌธ์€ ๊ฐ์ž ํ”„๋กœ์ ํŠธ์— ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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="com.atoz_develop.spms.dao.ProjectDao">
    <resultMap type="project" id="projectResultMap">
        <id column="PNO" property="no" />
        <result column="PNAME" property="title" />
        <result column="CONTENT" property="content" />
        <result column="STA_DATE" property="startDate" javaType="java.sql.Date" />
        <result column="END_DATE" property="endDate" javaType="java.sql.Date" />
        <result column="STATE" property="state" />
        <result column="CRE_DATE" property="createdDate" javaType="java.sql.Date" />
        <result column="TAGS" property="tags" />
    </resultMap>
 
    <select id="selectList" resultMap="projectResultMap">
        select PNO, PNAME, STA_DATE, END_DATE, STATE
        from PROJECTS
        order by PNO desc
    </select>
 
    <insert id="insert" parameterType="project">
        insert into PROJECTS(PNAME, CONTENT, STA_DATE, END_DATE, STATE, CRE_DATE, TAGS)
        values (#{title}, #{content}, #{startDate}, #{endDate}, 0, NOW(), #{tags})
    </insert>
 
    <select id="selectOne" parameterType="int" resultMap="projectResultMap">
        select PNO, PNAME, CONTENT, STA_DATE, END_DATE, STATE, CRE_DATE, TAGS
        from PROJECTS
        where PNO = #{no}
    </select>
 
    <update id="update" parameterType="project">
        update PROJECTS set
            PNAME = #{title},
            CONTENT = #{content},
            STA_DATE = #{startDate},
            END_DATE = #{endDate},
            STATE = #{state},
            TAGS = #{tags}
        where PNO = #{no}
    </update>
 
    <delete id="delete" parameterType="int">
        delete from PROJECTS
        where PNO = #{no}
    </delete>
</mapper>
cs

 

์ž์„ธํ•œ ์ž‘์„ฑ ๋ฐฉ๋ฒ•์€ MyBatis ์„ค์ • ํŒŒ์ผ - SQL Mapper ์ž‘์„ฑ ๋ฐฉ๋ฒ• ํฌ์ŠคํŠธ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค.

 

SQL mapper ํŒŒ์ผ์—์„œ <mapper>์˜ namespace๊ฐ’ ๊ณผ <select>, <insert>, <update>, <delete>์˜ id ๊ฐ’์€ SqlSession ๊ฐ์ฒด๊ฐ€ SQL๋ฌธ์„ ์ฐพ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

DAO ํด๋ž˜์Šค์—์„œ SqlSession์˜ selectList(), selectOne(), insert(), update(), delete() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋„˜๊ธฐ๋Š” ๋ฌธ์ž์—ด ๊ฐ’๊ณผ ๋งคํ•‘๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

4. MyBatis ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ

MyBatis ์„ค์ • ํŒŒ์ผ์—๋Š” DB ์ปค๋„ฅ์…˜์„ ์ƒ์„ฑํ•˜๋Š” data source์— ๋Œ€ํ•œ ์ •๋ณด, ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ์ž, MyBatis ๋™์ž‘์„ ์ œ์–ดํ•˜๋Š” ํ™˜๊ฒฝ๊ฐ’, SQL mapper ํŒŒ์ผ ๊ฒฝ๋กœ ๋“ฑ์„ ์„ค์ •ํ•œ๋‹ค.

DAO ํŒจํ‚ค์ง€์— xml ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

์ƒ์„ธํ•œ ์„ค์ •๊ฐ’์€ ๊ฐ์ž ํ”„๋กœ์ ํŠธ์— ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•œ๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="com/atoz_develop/spms/dao/db.properties"/>
 
    <typeAliases>
        <typeAlias type="com.atoz_develop.spms.vo.Project" alias="project"/>
        <typeAlias type="com.atoz_develop.spms.vo.Member" alias="member"/>
    </typeAliases>
 
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
 
    <mappers>
        <mapper resource="com/atoz_develop/spms/dao/MySqlProjectDao.xml"/>
    </mappers>
</configuration>
cs

 

5. ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ ์ž‘์„ฑ

MyBatis ์„ค์ • ํŒŒ์ผ์˜ <properties resource="...">์— ์ง€์ •ํ•œ ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์„ ์ž‘์„ฑํ•œ๋‹ค.

์ด ํŒŒ์ผ์€ DB ์ ‘์† ์ •๋ณด๋ฅผ ๋‹ด๋Š”๋‹ค.

 

1
2
3
4
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3307/spms
username=username
password=password
cs

 

6. SqlSessionFactory ๊ฐ์ฒด ์ƒ์„ฑ

DAO์—์„œ SqlSessionFactory์˜ setter๋ฅผ ์„ ์–ธํ•ด ์ฃผ์—ˆ๋‹ค.

์™ธ๋ถ€์—์„œ SqlSessionFactory ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ฃผ์ž…ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

SqlSessionFactory๋Š” SqlSessionFactoryBuilder ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ build()๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์œ„์—์„œ ์ž‘์„ฑํ•œ MyBatis ์„ค์ • ํŒŒ์ผ์˜ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

1
2
3
4
String resource = "com/atoz_develop/spms/dao/mybatis-config.xml";
// Resources.getResourceAsStream(): ClASSPATH ๊ฒฝ๋กœ์— ์žˆ๋Š” ํŒŒ์ผ์˜ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ ์–ป์Œ
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
cs

 

MyBatis ์„ค์ • ํŒŒ์ผ์€ ๋ณดํ†ต CLASSPATH์— ์œ„์น˜์‹œํ‚ค๋Š”๋ฐ, CLASSPATH์— ์žˆ๋Š” ํŒŒ์ผ์˜ ์ž…๋ ฅ์ŠคํŠธ๋ฆผ์€ Resources ํด๋ž˜์Šค์˜ getResourceAsStream()์„ ์ด์šฉํ•ด์„œ ์†์‰ฝ๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ์ƒ์„ฑํ•œ sqlSessionFactory๋ฅผ DAO์— ์ฃผ์ž…์‹œ์ผœ์ฃผ๋ฉด MyBatis ์‚ฌ์šฉ์— ํ•„์š”ํ•œ ๋ชจ๋“  ๊ตฌํ˜„์ด ์™„๋ฃŒ๋œ๋‹ค.

์„œ๋ฒ„ ๊ธฐ๋™ ํ›„ ์ด์ „๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ชจ๋“  ๊ธฐ๋Šฅ์ด ์ •์ƒ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

 

#MyBatis ์„ค์ •ํŒŒ์ผ

#MyBatis SQL Mapper


  • ๊ด€๋ จ ๊ธ€

- MyBatis ์„ค์ • ํŒŒ์ผ - SQL Mapper ์ž‘์„ฑ ๋ฐฉ๋ฒ•

- MyBatis ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ•

 

References

์—ดํ˜ˆ๊ฐ•์˜ ์ž๋ฐ” ์›น ๊ฐœ๋ฐœ ์›Œํฌ๋ถ(ํ”„๋ฆฌ๋ ‰, 2016, ์—„์ง„์˜)

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€