[6/28 TIL] SPRING(DataSource, JdbcTemplate)

yumyeonghanยท2023๋…„ 6์›” 29์ผ
0
post-custom-banner

๐Ÿƒํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฐฑ์—”๋“œ ๋ฐ๋ธŒ์ฝ”์Šค 4๊ธฐ ๊ต์œก๊ณผ์ •์„ ๋“ฃ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.๐Ÿƒ

DataSource

  • ๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ ์ด์šฉํ•ด์„œ ์ปค๋„ฅ์…˜ ํ’€์— ์žˆ๋Š” ์ปค๋„ฅ์…˜์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•˜๊ณ , ๋ฐ˜๋‚ฉํ•จ
  • ๋ฐ์ดํ„ฐ ์†Œ์Šค๊ฐ€ ์ปค๋„ฅ์…˜ ํ’€์„ ๊ด€๋ฆฌ

DataBase Connection Pool(DBCP)


๊ทธ๋ฆผ ์ถœ์ฒ˜

  • ์ปค๋„ฅ์…˜ ํ’€์€ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ(์ปค๋„ฅ์…˜)์„ ๋ฏธ๋ฆฌ ์„ค์ •ํ•˜๊ณ  ์žฌ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์ž„
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•œ ํ›„์—๋Š” ์—ฐ๊ฒฐ์„ ๋‹ซ์ง€ ์•Š๊ณ  ํ’€์— ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ด๋ฅผ ์žฌ์‚ฌ์šฉ

HikariCP

  • Java ์–ธ์–ด๋ฅผ ์œ„ํ•œ ์ปค๋„ฅ์…˜ ํ’€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • ๋ณ„๋„ ์„ค์ • ์—†์ด ์Šคํ”„๋ง ๋ถ€ํŠธ JDBC API์— ํฌํ•จ

JdbcTemplate

  • ๊ธฐ์กด JDBC API๋ฅผ ๋‹จ์ˆœํ™”ํ•˜๊ณ  ๋ณต์žกํ•œ ๋ฐ˜๋ณต ์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐ
  • ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์™€ ์ปค๋„ฅ์…˜ ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌ
  • DataSource๋ฅผ ์ด์šฉํ•ด์„œ ์ปค๋„ฅ์…˜์„ ์‚ฌ์šฉ

์˜ˆ์‹œ ์ฝ”๋“œ

public class JDBCTemplateCrudExample {

    public static void main(String[] args) {
        // JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋“œ
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์„ค์ •
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/sampledb");
        dataSource.setUsername("username");
        dataSource.setPassword("password");

        // JdbcTemplate ์ƒ์„ฑ
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        // ๋ฐ์ดํ„ฐ ์‚ฝ์ž…
        UUID id = UUID.randomUUID();
        String name = "์œ ๋ช…ํ•œ";
        int age = 25;
        insertData(jdbcTemplate, id, name, age);
        
        // ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ (๋‹จ์ผ ํ–‰)
        UUID searchId = UUID.fromString("your-id");
        UserData userData = retrieveDataById(jdbcTemplate, searchId);
        if (userData != null) {
            System.out.println("Data Found:");
            System.out.println("ID: " + userData.getId() + ", Name: " + userData.getName() + ", Age: " + userData.getAge());
        } else {
            System.out.println("Data Not Found");
        }

        // ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰
        System.out.println("All Data:");
        retrieveData(jdbcTemplate);

        // ๋ฐ์ดํ„ฐ ๊ฐฑ์‹ 
        String newName = "๊น€๋ช…ํ•œ";
        int newAge = 30;
        updateData(jdbcTemplate, id, newName, newAge);

        // ๋ฐ์ดํ„ฐ ์‚ญ์ œ
        deleteData(jdbcTemplate, id);

        // ์ตœ์ข… ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰
        System.out.println("Updated Data:");
        retrieveData(jdbcTemplate);
    }

    // ๋ฐ์ดํ„ฐ ์‚ฝ์ž…
    private static void insertData(JdbcTemplate jdbcTemplate, UUID id, String name, int age) {
        String sql = "INSERT INTO users (id, name, age) VALUES (?, ?, ?)";
        jdbcTemplate.update(sql, toBytes(id), name, age);
        System.out.println("Data inserted successfully.");
    }
    
    // ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ (๋‹จ์ผ ํ–‰)
    private static UserData retrieveDataById(JdbcTemplate jdbcTemplate, UUID id) {
        String sql = "SELECT * FROM users WHERE id = ?";
        Object[] params = {toBytes(id)};
        UserData userData = jdbcTemplate.queryForObject(sql, params, (ResultSet rs, int rowNum) -> {
            UUID userId = toUUID(rs.getBytes("id"));
            String name = rs.getString("name");
            int age = rs.getInt("age");
            return new UserData(userId, name, age);
        });
        return userData;
    }

    // ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰
    private static void retrieveData(JdbcTemplate jdbcTemplate) {
        String sql = "SELECT * FROM users";
        jdbcTemplate.query(sql, (ResultSet rs) -> {
            while (rs.next()) {
                UUID id = toUUID(rs.getBytes("id"));
                String name = rs.getString("name");
                int age = rs.getInt("age");
                System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
            }
        });
    }

    // ๋ฐ์ดํ„ฐ ๊ฐฑ์‹ 
    private static void updateData(JdbcTemplate jdbcTemplate, UUID id, String name, int age) {
        String sql = "UPDATE users SET name=?, age=? WHERE id=?";
        jdbcTemplate.update(sql, name, age, toBytes(id));
        System.out.println("Data updated successfully.");
    }

    // ๋ฐ์ดํ„ฐ ์‚ญ์ œ
    private static void deleteData(JdbcTemplate jdbcTemplate, UUID id) {
        String sql = "DELETE FROM users WHERE id=?";
        jdbcTemplate.update(sql, toBytes(id));
        System.out.println("Data deleted successfully.");
    }

    // byte[]์„ UUID๋กœ ๋ณ€ํ™˜
    private static UUID toUUID(byte[] bytes) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        return new UUID(byteBuffer.getLong(), byteBuffer.getLong());
    }

    // UUID๋ฅผ byte[]๋กœ ๋ณ€ํ™˜
    private static byte[] toBytes(UUID uuid) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(16);
        byteBuffer.putLong(uuid.getMostSignificantBits());
        byteBuffer.putLong(uuid.getLeastSignificantBits());
        return byteBuffer.array();
    }
}
profile
์›น ๊ฐœ๋ฐœ์— ๊ด€์‹ฌ ์žˆ์Šต๋‹ˆ๋‹ค.
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€