spring boot during spring date jpa (2)

Implementation steps for adding, deleting, and modifying JPA

(1) Add mysql and spring-data-jpa dependencies in pom.xml;
(2) Configure the mysql connection configuration file
in the application.properties file (3) Configure the JPA configuration information in the application.properties file;
(4) Write a Test example;
1: Create an entity class;
the above steps are omitted, see the previous article for details;
2: Create jpa repository class operation persistence (CrudRepository);
query, add and delete basically follow the methods defined in jpa;
Update:

1: You can use the original The save method; as follows: directly call save() to complete the update operation;
  @Transactional
    public void updateUser1(User user) {
        User user1 = userRepository.findOne(user.getId());
        if (user1 != null) {
            if (user .getAge() != null) {
                user1.setAge(user.getAge());
            }
            if (user.getGender() != null) {
                user1.setGender(user.getGender());
            }
            if (user.getName() != null) {
                user1.setName(user.getName());
            }
            userRepository.save(user1);
        }
    }
Second: use annotation to update; call the following custom update as follows The method completes the update operation;
@Modifying(clearAutomatically = true)
@Query(value = "UPDATE user as u SET u.NAME= ?1,u.gender= ?2,u.age= ?3 WHERE u.id = ?4 ",nativeQuery = true)
public int updateUser(String name,String gender,Integer age,Integer id);
3: Create service class;
4: Create controller;
5: Test completed;

GitHub address: https://github.com/ gumeimen/springbootjpab/tree/master

The following introduces the methods of the JpaRepository interface:
1. Query methods:
1. Find all: findAll
2. Find a single: findOne
3. Paging query: findAll(Pageable pageable)
4. Sort: findAll(Sort sort)
5. Query according to entity class attributes: findByProperty (type Property);
6, conditional query: and/or/findByAgeLessThan/LessThanEqual, etc., 
for example: findByUsernameAndPassword(String username, String password)
7, total query count() or query the total count according to the value of a property countById(int id );
two methods of adding, deleting, and modifying:
add:
1, save(T) save single or batch save
2, saveAndFlush save and refresh to the database
delete:
1, delete(T) delete or batch delete;
modify:
use jpa There are two main ways of update operation:
1. Call the method of saving entities
1) Save one entity: repository.save(T entity)
2) Save multiple entities: repository.save(Iterable<T> entities)
3) Save and immediately refresh an entity: repository.saveAndFlush(T entity)
2. @Query annotation, write your own JPQL statement
and use the @Query annotation in JPA to implement the update operation, code as follows:
@Modifying(clearAutomatically = true)
@Query(value = "UPDATE user as u SET u.NAME= ?1,u.gender= ?2,u.age= ?3 WHERE u.id = ?4",nativeQuery = true)
public int updateUser(String name,String gender,Integer age,Integer id);

Remarks:
1. Don't miss this nativeQuery = true.
2. When defining the modification method in JPA, don't pass an object. You should pass as many parameters as you need to modify. Then the sql placeholder is ?1, ?2 and so on.
What I am new is that the entire user has passed all fields. Go in, pay attention to the placeholders of sql and the passed parameters to be consistent, otherwise the modification will not be successful!,
3. @Modifying(clearAutomatically = true) automatically clears the data saved in the entity.

The official website is very detailed. If you use it, you can query the official website. Here are some commonly used ones:

And => equivalent to the and keyword in SQL, for example: findByUsernameAndPassword(String user, Striang pwd);

Or => equivalent to or in SQL Keywords, for example: findByUsernameOrAddress(String user, String addr);

Between => equivalent to between keyword in SQL, for example: SalaryBetween(int max, int min);

LessThan => equivalent to "<" in SQL , for example: findBySalaryLessThan(int max);

GreaterThan => equivalent to ">" in SQL, for example: findBySalaryGreaterThan(int min);

IsNull => equivalent to "is null" in SQL, for example: findByUsernameIsNull();

IsNotNull => is equivalent to "is not null" in SQL, for example: findByUsernameIsNotNull();

NotNull=> is equivalent to IsNotNull;

Like => is equivalent to "like" in SQL, for example: findByUsernameLike(String user);

NotLike => equivalent to "not like" in SQL,例如: findByUsernameNotLike(String user);

OrderBy => is equivalent to "order by" in SQL, for example: findByUsernameOrderBySalaryAsc(String user);

Not => is equivalent to "!=" in SQL, for example: findByUsernameNot(String user);

In => is equivalent to "in" in SQL, for example: findByUsernameIn(Collection<String> userList) , the parameter of the method can be a Collection type, an array or a variable-length parameter;

NotIn => is equivalent to "not in" in SQL, for example : findByUsernameNotIn(Collection<String> userList) , the parameters of the method can be of Collection type, arrays or indefinite-length parameters;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649224&siteId=291194637