@Retention(value=RUNTIME) @Target(value=METHOD) public @interface BindSqlInsert
For example suppose we want to persist bean Person defined as
follow:
@BindType
public class Person {
public long id;
public String name;
public String surname;
public String birthCity;
public Date birthDay;
}
The associated DAO interface is
@BindDao(Person.class)
public interface PersonDAO {
}
It's possible define a INSERT query with annotation BindSqlInsert. It
is possibile to define query parameter simply using method parameter with
same name of the bean property.
@BindDao(Person.class)
public interface PersonDAO {
@BindInsert
void insertOne(String name, String surname, String birthCity, Date birthDay);
@BindInsert
long insertTwo(String name, String surname, String birthCity, Date birthDay);
}
Each method parameter will be use like input parameter for query. The name of
parameters will be used to map field bean and then the column name of the
associated table. If you specify a return type for methods (like method
insertTwo), it has to be of type
int, long, Integer, Long. In this case, the return value will be
the id value of just inserted row.
The other way to define an INSERT query is using a bean as input parameter:
@BindDao(Person.class)
public interface PersonDAO
{
@BindInsert(excludedFields={"name", "surname"})
void insertThree(Person bean);
}
You can use attribute value to define which property insert into query
or you can use attribute excludedFields to avoid to insert some
fields, but you can not use both attributes in the same method
definition. Values of this attribute will be used like bean property
name. At the end of the insert, bean will have id value set to last row id
used to insert bean into table. If you specify a return type for methods, it
has to be of type int, long, Integer, Long and it will contains
same value like row id.
| Modifier and Type | Optional Element and Description |
|---|---|
ConflictAlgorithmType |
conflictAlgorithm
Type of INSERT.
|
String[] |
excludedFields
properties to exclude into INSERT command.
|
boolean |
includePrimaryKey
Allow to include primary key into INSERT command.
|
String[] |
value
bean properties to include into INSERT command.
|
public abstract ConflictAlgorithmType conflictAlgorithm
Type of INSERT. Default value is ABORT. See here for more info.
public abstract String[] value
bean properties to include into INSERT command. To use only if method have only one parameter and its type is the same of supported bean.
public abstract boolean includePrimaryKey
Allow to include primary key into INSERT command. To use only if method have only one parameter and its type is the same of supported bean.
public abstract String[] excludedFields
properties to exclude into INSERT command. To use only if method have only one parameter and its type is the same of supported bean.
Copyright © 2017. All rights reserved.