Good evening guys, I'm trying to implement a jdbctemplate model with controller / Service / Dao / DaoImpl / and Mapper ... But by the models I see the mapper needs to implement RowMapper or ParameterizedRowMapper and both have a maprow method that does not return a List. As I needed a List I implemented a method in the mapper to get me the list I needed. but I do not know what to call it. In the customerList method I have to pass the CustomerMapper inside the query like this:
jdbcTemplate.query (sql, new CustomerMapper (), id);
And the customer mapper must implement either RowMapper or ParameterizedRowMapper so that the jdbcTemplate.query accept it and along with RowMapper or ParameterizedRowMapper the maprow method must come.
When I call the Mapper method via the listCustomer when entering the CustomerMapper class it automatically enters the first mapRow method and does not enter the method I wanted listCustomer that would return the List that I need.
Any idea how to help me do this?
I need to return a list of customers. just this! But following this form of implementation...
Thank you!
My class:
@Controller
public class CustomerController {
@Autowired
private CustomerService customerService;
@ResponseBody
@RequestMapping(value = "/customer/{id}", method = RequestMethod.GET)
public Map<String, Object> searchCustomer(@PathVariable(value="id") Long id,
final HttpServletRequest request) throws IOException, SQLException {
Map<String, Object> map = new HashMap<String, Object>();
List<Customer> customerList = customerService.searchCustomer(id);
map.put("customer", customerList);
return map;
}
}
SERVICE
@Service
public class CustomerService {
@Autowired
private CustomerDAO dao;
public List<Customer> searchCustomer(Long id) throws SQLException {
return dao.listCustomer(id);
}
}
DAO
public interface CustomerDAO {
List<Customer> listCustomer(Long id);
}
DAOIMPL
public class CustomerDAOImpl implements CustomerDAO {
@Autowired
private SimpleJdbcTemplate jdbcTemplate;
String sql = "SELECT * FROM purchases C WHERE C.ID = ?";
public List<Customer> listCustomer(Long id) {
return jdbcTemplate.query(sql, new CustomerMapper(), id);
}
}
//MAPPER
public class CustomerMapper implements RowMapper<Customer>{
public Customer mapRow(ResultSet rs, int arg1) throws SQLException {
... "N" RULES INSIDE BUT DONT RETURN ArrayList OR LIST.... ANYAWAY..
//THIS METHOD IS INTERFACE INHERITANCE AND HAS TO BE IMPLEMENTED
//BUT I NEED A METHOD THAT RETURNS A LIST OF CUSTOMERS FOR THE CONTROLLER
//AND THEREFORE IT RETURNS JUST 1 CUSTOMER
}
//SO I CREATED THIS OTHER METHOD THAT RETURNS A LIST OF CUSTOMERS AND
//IMPLEMENTED IN THE INTERFACE ... BUT I DO NOT KNOW HOW THE MAPPER CALLS IT ...
//NOT MAPROW
public List<Customer> listCustomer(ResultSet rs, int arg1) throws SQLException {
List<Customer> customerList = new ArrayList<Customer>();
Customer customer = new Customer();
while (rs.next()) {
if (rs.getString("ID") != null)
customer.setEtpId(rs.getString("ID"));
......
......
customerList.add(customer);
}
return customerList;
}
}
sorry, I did not put a whole query of an example, but I'm sorry if it was poorly explained. Well, what I need is to pass the customer number to make a select in the table of purchases for example, and bring me all purchases from that customer.
for example.
Select * from table purchases p where p.customerid = 4;
This would be the query, so let's imagine that it returns 5 records. I want the mapper to return me a list of buying objects (not customer) with the 5 purchases that that customer made.
Understood now?
I will set the example for better understanding.
Thanks for the answers!