-
Notifications
You must be signed in to change notification settings - Fork 16
@BindDao
xcesco edited this page Apr 6, 2018
·
5 revisions
This annotation decorate a DAO interface associated to a database schema. A DAO is associated to Java bean class. Kripton allow you to define what query you'll use, Kripton generate code for you.
Every method of a DAO interface have to be marked with one of these annotations: @BindSqlInsert, @BindSqlUpdate, @BindSqlSelect and @BindSqlDelete
- value: bean class to associate with this dao definition.
Suppose we have the following DaoBean03
DAO interface
@BindDao(Bean03.class)
public interface DaoBean03 {
@BindSqlSelect(where="id=${id}")
Bean03 selectOne(long id);
@BindSqlDelete(where="id=${id}")
long deleteOne(long id);
}
And Bean03
definition:
@BindType
public class Bean03 {
@Bind
@BindColumn(columnType = ColumnType.PRIMARY_KEY)
protected Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Bind
@BindColumn
protected String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
When Kripton Annotation Processor evaluate DaoBean03
, it generate the following DAO implementations:
/**
* <p>
* DAO implementation for entity <code>Bean03</code>, based on interface <code>DaoBean03</code>
* </p>
*
* @see Bean03
* @see DaoBean03
* @see Bean03Table
*/
public class DaoBean03Impl extends AbstractDao implements DaoBean03 {
public DaoBean03Impl(BindDummy03DataSource dataSet) {
super(dataSet);
}
/**
* <h2>Select SQL:</h2>
* <p>
* <pre>SELECT id, text FROM bean03 WHERE id=${id}</pre>
*
* <h2>Projected columns:</h2>
* <p>
* <dl>
* <dt>id</dt><dd>is associated to bean's property <strong>id</strong></dd>
* <dt>text</dt><dd>is associated to bean's property <strong>text</strong></dd>
* </dl>
*
* <h2>Query's parameters:</h2>
* <p>
* <dl>
* <dt>${id}</dt><dd>is binded to method's parameter <strong>id</strong></dd>
* </dl>
*
* @param id
* is binded to ${id}
*
* @return selected bean or <code>null</code>.
*/
@Override
public Bean03 selectOne(long id) {
// build where condition
String[] args={String.valueOf(id)};
Logger.info(StringUtils.formatSQL("SELECT id, text FROM bean03 WHERE id='%s'"),
(Object[])args);
Cursor cursor = database().rawQuery("SELECT id, text FROM bean03 WHERE id=?",
args);
Logger.info("Rows found: %s",cursor.getCount());
Bean03 resultBean=null;
if (cursor.moveToFirst()) {
int index0=cursor.getColumnIndex("id");
int index1=cursor.getColumnIndex("text");
resultBean=new Bean03();
if (!cursor.isNull(index0)) { resultBean.setId(cursor.getLong(index0)); }
if (!cursor.isNull(index1)) { resultBean.setText(cursor.getString(index1)); }
}
cursor.close();
return resultBean;
}
/**
* <p>SQL delete:</p>
* <pre>DELETE bean03 WHERE id=${id}</pre>
*
* <p><strong>Where parameters:</strong></p>
* <dl>
* <dt>${id}</dt><dd>is mapped to method's parameter <strong>id</strong></dd>
* </dl>
*
* @param id
* is used as where parameter <strong>${id}</strong>
*
* @return number of deleted records
*/
@Override
public long deleteOne(long id) {
String[] whereConditions={String.valueOf(id)};
Logger.info(StringUtils.formatSQL("DELETE bean03 WHERE id=%s"), (Object[])whereConditions);
int result = database().delete("bean03", "id=?", whereConditions);
return result;
}
}
DAO implementations contains null checks and javadoc for every method that are present in DAO interface and log for executed query (if in DataSource attribute log is set to true).
- Introduction
- Goals & Features
- Kotlin
- Immutable or Mutable Pojo
- Annotation Processor Args
- Credits
- Articles
- Benchmarks
- Setup
- Tutorial
- Usage
- Dependencies and inspirations
- Stackoverflow
- Documentation
- SQL logging
- Data source options
- Indices
- SQL Type adapter
- Global SQL Type adapter
- Constraints
- Live data: welcome Architectural components!!
- Paged Live data
- Dynamic parts
- Transactional and batch operations
- Async Transactional and batch operations
- Global transaction
- Support for immutable POJO
- Generate Content provider
- Generate Database schema generation
- Database migration
- BindSqlColumn
- BindContentProvider
- BindContentProviderEntry
- BindContentProviderPath
- BindDao
- BindDaoMany2Many
- BindDataSource
- BindDataSourceOptions
- BindDataSourceUpdateTask
- BindIndex
- BindSqlRelation
- BindSqlAdapter
- BindSqlChildSelect
- BindSqlDelete
- BindSqlDynamicOrderBy
- BindSqlDynamicWhere
- BindSqlDynamicWhereParams
- BindSqlInsert
- BindSqlPageSize
- BindSqlParam
- BindSqlSelect
- BindSqlUpdate
- BindSqlType
- BindSqlTransaction