Spring 개요

wit_dan·2021년 11월 2일
0

Spring

목록 보기
1/2

정의

  • 자바 엔터프라이즈 애플리케이션 개발에 이용되는 애플리케이션 프레임워크
  • 엔터프라이즈 서비스는 실제 기업에서 사용할 수 있을 정도의 시스템
    ⇒ 보안, 트랜잭션과 같이 서비스의 안정성과 성능을 고려하여 개발할 수 있는 환경을 제공

특징

  • Spring은 적용한 기술이 Method나 Class 코드에 직접 반영되지 않음
    ⇒ 비침투적인(non-invasive) 기술

  • 기술 제약과 비즈니스 로직의 분리로

  • 기존 Java 방식

/*
 * 기존 Java 방식
 * 기술제약(트랜잭션)과 비즈니스 로직(데이터 삽입)의 혼재
 */
public void DBTest() {
    String serverURL = "jdbc:mysql://localhost:3306/{DB명}";
    String id = "{계정명}";
    String pw = "{비밀번호}";

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(serverURL, id, pw);
        con.setAutoCommit(false); // transaction 유지를 위해 AutoCommit false

        String sql1 = "insert into user_info(userno, username, age) values(1, 'Heidi', 18, 05)";
        String sql2 = "insert into user_info(userno, username, age) values(2, 'Solar', 23, 06)";

        PreparedStatement pstmt1 = con.prepareStatement(sql1);
        pstmt1.executeUpdate();

        PreparedStatement pstmt2 = con.prepareStatement(sql2);
        pstmt2.executeUpdate();

        conn.commit();
    } catch (ClassNotFoundException e) {
        System.out.println("드라이버가 존재하지 않습니다");
    } catch (SQLException e) {
        e.printStackTrace();
        conn.rollback();
    } finally {
        pstmt1.close();
        pstmt2.close();
        conn.close();
    }
}
  • Spring 방식
/*
 * Spring 방식
 * Annotation(@)의 등장으로 기술제약과 비즈니스 로직의 분리가 가능해짐
 */
@Autowired
private JdbcTemplate jdbcTemplate;

@Transactional
public void DBTest() {
    String sql1 = "insert into user_info(userno, username, age) values(1, 'Heidi', 18, 05)";
    String sql2 = "insert into user_info(userno, username, age) values(2, 'Solar', 23, 06)";

    jdbcTemplate.update(sql1);
    jdbcTemplate.update(sql2);
}

Version 비교

[Spring MVC / Spring boot 13] Spring의 버전 별 차이

profile
Back-End 개발자 / 기록용

0개의 댓글