티스토리 뷰

DDL

Create Table

employee라는 이름의 테이블을 만듭니다.

컬럼은 id, name, contents 3개 입니다.

if not exists 가 들어있어서 없을때만 create table을 실행하고 테이블이 존재한다면 실행하지 않습니다.

.update()를 썼기 때문에 리턴값이 0입니다.

public int createTable() {
    // table이 있는지 먼저 check
    return this.jdbcTemplate.update(String.format("create table if not exists employee(id bigint, name varchar, contents text)"));
}

 

With Primary Key

public int createTable() {
    // table이 있는지 먼저 check
    return this.jdbcTemplate.update(String.format("create table if not exists " +
            "customer_service_record(id bigint, car_model_id int, cs_start_date timestamp," +
            " contents text, PRIMARY KEY (id))"));
}

 

Drop Table

테이블을 지우고 싶을 때 씁니다.

public void dropTable() {
    // table이 있는지 먼저 check
    this.jdbcTemplate.execute("drop table if exists employee");
}

 

 

build.gradle에 추가할 Hikari cp

implementation 'com.zaxxer:HikariCP:5.0.0'

 

Hikari DataSource 만들기

public DataSource dataSourceHikari() {
    HikariConfig hc = new HikariConfig();
    hc.setDriverClassName("org.postgresql.Driver");
    hc.setJdbcUrl("jdbc:postgresql://<ip>:9999/testdb");
    hc.setUsername("postgres");
    hc.setPassword("<pw>");
    DataSource dataSource = new HikariDataSource(hc);
    return dataSource;
}

 

JdbcTemplate과 연동

@Bean
public AccountsDao accountsDao() {
    return new AccountsDao(new JdbcTemplate(dataSourceHikari()));
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함