Back-end/Spring

[Spring] DAO ๊ธฐ๋ณธ ๊ตฌ์กฐ ๋งŒ๋“ค๊ธฐ

 

๐Ÿง DAO๋ž€?

DAO(Data Access Object)๋Š” DB๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ์กฐ์ž‘ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ „๋‹ดํ•˜๋„๋ก ๋งŒ๋“  ์˜ค๋ธŒ์ ํŠธ

 

๐Ÿง DAO ๊ธฐ๋ณธ ๊ตฌ์กฐ

๋จผ์ € ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์‰ฝ๊ฒŒ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด๋ฅผ ์œ„ํ•ด User ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

User Class

public class User {
    String id;
    String name;
    String password;
    
    public String getId(){
        return id;
    }
    public void setId(String id){
        this.id = id;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getPassword(){
        return password;
    }
    public void setPassword(String password){
        this.password = password;
    }
}

 User ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด DAO ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

๐Ÿ‘‰ Database์™€ ์—ฐ๋™ํ•˜๊ธฐ ์œ„ํ•ด JDBC๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ๋ฐ, ์ผ๋ฐ˜์ ์ธ ์ˆœ์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

  • DB ์—ฐ๊ฒฐ์„ ์œ„ํ•œ Connection์„ ๊ฐ€์ ธ์˜จ๋‹ค.
  • SQL์„ ๋‹ด์€ Statement(or PreparedStatement)๋ฅผ ๋งŒ๋“ ๋‹ค.
  • ๋งŒ๋“ค์–ด์ง„ Statement๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
  • ์กฐํšŒ์˜ ๊ฒฝ์šฐ SQL ์ฟผ๋ฆฌ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ResultSet์œผ๋กœ ๋ฐ›์•„์„œ ์ •๋ณด๋ฅผ ์ €์žฅํ•  ์˜ค๋ธŒ์ ํŠธ(User)์— ์˜ฎ๊ฒจ์ค€๋‹ค.
  • ์ž‘์—… ์ค‘์— ์ƒ์„ฑ๋œ Connection, Statement, ResultSet ๊ฐ™์€ ๋ฆฌ์†Œ์Šค๋Š” ์ž‘์—…์„ ๋งˆ์นœ ํ›„ ๋ฐ˜๋“œ์‹œ ๋‹ซ์•„์ค€๋‹ค.
  • JDBC API๊ฐ€ ๋งŒ๋“ค์–ด๋‚ด๋Š” ์˜ˆ์™ธ๋ฅผ ์žก์•„์„œ ์ง์ ‘ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜, throws๋ฅผ ์„ ์–ธํ•ด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋ฉ”์„œ๋“œ ๋ฐ–์œผ๋กœ ๋˜์ง€๊ฒŒ ํ•œ๋‹ค.

UserDao Class

public class UserDao {
	public void add(User user) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring?autoReconnect=true&useSSL=false\";","root","@min753951");

        PreparedStatement ps = c.prepareStatement(
                "insert into users(id, name, password) values(?,?,?)");

        ps.setString(1, user.getId());
        ps.setString(2, user.getName());
        ps.setString(3, user.getPassword());

        ps.executeUpdate();

        ps.close();
        c.close();

    }
	
	public User get(String id) throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
        Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring?autoReconnect=true&useSSL=false\";","root","@min753951");

        PreparedStatement ps = c.prepareStatement(
                "select * from users where id = ?");
		ps.setString(1,  id);
		
		ResultSet rs = ps.executeQuery();
		rs.next();
		User user = new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPassword(rs.getString("password"));
		
		rs.close();
		ps.close();
		c.close();
        
        return user;
	}

}

 

DAO ๊ฐ์ฒด๊ฐ€ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” main()์„ ์‚ฌ์šฉํ•ด ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Main Class

public static void main(String[] args) {
        UserDao dao = new UserDao();
        user.setId("alstmdsha");
        user.setName("๋ฐ•๋ฏผ์†ก");
        user.setPassword("123456");
        
        dao.add(user);
        
        System.out.println(user.getId()+" ๋“ฑ๋ก ์„ฑ๊ณต ");
        
        User user2 = dao.get(user.getId());
        System.out.println(user2.getName());
        System.out.println(user2.getPassword());
        
        System.out.println(user2.getId() + " ์กฐํšŒ ์„ฑ๊ณต ");
    }

 

๐Ÿ‘‰ main() ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํ…Œ์ŠคํŠธ ์„ฑ๊ณต ๋ฉ”์‹œ์ง€๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿงพ ์ •๋ฆฌ 

์ง€๊ธˆ๊นŒ์ง€ ๊ฐ„๋‹จํ•œ DAO์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด DAO ํด๋ž˜์Šค์—๋Š” ๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„ ์›์น™(SOLID)๋“ค ์ค‘ ๊ฐœ๋ฐฉ ํ์‡„ ์›์น™์— ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ฐœ๋ฐฉ ํ์‡  ์›์น™์ด๋ž€?

 'ํด๋ž˜์Šค๋‚˜ ๋ชจ๋“ˆ์€ ํ™•์žฅ์—๋Š” ์—ด๋ ค ์žˆ์–ด์•ผ ํ•˜๊ณ  ๋ณ€๊ฒฝ์—๋Š” ๋‹ซํ˜€ ์žˆ์–ด์•ผ ํ•œ๋‹ค'

 

์ฆ‰, ์ด Dao ํด๋ž˜์Šค๋ฅผ ๋‹ค์–‘ํ•œ ํด๋ผ์ด์–ธํŠธ๋“ค์ด ์‚ฌ์šฉํ•  ํ…๋ฐ,,,,

๋ชจ๋“  ํด๋ผ์ด์–ธํŠธ๋“ค์ด ๋‹ค ๊ฐ™์€ DB connect ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ๋ชจ๋“  ํด๋ผ์ด์–ธํŠธ๋“ค์˜ DB connect ๋ฐฉ์‹์ด ๋‹ค๋ฅด๋‹ค๋ฉด, ๋งค๋ฒˆ Dao ๊ฐ์ฒด๋ฅผ ๊ณ ์ณ์ฃผ์–ด์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ๋Œ€๋ถ€๋ถ„์˜ ๊ฐœ๋ฐœ์ž๋“ค์€ ๊ณ ๊ฐ์—๊ฒŒ ์†Œ์Šค๋ฅผ ์ง์ ‘ ๊ณต๊ฐœํ•˜๊ณ  ์‹ถ์ง€ ์•Š์•„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—,

ํด๋ผ์ด์–ธํŠธ๋“ค์ด ์›ํ•˜๋Š” DB connection ๋ฐฉ์‹์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์š”๊ตฌ์— ๋งž๊ฒŒ ์ƒ์„ฑํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

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

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

๋ฐ˜์‘ํ˜•