Mapping SQL results to Java objects
https://gyazo.com/a08b0cdc5137ba21645efb6320f27145
Mapping SQL result to container object
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. Example
In the following example, Sorm reads column id by ResultSet#getInt and column name by ResultSet#getString.
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 SQL 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 supported returned 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 SQL result to RowMap
RowMap instances can be used as SQL result container class. RowMap extends Map<String, Object>. Sorm will convert a row in SQL result to a RowMap object. The types of values are depending on JDBC type. Keys of the map are the columns name in lower case returned from the database. code:Mapping SQL results to java.util.Map example.java
// reads a customer using a custom query and return the result as a map
RowMap customerMap = sorm.readOne(RowMap.class, "select * from customer where id=?", 10); => "{id=10, name="Alice"}"
// reads all customers and result the results as Map instances in a List