Advertisement
JeffGrigg

Untitled

Mar 9th, 2024
1,538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.88 KB | None | 0 0
  1. import org.springframework.jdbc.core.RowMapper;
  2.  
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5.  
  6. public class CustomerRowMapper implements RowMapper<Customer> {
  7.  
  8.     @Override
  9.     public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
  10.  
  11.         Customer customer = new Customer();
  12.         customer.setID(rs.getLong("ID"));
  13.         customer.setName(rs.getString("NAME"));
  14.         customer.setAge(rs.getInt("AGE"));
  15.         customer.setCreatedDate(rs.getTimestamp("created_date").toLocalDateTime());
  16.  
  17.         return customer;
  18.  
  19.     }
  20. }
  21.  
  22.  
  23. import org.springframework.jdbc.core.JdbcTemplate;
  24.  
  25.     @Autowired
  26.     private JdbcTemplate jdbcTemplate;
  27.    
  28.     public Customer findByCustomerId(Long id) {
  29.  
  30.         String sql = "SELECT * FROM CUSTOMER WHERE ID = ?";
  31.  
  32.         return jdbcTemplate.queryForObject(sql, new Object[]{id}, new CustomerRowMapper());
  33.  
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement