DAO Example Sormula does not require DAO's but they are
easy to implement.
// set up
Database database = new Database(connection);
InventoryDAO inventoryDAO = new InventoryDAO(database);
// get part by primary key
Inventory inventory = inventoryDAO.select("1234");
// update
inventory.setQuantity(inventory.getQuantity() - 1);
inventoryDAO.update(inventory);
InventoryDAO.java
DAO's are trivial to implement with Sormula.
public class InventoryDAO extends Table<Inventory>
{
public InventoryDAO(Database database) throws SormulaException
{
super(database, Inventory.class);
}
}
The InventoryDAO implemented this way will inherit all of the methods from Table
including these common methods:
public List<R> selectAll()
public R select(Object... primaryKeys)
public R selectWhere(String whereConditionName, Object...parameters)
public List<R> selectAllWhere(String whereConditionName, Object...parameters)
public List<R> selectAllWhereOrdered(String whereConditionName, String orderByName, Object...parameters)
public List<R> selectAllCustom(String customSql, Object... parameters)
public R selectCustom(String customSql, Object... parameters)
public int selectCount()
public int selectCount(String whereConditionName, Object...parameters)
public <T> T selectCount(String expression)
public <T> T selectCount(String expression, String whereConditionName, Object...parameters)
public <T> T selectMin(String expression)
public <T> T selectMin(String expression, String whereConditionName, Object...parameters)
public <T> T selectMax(String expression)
public <T> T selectMax(String expression, String whereConditionName, Object...parameters)
public <T> T selectAvg(String expression)
public <T> T selectAvg(String expression, String whereConditionName, Object...parameters)
public <T> T selectSum(String expression)
public <T> T selectSum(String expression, String whereConditionName, Object...parameters)
public int insert(R row)
public int insertBatch(R row)
public int insertAll(Collection<R> rows)
public int insertAllBatch(Collection<R> rows)
public int update(R row)
public int updateBatch(R row)
public int updateAll(Collection<R> rows)
public int updateAllBatch(Collection<R> rows)
public int delete(Object... parameters)
public int delete(R row)
public int deleteBatch(R row)
public int deleteAll(Collection<R> rows)
public int deleteAllBatch(Collection<R> rows)
public int deleteAll()
public int save(R row)
public int saveBatch(R row)
public int saveAll(Collection<R> rows)
public int saveAllBatch(Collection<R> rows)
public boolean isAutoGeneratedKeys()
public void setAutoGeneratedKeys(boolean autoGeneratedKeys)
public boolean isCached()
public Cache<R> getCache()
public void flush()
public boolean isReadOnly()
public void setReadOnly(boolean readOnly)
public void setRequiredCascades(String... cascadeNames)
public String[] getRequiredCascades()
public Database getDatabase()
public Class<R> getRowClass()
public String getTableName()
public void setTableName(String tableName)
public String getQualifiedTableName()
public void putTypeTranslator(Class<?> typeClass, TypeTranslator<?> typeTranslator)
public void putTypeTranslator(String typeClassName, TypeTranslator<?> typeTranslator)
public TypeTranslator<?> getTypeTranslator(Class<?> typeClass)
INVENTORY schema and Inventory.java used by this example.
See SimpleExample.java class in the Simple Example download.
|