티스토리 뷰

 

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/likelion-db
    username: root
    password: 1234

 

UserDao

@Repository
public class UserDao {

    private final JdbcTemplate jdbcTemplate;

    public UserDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void add(User user) {
        jdbcTemplate.update("insert into users(id, name, password) values(?, ?, ?);",
        user.getId(), user.getName(), user.getPassword());
    }
}

 

 

Select

select * from car_model

Spring Jdbc

spring jdbc에서는 .query()를 사용합니다.

public List<CarModel> getAll() {
    return this.jdbcTemplate.query("select * from car_model", this.rowMapper);
}

 

Insert

INSERT INTO important_game (gidx, show_finish_date, show_finish_time)

VALUES (123, "2016-04-29", "15:20:22");

ex)

insert into car_model (id, name, level, login, recommend) values(?, ?, ?, ?, ?)

 

Spring Jdbc

spring jdbc에서는 .update()를 사용합니다.

public void add(CarModel carModel) {
    this.jdbcTemplate.update("insert into car_model (id, name, level, login, recommend) values(?, ?, ?, ?, ?)",
            carModel.getId(), carModel.getName(), carModel.getLevel(), carModel.getLogin(), carModel.getRecommend());
}

 

Update

update car_model set name=?, level=?, login=?, recommend=? where id =?

 

jdbc

spring jdbc에서는 위에 Insert와 마찬가지로 .update()를 사용합니다.

public void update(CarModel carModel) {
    this.jdbcTemplate.update("update car_model set name=?, level=?, login=?, recommend=? where id =?",
            carModel.getName(), carModel.getLevel(), carModel.getRecommend(), carModel.getId());
}

 

 

end.

 

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
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
31
글 보관함