Reputation: 190
I am working on spring boot and I have to run a lot of update and delete operations on different tables. Is there a library or interface which would help me generalize the query So I dont have to write it again and again.
Here is my code
public int updatetable1(Table table1)
{
return namedParameterJdbcTemplate.update("update table1 set sname = :name," +
"email=:email, mobile=:mobile",new BeanPropertySqlParameterSource(table1));
}
Upvotes: 1
Views: 608
Reputation: 1083
The central interface in the Spring Data repository abstraction is Repository. It takes the domain class to manage as well as the ID type of the domain class as type arguments. This interface acts primarily as a marker interface to capture the types to work with and to help you to discover interfaces that extend this one. The CrudRepository provides sophisticated CRUD functionality for the entity class that is being managed.
Example. CrudRepository interface
public interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S entity);
Optional<T> findById(ID primaryKey);
Iterable<T> findAll();
long count();
void delete(T entity);
boolean existsById(ID primaryKey);
// … more functionality omitted.
}
Derived Query Methods in Spring
Derived method names have two main parts separated by the first By keyword:
List<User> findByName(String name)
The first part – like find – is the introducer and the rest – like ByName – is the criteria.
Spring Data JPA supports find, read, query, count and get. So, for example, we could have done queryByName and Spring Data would behave the same.
We can also use Distinct, First, or Top to remove duplicates or limit our result set:
List<User> findTop3ByAge()
The criteria part contains the entity-specific condition expressions of the query. We can use the condition keywords along with the entity's property names. We can also concatenate the expressions with And and Or.
Upvotes: 2