Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Mapeando um ResultSet JDBC para um objeto


Se você não quiser usar nenhum provedor JPA, como OpenJPA ou Hibernate, basta experimentar o Apache DbUtils.

http://commons.apache.org/proper/commons-dbutils/examples .html

Então seu código ficará assim:
QueryRunner run = new QueryRunner(dataSource);

// Use the BeanListHandler implementation to convert all
// ResultSet rows into a List of Person JavaBeans.
ResultSetHandler<List<Person>> h = new BeanListHandler<Person>(Person.class);

// Execute the SQL statement and return the results in a List of
// Person objects generated by the BeanListHandler.
List<Person> persons = run.query("SELECT * FROM Person", h);