Search code examples
javadatabasespringdaodto

Using Spring for database and webpage integration


I am writing my first java/DB/web app. I am new to java/javascript/css/html so this project is a learning experience for me. The app will display results from a DB in a web page and also allow users to edit/add info to the DB from the web page.

I am using spring and the general flow of the program is as follows: Web Page -> some javascript -> Reference Rest Service -> Reference Service -> DAO -> DTO -> DB

The problem is that the app currently has a different DAO and DTO for every query. I would like to simplify the program so that there is a single DAO and DTO that can handle all queries.

I'm trying to create a DTO that could handle anywhere from 2 to 10 different slots, I would like each slot to have a name instead of a number associated with it. ALso I believe that the name of the slot should be set by the return from the DB. Then I need a reliable way to map data to rows in the DAO. I will post some simple sample code to show what I am working with.

@Repository
public class DecisionLevelDaoImpl implements DecisionLevelDao {

private JdbcTemplate jdbcTemplate;

@Autowired
public DecisionLevelDaoImpl(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}
private static String FIND_ACTIVE_DECISION_LEVEL = DaoHelper
        .loadResourceToString("queries/DecisionLevel_FindActive.sql");
@Override
public List<DecisionLevelDTO> findActiveDecisionLevel() {
    return jdbcTemplate.query(FIND_ACTIVE_DECISION_LEVEL, new DecisionLevelDTORowMapper());
}
private class DecisionLevelDTORowMapper implements RowMapper<DecisionLevelDTO> {
    @Override
    public DecisionLevelDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
        String levelCd = rs.getString("LevelCd");
        String levelName = rs.getString("LevelName");
        String levelInstruct = rs.getString("LevelInstruct");
        String active = (rs.getBoolean("active") ? "Y" : "N");
        String printOrder = Integer.toString(rs.getInt("PrintOrder"));
        String lastUpdUser = rs.getString("LastUpdUser");
        return new DecisionLevelDTO (levelCd, levelName,
                   levelInstruct, active, printOrder,lastUpdUser);
    }
}

Here is the DTO:

import org.codehaus.jackson.annotate.JsonProperty;

public class DecisionLevelDTO {

@JsonProperty("levelCd")
private String levelCd;
@JsonProperty("levelName")
private String levelName;
@JsonProperty("levelInstruct")
private String levelInstruct;
@JsonProperty("active")
private String active;
@JsonProperty("printOrder")
private String printOrder;
@JsonProperty("lastUpdUser")
private String lastUpdUser;

public DecisionLevelDTO () { }
public DecisionLevelDTO (String levelCd, String levelName,
        String levelInstruct, String active, String printOrder,
        String lastUpdUser) {
    super();
    this.levelCd = levelCd;
    this.levelName = levelName;
    this.levelInstruct = levelInstruct;
    this.active = active;
    this.printOrder = printOrder;
    this.lastUpdUser = lastUpdUser;
}

There are currently about 25 Dao's and 25 DTO's. This seems horribly repetitive and I would love to find a method that is more maintainable.

I'm not looking for anyone to write my code, I'm doing this to learn so free code won't help me at all. All the tutorials I see have a single DAO and a single DTO that returns results from a single query.

Sorry if this is the wrong forum for this type of question.


Solution

  • I got your point now. What you can do is create separate generic methods for DML and DQL queries.

    These methods would accept the query and one ResultSet(in case of DQL) or PreparedStatement(in case of DML). Again you can divide the DML into Update/ Delete/INsert according to your needs.

    Now when you get a query you know what the query does. Accordingly set the ResultSet or PreparedStatement as applicable and pass the query along with it and get the results.
    Let me know if it is not clear.