简体   繁体   中英

How to write update statement in spring boot without query?

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));
    }

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.
}
  1. Saves the given entity.
  2. Returns the entity identified by the given
  3. ID. Returns all entities. Returns the number of entities.
  4. Deletes the given entity.
  5. Indicates whether an entity with the given ID exists.

Spring Data

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM