Examples
https://gyazo.com/ed6df50c3b2e854f351aa2a1327e18ad
Overview
org.nkjmlab.sorm4j.Sorm is the main entry point.
code:java
Customer c1 = sorm.readFirst(Customer.class, "select * from customer");
sorm.exeuteUpdate(CREATE TABLE customer (id INT PRIMARY KEY, name VARCHAR, address VARCHAR));
/yuunkjm/--.icon
Table schema
code:sql
CREATE TABLE customer (id INT PRIMARY KEY, name VARCHAR, address VARCHAR)
/yuunkjm/--.icon
Create entry point
code:java
Sorm sorm = Sorm.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;");
or
DataSource dataSource = Sorm.createDataSource("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;", "username", "password");
Sorm sorm = Sorm.create(dataSource);
/yuunkjm/--.icon
Execute plain sql
Create table
code:java
sorm.executeUpdate("CREATE TABLE customer (id INT PRIMARY KEY, name VARCHAR, address VARCHAR)");
/yuunkjm/--.icon
Read
Read all rows
code:java
List<Customer> customers = sorm.selectAll(Customer.class);
Read selected rows
code:java
List<Customer> customers = sorm.readList(Customer.class, "select * from customer where id=? and address=?", 1, "Tokyo");
Read a row by primary key
code:java
Customer customers = sorm.selectByPrimaryKey(Customer.class, 1);
Read all rows as stream
code:java
List<Customer> customers = sorm.streamAll(Customer.class)
.apply(stream -> stream.map(c -> "Hello, " + c.getName()).collect(Collectors.toList()));
/yuunkjm/--.icon
Update
Insert / multirow insert
code:java
sorm.insert(new Customer(1, "Alice", "Tokyo"));
sorm.insert(new Customer(2, "Bob", "Tokyo"), new Customer(3, "Carol", "Osaka"), new Customer(4, "Dave", "Nara"));
Update
code:java
sorm.update(new Customer(1, "Alice", "Tokyo"), new Customer(2, "Bob", "Tokyo"));
Delete
code:java
sorm.delete(new Customer(1), new Customer(2));
Merge
code:java
sorm.merge(new Customer(1, "Alice", "Tokyo"), new Customer(2, "Bob", "Tokyo"));
Insert object to user specified table
code:java
sorm.insert("CUSTOMER_LOG", new Customer(1, "Alice", "Tokyo"));
/yuunkjm/--.icon