In my user DAO implementation, I have 2 methods "findAll" and "findByID", which are very similar. This causes two problems:
- (1) duplicate SQL queries
- (2) duplicate mapping code between the resultSet and the user object
Does anyone know a good solution to prevent these duplicates? Also, pls don't advise any frameworks or libraries! Thanks :)
@Override
public List<User> findAll() {
List<User> userList = new ArrayList<>();
String query = "SELECT users.id AS user_id, users.default_delivery_address, users.default_billing_address, users.first_name," +
"users.last_name, users.created_at, users.password, users.email_address, delivery_address.id AS delivery_id, " +
"delivery_address.country_code AS delivery_country_code, delivery_address.street AS delivery_street, delivery_address.street_number AS delivery_street_number, delivery_address.postal_code AS delivery_postal_code, " +
"delivery_address.first_name AS delivery_first_name, delivery_address.last_name AS delivery_last_name, delivery_address.city AS delivery_city, " +
"delivery_address.additional AS delivery_additional, billing_address.id AS billing_id, billing_address.country_code, billing_address.street, " +
"billing_address.street_number, billing_address.postal_code, billing_address.first_name AS billing_first_name, " +
"billing_address.last_name AS billing_last_name, billing_address.city, billing_address.additional FROM users inner join address delivery_address on " +
"delivery_address.id = users.default_delivery_address INNER JOIN address billing_address ON (billing_address.id = users.default_billing_address)";
try (Statement stmt = connection.createStatement()) {
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
Address deliveryAddress = new Address(rs.getInt("delivery_id"),rs.getInt("delivery_country_code"),rs.getString("delivery_street"),rs.getString("delivery_street_number"),rs.getString("delivery_postal_code"),rs.getString("delivery_first_name"),rs.getString("delivery_last_name"),rs.getString("delivery_city"), rs.getString("delivery_additional"));
Address billingAddress = new Address(rs.getInt("billing_id"),rs.getInt("country_code"),rs.getString("street"),rs.getString("street_number"),rs.getString("postal_code"),rs.getString("first_name"),rs.getString("last_name"),rs.getString("city"), rs.getString("additional"));
User user = new User(rs.getInt("user_id"),deliveryAddress,billingAddress,rs.getString("first_name"),rs.getString("last_name"),rs.getDate("created_at"),rs.getString("password"),rs.getString("email_address"));
userList.add(user);
}
} catch (SQLException e) {
LOG.error(e.getMessage());
}
return userList;
}
@Override
public User findByID(int id) {
String query = "SELECT users.id AS user_id, users.default_delivery_address, users.default_billing_address, users.first_name," +
"users.last_name, users.created_at, users.password, users.email_address, delivery_address.id AS delivery_id, " +
"delivery_address.country_code AS delivery_country_code, delivery_address.street AS delivery_street, delivery_address.street_number AS delivery_street_number, delivery_address.postal_code AS delivery_postal_code, " +
"delivery_address.first_name AS delivery_first_name, delivery_address.last_name AS delivery_last_name, delivery_address.city AS delivery_city, " +
"delivery_address.additional AS delivery_additional, billing_address.id AS billing_id, billing_address.country_code, billing_address.street, " +
"billing_address.street_number, billing_address.postal_code, billing_address.first_name AS billing_first_name, " +
"billing_address.last_name AS billing_last_name, billing_address.city, billing_address.additional FROM users inner join address delivery_address on " +
"delivery_address.id = users.default_delivery_address INNER JOIN address billing_address ON (billing_address.id = users.default_billing_address) WHERE users.id = ?";
try(PreparedStatement getUserStatement = connection.prepareStatement(query)){
getUserStatement.setInt(1,id);
ResultSet rs = getUserStatement.executeQuery();
if(rs.next()){
Address deliveryAddress = new Address(rs.getInt("delivery_id"),rs.getInt("delivery_country_code"),rs.getString("delivery_street"),rs.getString("delivery_street_number"),rs.getString("delivery_postal_code"),rs.getString("delivery_first_name"),rs.getString("delivery_last_name"),rs.getString("delivery_city"), rs.getString("delivery_additional"));
Address billingAddress = new Address(rs.getInt("billing_id"),rs.getInt("country_code"),rs.getString("street"),rs.getString("street_number"),rs.getString("postal_code"),rs.getString("first_name"),rs.getString("last_name"),rs.getString("city"), rs.getString("additional"));
User user = new User(rs.getInt("user_id"),deliveryAddress,billingAddress,rs.getString("first_name"),rs.getString("last_name"),rs.getDate("created_at"),rs.getString("password"),rs.getString("email_address"));
return user;
}
} catch(SQLException e){
LOG.info(e.getMessage());
}
return null;
}
Aucun commentaire:
Enregistrer un commentaire