forked from mkyong/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
281 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
spring-jdbc/src/main/java/com/mkyong/customer/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.mkyong.customer; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class Customer { | ||
|
||
private Long ID; | ||
private String name; | ||
private Integer age; | ||
private LocalDateTime createdDate; | ||
|
||
public Customer() { | ||
} | ||
|
||
public Customer(String name, Integer age) { | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public Customer(Long ID, String name, Integer age, LocalDateTime createdDate) { | ||
this.ID = ID; | ||
this.name = name; | ||
this.age = age; | ||
this.createdDate = createdDate; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Customer{" + | ||
"ID=" + ID + | ||
", name='" + name + '\'' + | ||
", age=" + age + | ||
", createdDate=" + createdDate + | ||
'}'; | ||
} | ||
|
||
public Long getID() { | ||
return ID; | ||
} | ||
|
||
public void setID(Long ID) { | ||
this.ID = ID; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Integer getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(Integer age) { | ||
this.age = age; | ||
} | ||
|
||
public LocalDateTime getCreatedDate() { | ||
return createdDate; | ||
} | ||
|
||
public void setCreatedDate(LocalDateTime createdDate) { | ||
this.createdDate = createdDate; | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
spring-jdbc/src/main/java/com/mkyong/customer/CustomerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.mkyong.customer; | ||
|
||
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 java.math.BigDecimal; | ||
import java.sql.Timestamp; | ||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Repository | ||
public class CustomerRepository { | ||
|
||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
public int save(Customer customer) { | ||
return jdbcTemplate.update( | ||
"insert into customer (name, age, created_date) values(?,?,?)", | ||
customer.getName(), customer.getAge(), LocalDateTime.now()); | ||
} | ||
|
||
public Customer findByCustomerId(Long id) { | ||
|
||
String sql = "SELECT * FROM CUSTOMER WHERE ID = ?"; | ||
|
||
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new CustomerRowMapper()); | ||
|
||
} | ||
|
||
public Customer findByCustomerId2(Long id) { | ||
|
||
String sql = "SELECT * FROM CUSTOMER WHERE ID = ?"; | ||
|
||
return (Customer) jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper(Customer.class)); | ||
|
||
} | ||
|
||
public Customer findByCustomerId3(Long id) { | ||
|
||
String sql = "SELECT * FROM CUSTOMER WHERE ID = ?"; | ||
|
||
return jdbcTemplate.queryForObject(sql, new Object[]{id}, (rs, rowNum) -> | ||
new Customer( | ||
rs.getLong("id"), | ||
rs.getString("name"), | ||
rs.getInt("age"), | ||
rs.getTimestamp("created_date").toLocalDateTime() | ||
)); | ||
|
||
} | ||
|
||
public List<Customer> findAll() { | ||
|
||
String sql = "SELECT * FROM CUSTOMER"; | ||
|
||
List<Customer> customers = new ArrayList<>(); | ||
|
||
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql); | ||
|
||
for (Map row : rows) { | ||
Customer obj = new Customer(); | ||
|
||
obj.setID(((Integer) row.get("ID")).longValue()); | ||
//obj.setID(((Long) row.get("ID"))); no, ClassCastException | ||
obj.setName((String) row.get("NAME")); | ||
obj.setAge(((BigDecimal) row.get("AGE")).intValue()); // Spring returns BigDecimal, need convert | ||
obj.setCreatedDate(((Timestamp) row.get("CREATED_DATE")).toLocalDateTime()); | ||
customers.add(obj); | ||
} | ||
|
||
return customers; | ||
} | ||
|
||
public List<Customer> findAll2() { | ||
|
||
String sql = "SELECT * FROM CUSTOMER"; | ||
|
||
List<Customer> customers = jdbcTemplate.query( | ||
sql, | ||
new CustomerRowMapper()); | ||
|
||
return customers; | ||
} | ||
|
||
public List<Customer> findAll3() { | ||
|
||
String sql = "SELECT * FROM CUSTOMER"; | ||
|
||
List<Customer> customers = jdbcTemplate.query( | ||
sql, | ||
new BeanPropertyRowMapper(Customer.class)); | ||
|
||
return customers; | ||
} | ||
|
||
public List<Customer> findAll4() { | ||
|
||
String sql = "SELECT * FROM CUSTOMER"; | ||
|
||
return jdbcTemplate.query( | ||
sql, | ||
(rs, rowNum) -> | ||
new Customer( | ||
rs.getLong("id"), | ||
rs.getString("name"), | ||
rs.getInt("age"), | ||
rs.getTimestamp("created_date").toLocalDateTime() | ||
) | ||
); | ||
} | ||
|
||
public String findCustomerNameById(Long id) { | ||
|
||
String sql = "SELECT NAME FROM CUSTOMER WHERE ID = ?"; | ||
|
||
return jdbcTemplate.queryForObject( | ||
sql, new Object[]{id}, String.class); | ||
|
||
} | ||
|
||
public int count() { | ||
|
||
String sql = "SELECT COUNT(*) FROM CUSTOMER"; | ||
|
||
// queryForInt() is Deprecated | ||
// https://www.mkyong.com/spring/jdbctemplate-queryforint-is-deprecated/ | ||
//int total = jdbcTemplate.queryForInt(sql); | ||
|
||
return jdbcTemplate.queryForObject(sql, Integer.class); | ||
|
||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
spring-jdbc/src/main/java/com/mkyong/customer/CustomerRowMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.mkyong.customer; | ||
|
||
import org.springframework.jdbc.core.RowMapper; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class CustomerRowMapper implements RowMapper<Customer> { | ||
|
||
@Override | ||
public Customer mapRow(ResultSet rs, int rowNum) throws SQLException { | ||
|
||
Customer customer = new Customer(); | ||
customer.setID(rs.getLong("ID")); | ||
customer.setName(rs.getString("NAME")); | ||
customer.setAge(rs.getInt("AGE")); | ||
customer.setCreatedDate(rs.getTimestamp("created_date").toLocalDateTime()); | ||
|
||
return customer; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters