Back-end/Spring

[Spring] ๊ฐ์ฒด์ง€ํ–ฅ์  DAO - ๋ถ„๋ฆฌ (ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด)

๐Ÿ‘‰ ๊ฐ์ฒด์ง€ํ–ฅ์˜ ์„ธ๊ณ„์—์„œ๋Š” ๋ชจ๋“ ๊ฒƒ์ด ๋ณ€ํ•˜๋Š”๋ฐ, ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ๊ฐ์ฒด์— ๋Œ€ํ•œ ์„ค๊ณ„์™€ ์ด๋ฅผ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๊ฐ€ ๋ณ€ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ์ž๋Š” ๊ฐ์ฒด๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ ๋ฐ”๋กœ ๋ฏธ๋ž˜์˜ ๋ณ€ํ™”๋ฅผ ์–ด๋–ป๊ฒŒ ๋Œ€๋น„ํ•  ๊ฒƒ์ธ๊ฐ€ ์—ผ๋‘ํ•ด ๋‘ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ์ฒด์ง€ํ–ฅ ๊ธฐ์ˆ (์–ธ์–ด)๋Š” ์ด๋Ÿฌํ•œ ๊ฐ์ฒด์ง€ํ–ฅ ์„ธ๊ณ„์˜ ๋ณ€ํ™”์— ํšจ๊ณผ์ ์œผ๋กœ ๋Œ€์ฒ˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

How?

๋ณ€ํ™”์˜ ํญ์„ ๋ถ„๋ฆฌ๋ฅผ ํ†ตํ•ด ์ตœ์†Œํ•œ์œผ๋กœ ์ค„์—ฌ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿง ๋ถ„๋ฆฌ๋ž€?

 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๊ธฐ์ดˆ ๊ฐœ๋… ์ค‘ ๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ๋ผ๋Š” ๊ฒŒ ์žˆ๋Š”๋ฐ,

 

์ด๋ฅผ ๊ฐ์ฒด์ง€ํ–ฅ์— ์ ์šฉํ•ด๋ณด๋ฉด, ๊ด€์‹ฌ์ด ๊ฐ™์€ ๊ฒƒ๋ผ๋ฆฌ๋Š” ํ•˜๋‚˜์˜ ๊ฐ์ฒด ์•ˆ์œผ๋กœ ๋˜๋Š” ์นœํ•œ ๊ฐ์ฒด๋กœ ๋ชจ์ด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ณ ,

๊ด€์‹ฌ์ด ๋‹ค๋ฅธ ๊ฒƒ์€ ๊ฐ€๋Šฅํ•œ ํ•œ ๋”ฐ๋กœ ๋–จ์–ด์ ธ์„œ ์„œ๋กœ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋„๋ก ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ๋ฅผ ์ ์šฉํ•ด๋ณด๋ฉด,

DB connection ๋ถ€๋ถ„์„ ๋‹ค๋ฅธ ๊ฒƒ๊ณผ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋„๋ก ๋…๋ฆฝ์ ์œผ๋กœ ๋ถ„๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋ถ„๋ฆฌํ•˜๊ณ  ๋‚œ UserDao Class

public class UserDao_distributed {
    public void add(User user) throws ClassNotFoundException, SQLException{
        Connection c = getConnection();
        //...์ฝ”๋“œ ์ƒ๋žต
    }
    
    public User get(String id) throws ClassNotFoundException, SQLException {
        Connection c = getConnection();
        //...์ฝ”๋“œ ์ƒ๋žต
    }
    
    private Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection c = getConnection("jdbc:mysql://localahost/springDao","spring", "753951");
        return c;
    }
}

 

์œ„์™€ ๊ฐ™์ด DB Connection ์ƒ์„ฑ ๋ฐฉ์‹์„ ๋…๋ฆฝ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ์ง€๋งŒ,

์—ฌ๋Ÿฌ ๊ณ ๊ฐ๋“ค์ด ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ DB๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ, ๊ณ ๊ฐ๋“ค์—๊ฒŒ ์†Œ์Šค๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ DB๋ฅผ ์ œ๊ณตํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค. 

์™œ๋ƒํ•˜๋ฉด ๊ฐœ๋ฐœ์ž๋“ค์€ ๊ณ ๊ฐ์—๊ฒŒ ์†Œ์Šค๋ฅผ ์ง์ ‘ ๊ณต๊ฐํ•˜๊ณ  ์‹ถ์ง€ ์•Š์•„ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

์ƒ์†

์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด UserDao ์ฝ”๋“œ๋ฅผ ํ•œ ๋‹จ๊ณ„ ๋” ๋ถ„๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด Naver์™€ Kakao๊ฐ€ Dao ๊ฐ์ฒด๋ฅผ ์‚ฌ๊ณ  ์‹ถ์–ดํ•  ๋•Œ, DB ์ƒ์„ฑ ๋ฐฉ์‹์„ ์ž์‹ ๋“ค์˜ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์–ดํ•˜๊ธฐ ๋•Œ๋ฌธ์—,

์ถ”์ƒ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด์ง„ UserDao๋ฅผ ์ƒ์†ํ•ด NUserDao์™€ KUserDao ์„œ๋ธŒํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๊ตฌํ˜„์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ถ”์ƒํ™”ํ•œ UserDao Class

public abstract class UserDao_abstracted {
    public void add(User user) throws ClassNotFoundException, SQLException {
        Connection c = getConnection();
        //...์ฝ”๋“œ ์ƒ๋žต
    }

    public User get(String id) throws ClassNotFoundException, SQLException {
        Connection c = getConnection();
        //...์ฝ”๋“œ ์ƒ๋žต
    }
    
    public abstract Connection getConnection() throws ClassNotFoundException, SQLException;
}


    public class NUserDao extends UserDao {
        public Connection getConnection() throws ClassNotFoundException, SQLException {
            // Naver DB connection Code
        }
    }

    public class KUserDao extends UserDao {
        public Connection getConnection() throws ClassNotFoundException, SQLException {
            // Kakao DB connection Code
        }
    }

 

 

๐Ÿ‘‰ ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด - ์Šคํ”„๋ง์ด ์‚ฌ๋ž‘ํ•˜๋Š”..

 

 ์œ„์™€ ๊ฐ™์ด ์Šˆํผํด๋ž˜์Šค์— ๊ธฐ๋ณธ์ ์ธ ๋กœ์ง์˜ ํ๋ฆ„์„ ๋งŒ๋“ค๊ณ , 

 ๊ทธ ๊ธฐ๋Šฅ์˜ ์ผ๋ถ€๋ฅผ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋กœ ๋งŒ๋“  ๋’ค ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ์ด๋Ÿฐ ๋ฉ”์†Œ๋“œ๋ฅผ ํ•„์š”์— ๋งž๊ฒŒ ๊ตฌํ˜„ํ•ด์„œ

 ์‚ฌ์šฉํ•˜๋„๋ก ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿงพ ์ •๋ฆฌ 

๋ถ„๋ฆฌ๋ฅผ ํ†ตํ•ด ๊ทธ๋Ÿด๋“ฏํ•œ Dao ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ, ์•„์‰ฝ๊ฒŒ๋„ ๋ฌธ์ œ์ ์€ ๋‚จ์•„์žˆ์Šต๋‹ˆ๋‹ค.

์ƒ์†์€ ๋‹ค์ค‘ ์ƒ์†์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ชฉ์ ์œผ๋กœ UserDao์— ์ƒ์†์„ ์ ์šฉํ•˜๊ธฐ ํž˜๋“ญ๋‹ˆ๋‹ค.

ํ™•์žฅ๋œ ๊ธฐ๋Šฅ์ธ DB Connection์„ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋‹ค๋ฅธ Dao ํด๋ž˜์Šค์—์„œ ์ ์šฉ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

์ด๋ฅผ ์œ„ํ•ด DAO์— ํ™•์žฅ์„ ์ ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

์ฐธ๊ณ  ๋ฌธ์„œ : 

ํ† ๋น„์˜ ์Šคํ”„๋ง3 (์ด์ผ๋ฏผ)

๋ฐ˜์‘ํ˜•