Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class AutowiredBean {
/*
어떤 방법으로 Component에 Bean을 주입하는지 학습하기
*/
@Autowired
private SpringBean springBean;

public String sayHello() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/*
어떤 어노테이션을 붙였을 때 Bean으로 생성되는지 학습하기
*/
@Component
public class SpringBean {
public String hello() {
return "Hello";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
public class ConstructorInjection {
private InjectionBean injectionBean;

public ConstructorInjection(InjectionBean injectionBean) {
this.injectionBean = injectionBean;
}

/*
ConstructorInjection으로 InjectionBean 주입받기
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class FieldInjection {
/*
FieldInjection으로 InjectionBean 주입받기
*/
@Autowired
private InjectionBean injectionBean;

public String sayHello() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public class SetterInjection {
public String sayHello() {
return injectionBean.hello();
}

@Autowired
public void setInjectionBean(InjectionBean injectionBean) {
this.injectionBean = injectionBean;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package cholog.scan;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
/*
ComponentScan에 대해 학습하고, ComponenetScanBean을 Bean으로 등록하기
*/
@Configuration
@ComponentScan(basePackages = "cholog.scan")
public class ContextConfiguration {

}
40 changes: 32 additions & 8 deletions spring-jdbc-1/initial/src/main/java/cholog/QueryingDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,65 @@ public QueryingDAO(JdbcTemplate jdbcTemplate) {
*/
public int count() {
//TODO : customers 디비에 포함되어있는 row가 몇개인지 확인하는 기능 구현
return 0;
return jdbcTemplate.queryForObject("select count(*) from customers", Integer.class);
}

/**
* public <T> T queryForObject(String sql, Class<T> requiredType, @Nullable Object... args)
*/
public String getLastName(Long id) {
//TODO : 주어진 Id에 해당하는 customers의 lastName을 반환
return null;
return jdbcTemplate.queryForObject("select last_name from customers where id = ?", String.class, id);
}

/**
* public <T> T queryForObject(String sql, RowMapper<T> rowMapper, @Nullable Object... args)
*/
public Customer findCustomerById(Long id) {
String sql = "select id, first_name, last_name from customers where id = ?";
//TODO : 주어진 Id에 해당하는 customer를 객체로 반환
return null;
return jdbcTemplate.queryForObject(
"select id, first_name, last_name from customers where id = ?",
(resultSet, rowNum) -> {
Customer customer = new Customer(
resultSet.getLong("id"),
resultSet.getString("first_name"),
resultSet.getString("last_name")
);
return customer;
}, id);
}

/**
* public <T> List<T> query(String sql, RowMapper<T> rowMapper)
*/
public List<Customer> findAllCustomers() {
String sql = "select id, first_name, last_name from customers";
//TODO : 저장된 모든 Customers를 list형태로 반환
return null;
return jdbcTemplate.query(
"select id, first_name, last_name from customers",
(resultSet, rowNum) -> {
Customer customer = new Customer(
resultSet.getLong("id"),
resultSet.getString("first_name"),
resultSet.getString("last_name")
);
return customer;
});
}

/**
* public <T> List<T> query(String sql, RowMapper<T> rowMapper, @Nullable Object... args)
*/
public List<Customer> findCustomerByFirstName(String firstName) {
String sql = "select id, first_name, last_name from customers where first_name = ?";
//TODO : firstName을 기준으로 customer를 list형태로 반환
return null;
return jdbcTemplate.query(
"select id, first_name, last_name from customers where first_name = ?",
(resultSet, rowNum) -> {
Customer customer = new Customer(
resultSet.getLong("id"),
resultSet.getString("first_name"),
resultSet.getString("last_name")
);
return customer;
}, firstName);
}
}
18 changes: 12 additions & 6 deletions spring-jdbc-1/initial/src/main/java/cholog/UpdatingDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,32 @@ public UpdatingDAO(JdbcTemplate jdbcTemplate) {
*/
public void insert(Customer customer) {
//todo: customer를 디비에 저장하기
jdbcTemplate.update("insert into customers (first_name, last_name) values (?, ?)",
customer.getFirstName(), customer.getLastName());
}
/**
* public int update(String sql, @Nullable Object... args)
*/
public int delete(Long id) {
//todo: id에 해당하는 customer를 지우고, 해당 쿼리에 영향받는 row 수반환하기
return 0;
return jdbcTemplate.update("delete from customers where id = ?", Long.valueOf(id));
}

/**
* public int update(final PreparedStatementCreator psc, final KeyHolder generatedKeyHolder)
*/
public Long insertWithKeyHolder(Customer customer) {
String sql = "insert into customers (first_name, last_name) values (?, ?)";
KeyHolder keyHolder = new GeneratedKeyHolder();

//todo : keyHolder에 대해 학습하고, Customer를 저장후 저장된 Customer의 id를 반환하기
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(
"insert into customers (first_name, last_name) values (?, ?)",
new String[]{"id"});
ps.setString(1, customer.getFirstName());
ps.setString(2, customer.getLastName());
return ps;
}, keyHolder);

Long id = keyHolder.getKey().longValue();

return keyHolder.getKey().longValue();
return id;
}
}