简体   繁体   中英

Generic class with parameters

public class ModelMapper implements ResultSetMapper<Model> {
    public Model map(int index, ResultSet resultSet, StatementContext ctx) throws SQLException {
        // Get result set meta data & column count
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columns = metaData.getColumnCount();

        Map<String, Object> map = new HashMap<>(columns);
        for (int i = 1; i <= columns; ++i) {
            String columnName = metaData.getColumnName(i);
            map.put(columnName, resultSet.getObject(columnName));
        }

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);

        return objectMapper.convertValue(map, Model.class);
    }
}

In my ModelImplDao.class I have registered it as @RegisterMapper(ModelMapper.class) . Since I can see that ModelMapper class there is exactly one parameter Model , how can I create a class that would be generic and parameterised to take Model as a parameter and I am also able to register it in ModelImplDao.class as a mapper?

We can't use ResultMapper where T is generic. It needs to know the type. Alternatively you can use this library ( http://manikandan-k.github.io/jdbi_folder/ ). It provides a way remove the mappers. This library will take care of mapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM