Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Spring annotations and proxy mode

Shulou Source: shulou.com Published: 2022-06-01 23:07:26 10月24日 Update

This article mainly explains "how to use Spring annotations and proxy mode". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Spring annotations and proxy mode".

I. SpringDataJpa1.pom.xml configuration

-1. Import of basic SSJ-2.SpringDataJpa

Org.springframework.data spring-data-jpa ${spring-data-jpa.version} 2.SpringDataJpa basic configuration

1. Configure the corresponding Spring-data-jpa

two。 Inherit JpaRepository interface EmployeeRepository extends JpaRepository, JpaSpecificationExecutor

3. Complete the corresponding CRUD

EmployeeRepository.findAll (); employeeRepository.findOne (Long id); employeeRepository.save (object); / / add or modify employeeRepository.delete (id/ object); employeeRepository.findAll (Pageable)-> paging Pageable pageable = new PageRequest (0,5); / / 0 is the first page employeeRepository.findAll (Sort)-> sort Sort sort = new Sort (Sort.Direction.DESC, "username") If you want to combine paging and sorting: new PageRequest (0,5-sort); 3. Advanced query 1. Name rule findByUsername (String username)-> query username by name =? FindByUsernameLike (String username)-> username like? FindByUsernameLikeAndEmailLike (String username,String email)-> username like? And email like? For detailed rules, please see the file (idea also has a hint) 2.Query Note @ Query ("jpql statement") @ Query ("select o from Employee o where o.name like? 1") / / @ Query ("select o from Employee o where o.name like: name") @ Query (nativeQuery = true,value= "select * from employee") II. Advanced query and paging 1.JpaSpecificationExecutor

You can complete the function without writing SQL.

1. Our Repository inherits JpaSpecificationExecutorEmployeeRepository extends JpaRepository, and the query method of JpaSpecificationExecutor2.findAll employeeRepository.findAll (Specification spec) employeeRepository.findAll (Specification spec,Pageable pageable) new Specification {/ / root: (root, table) get fields / / query:where,group by, order by... / / cb: conditional judgment (one or more) @ Override public Predicate toPredicate (Root root, CriteriaQuery query, CriteriaBuilder cb) {Path path = root.get ("username"); / / get the field to query Predicate p = cb.like (path, "% 1") / / like represents a fuzzy query, followed by its conditional value return p;}} 2.jpa-spec com.github.wenhao jpa-spec 3.1.0 max / first parameter: true-> conditional filtering enabled / / second parameter: attribute to be filtered / / third parameter: filter conditional? Corresponding value Specification specification = Specifications.and () .eq (StringUtils.isNotBlank (request.getName ()), "name", name) .gt (Objects.nonNull (request.getAge ()), "age", 18) .build (); the extraction Query function of 3.Query: receiving query conditions (paging, data) sent from the foreground BaseQuery: common query [specification, common code] int currentPage=1 GetJpaPage {return currentPage-1} int pageSize=10; String orderName; Boolean orderType=true; provides the getter,setter method / / specifies that the subclass must provide a method protected abstract Specification createSpec () that gets the condition; / / provides the method Sort createSort () {/ / 1 to get the sort. If orderName has no value, it returns null [do not sort] / / 2. If orderType is DESC, upgrade (otherwise descend)} EmployeeQuery:Employee 's separate query username,email,age,... Specification createSpec () {...} Thank you for your reading, the above is the content of "how to use Spring annotations and proxy mode". After the study of this article, I believe you have a deeper understanding of how to use Spring annotations and proxy patterns, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Query condition annotation pattern agent method sort parameter that is learning configuration advanced content name field object rule three code representative Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux Redmi Shulou Tech Info Shulou Technology