Mapping query results to Java objects
https://gyazo.com/89f6a1fa04b59cb92048986e10832005 #Core
Mapping query results to container object
When a SELECT sql is executed with Results container, the class is mapped with columns in the SQL result. In other words Sorm4j will use Results container as the data transfer object from java.sql.ResultSet.
For converting the JDBC type to the Java class, Sorm4j refers the types of accessor (which are types of field, types of constructor parameters or types of setter parameter) in the container class. The type of accessor are expected to support returned types to be described.
Examples of Results container are described at Results container.
Example
In the following example, Sorm reads column id by ResultSet#getInt and column name by ResultSet#getString.
The conversion is depend on the Results container not the JDBC types of the column.
If the id column has INTEGER JDBC type and the id field in corresponding container object is String class, java.sql.ResultSet#getString will be called.
code:SQL result container class example.java
public class Customer{
public int id;
public String name;
}
code:Mapping to SQL result container class example.java
// finds customers with sql query and get as List
List<Customer> list = sorm.readList(Customer.class, "select * from customer where id>?", 5);
/yuunkjm/--.icon
Mapping query results to standard Java objects
If a query returns a single column, Sorm can map data directly into the object (either single values or multiple values). The class of the object should be Results container/Supported types, for example, String, Integer, Double....
code:Mapping SQL results to standard Java object Example.java
// reads customer name as String
String name = sorm.read(String.class, "select name from customer where id=?", 1);
// reads all customer id's as a list of integers
List<Integer> ids = sorm.readList(Integer.class, "select id from customer");
/yuunkjm/--.icon
Mapping query results to RowMap
> Results container/RowMap