Basic usage
https://gyazo.com/ed6df50c3b2e854f351aa2a1327e18ad #Core
Overview
We shows short typical examples here. yuu-nkjm/sorm4j-example: Example of Sorm4j provides complete examples.
We also comparing syntax with other library, see Comparing syntax page.
org.nkjmlab.sorm4j.Sorm is the main entry point.
/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);
Execute CREATE TABLE SQL/Execute plain sql
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()));
Join > JOIN
code:java
List<Tuple2<Customer, Address>> result = sorm.joinOn(Customer.class, Address.class, "customer.address=address.name");
/yuunkjm/--.icon
Update
Table name is guessed by DefaultTableNameMapper > Binding Java object to SQL parameters
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
Others
JOIN
Handling multiple statements and transaction
Container (sorm4j.container)