@Documented @Target(value=METHOD) @Retention(value=RUNTIME) public @interface Update
Annotation used to decorate a method in a Query interface with a SQL statement that can return an update count or can return auto generated keys. The Update annotation must not be used in-conjunction with a SQL statement that returns a ResultSet.
A Query interface may contain zero, one or more methods decorated with an Updateannotation.
The following provides an example of using the Update annotation.
// Define DataSet type public class Mammal { public String name; public String description; public int age; public int weight; } // Define the interface containing the methods representing SQL statements // that can be invoked. interface MyQueries extends BaseQuery { @Select("select name, description, age from mammal") DataSet<Mammal> getAllMammals(); @Update("delete from mammal") int deleteAllMammals(); }To invoke the deleteAllMammals() method, an instance of the MyQueries interface must be created by invoking either the Connnection.createQueryObject or DataSource.createQueryObject method.
MyQueries mq = con.createQueryObject(MyQueries.class); int success = mq.deleteAllMammals();
interface MyQueries extends BaseQuery { @Update(sql="update mammal set description= ?1 where age <10") int setDescription(String desc); }
MyQueries mq = con.createQueryObject(MyQueries.class); int success = mq.setDescription("a young mammal");
When the setDescription() method is invoked, the value specified for the parameter desc will be used as the value for the parameter marker.
In the above example, the MyQueries.addPerson method indicates that it can return auto generated keys upon its execution. The returned auto generated keys may be accessed as follows:@AutoGeneratedKeys public class TabKeys { public String col1;
public String col2;} public interface MyQueries extends BaseQuery{ @Update(sql="insert intotabName(?1, ?2)", keys=GeneratedKeys.RETURNED_KEYS_DRIVER_DEFINED)
tabName({name}, {age})", keys=GeneratedKeys.ALL_KEYS)DataSet<TabKeys>
DatSet<TabKeys>addPerson(String name, int age); }
MyQueries mq = con.createQueryObject(MyQueries.class); DataSet<TabKeys> keys = mq.addPerson("Jane Doe", 29); for (TabKeys key : keys) { System.out.println("Key=" + key.col1);
+ "," + key.col2);}
interface MyQueries extends BaseQuery { @Update(sql="update mammal set weight = 5 where weight >?1")
{weight}")int shrinkBigMammals(int weight); }
A returned value of 0 indicates that no rows were updated. A returned value greater than 0 indicates that 1 or more rows were affected by the method invocation.
Optional Element Summary | |
---|---|
GeneratedKeys |
keys
Determines whether auto-generated keys are returned or not |
String |
sql
The SQL command to execute. |
String |
value
The SQL command to execute. |
public abstract String sql
Note : If the sql and the value annotation element are specified at the same time, a SQLRuntimeException will be thrown.
public abstract GeneratedKeys keys
public abstract String value
Note : If the sql and the value annotation element are specified at the same time, a SQLRuntimeException will be thrown.