import static net.sf.persism.Parameters.*; import static net.sf.persism.SQL.*;
Connection con = DriverManager.getConnection(url, username, password);
// Instantiate a Persism session object with the connection Session session = new Session(con);
List<Customer> list = session.query(Customer.class, sql("select * from Customers where CUST_NAME = ?"), params("Fred")); // or List<Customer> list = session.query(Customer.class, proc("sp_FindCustomers(?)"), params("Fred"));
Customer customer; customer = session.fetch(Customer.class, sql("select * from Customers where CUST_NAME = ?"), params("Fred")); // or customer = session.fetch(Customer.class, proc("sp_FindCustomer(?)"), params("Fred")); if (customer != null) { // etc... }
// You don't need the SELECT parts for Views or Tables List<Customer> list = session.query(Customer.class, where("CUST_NAME = ?"), params("Fred"));
// You can reference the property names instead of the column names - just use :propertyName List<Customer> list = session.query(Customer.class, where(":name = ?"), params("Fred"));
// Order by is also supported with where() method List<Customer> list = session.query(Customer.class, where(":name = ? ORDER BY :lastUpdated"), params("Fred"));
// Named parameters are also supported - just use @name SQL sql = where("(:firstname = @name OR :company = @name) and :lastname = @last"); customer = session.fetch(Customer.class, sql, params(Map.of("name", "Fred", "last", "Flintstone")));
// fetch an existing instance Customer customer = new Customer(); customer.setCustomerId(123); if (session.fetch(customer)) { // customer found and initialized }
// Supports basic types String result = session.fetch(String.class, sql("select Name from Customers where ID = ?"), params(10));
// Fetch a count as an int - Enums are supported int count = session.fetch(int.class, sql("select count(*) from Customers where Region = ?"), params(Region.West));
// Insert - get autoinc Customer customer = new Customer(); customer.setCustomerName("Fred"); customer.setAddress("123 Sesame Street");
session.insert(customer);
// Inserted and new autoinc value assigned assert customer.getCustomerId() > 0
// Update customer.setCustomerName("Barney"); sesion.update(customer); // Update Customer
// Delete session.delete(customer);
// Handles transactions session.withTransaction(() -> { Contact contact = getContactFromSomewhere(); contact.setIdentity(randomUUID); session.insert(contact); contact.setContactName("Wilma Flintstone"); session.update(contact); session.fetch(contact); });
|