Content deleted Content added
Syntax highlight |
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags |
||
Line 26:
By default, if no annotations specify a given class should not be mapped to a table, Persist will try to find a table that matches that class and create a mapping between fields and columns.
<
// Inserts a new customer (the class Customer is mapped to the table customer automatically)
persist.insert(customer);
Line 38:
// Fetch all customers and assign the ResultSet to an Iterator
Iterator allCustomersIterator = persist.readIterator(Customer.class, "select * from customer");
</syntaxhighlight>
'''POJOs not mapped to tables'''
If a class is annotated with @NoTable, Persist will not try to map it to a table, and the class will only be able to hold data produced by queries.
<
@NoTable
class QueryData {
Line 57:
QueryData qd1 = persist.read(QueryData.class, "select 1 as count, 'hello' as concat_name from dual");
</syntaxhighlight>
'''java.util.Map's'''
Map's can be used to hold data from queries. Persist will convert values returned from the query to Java types. Keys in the table are the names of the columns returned in lower case.
<
// Fetch a customer using a custom query and return the result as a map
Map<String,Object> customerMap = persist.readMap("select * from customer where id=?", 10);
Line 71:
// Fetch all customers and assign the ResultSet to an Iterator which maps rows to Map instances
Iterator allCustomersIterator = persist.readMapIterator("select * from customer");
</syntaxhighlight>
'''Java primitive types'''
If a query returns a single column, Persist can map data directly into primitive types (either single values or lists):
<
// Return customer name as String
String name = persist.read(String.class, "select name from customer where id=?", 55);
Line 82:
// Fetch all customer id's as a list of integers
List<Integer> ids = persist.readList(Integer.class, "select id from customer");
</syntaxhighlight>
'''Custom queries with no returning data'''
Arbitrary queries that return no data can be easily executed.
<
// Execute arbitrary SQL with parameters
persist.executeUpdate("delete from customer where id in (?,?)", 10, 20);
</syntaxhighlight>
==See also==
|