[스프링 부트] chapter 26. 스프링 데이터 2부 인메모리 데이터베이스

스프링 데이터 2부 인메모리 데이터베이스

지원하는 인-메모리 데이터베이스

Spring-JDBC가 클래스패스에 있으면 자동 설정이 필요한 빈을 설정해 줌

인-메모리 데이터베이스 기본 연결 정보 확인하는 방법

H2 콘솔 사용하는 방법

실습코드

@Component
public class H2Runner implements ApplicationRunner { 

    @Autowired 
    DataSource dataSource; 

    @Autowired 
    JdbcTemplate jdbcTemplate; 

    @Override 
    public void run(ApplicationArguments args) throws Exception { 
        try(Connection connection = dataSource.getConnection()) { 
            System.out.println(connection.getMetaData().getURL()); 
            System.out.println(connection.getMetaData().getUserName()); 
            Statement statement = connection.createStatement(); 
            String sql = "CREATE TABLE USER (ID INTEGER NOT NULL, name VARCHAR(255), PRIMARY KEY (id))"; 
            statement.executeUpdate(sql); 
        } 
        jdbcTemplate.execute("INSERT INTO USER VALUES (1, 'juho')"); 
    } 
}