반응형
SMALL
package com.in28minutes.springboot.learnjapandhibernate.course.jdbc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.in28minutes.springboot.learnjapandhibernate.course.Course;
@Component
public class CourseJdbcCommandLineRunner implements CommandLineRunner{
@Autowired
private CourseJdbcRepository repository;
@Override
public void run(String... args) throws Exception {
repository.insert(new Course(1, "Learn AWS", "in28minutes"));
repository.insert(new Course(2, "Learn AWS!", "in28minutes"));
repository.insert(new Course(3, "Learn AWS!!", "in28minutes"));
repository.deleteByid(1);
System.out.println(repository.findid(2));
System.out.println(repository.findid(3));
}
}
1,2,3 삽입
1 삭제
2,3 find -> select
package com.in28minutes.springboot.learnjapandhibernate.course.jdbc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.in28minutes.springboot.learnjapandhibernate.course.Course;
@Repository
public class CourseJdbcRepository {
@Autowired
private JdbcTemplate springJdbcTemplate;
private static String INSERT_QUERY =
"""
insert into course(id,name,author)
values (?, ?, ?);
""";
public void insert(Course course) {
springJdbcTemplate.update(INSERT_QUERY, course.getId(), course.getName(), course.getAuthor());
}
private static String DELETE_QUERY =
"""
delete
from course
where id = ?
""";
public void deleteByid(long id) {
springJdbcTemplate.update(DELETE_QUERY, id);
}
private static String SELECT_QUERY =
"""
select *
from course
where id = ?
""";
public Course findid(long id) {
return springJdbcTemplate.queryForObject(SELECT_QUERY, new BeanPropertyRowMapper<>(Course.class), id);
}
}
반응형
LIST
'Spring > Spring Boot' 카테고리의 다른 글
Spring Data JPA (0) | 2023.09.20 |
---|---|
JPA marge, find, remove (0) | 2023.09.20 |
h2 삽입, 조회, 삭제 (0) | 2023.09.19 |
h2콘솔 실행하기 (0) | 2023.09.19 |
@ConfigurationProperties (0) | 2023.09.19 |