Spring-Data-Jpa environment configuration and practical application,

Source: Internet
Author: User

Spring-Data-Jpa environment configuration and practical application,

Last time we talked about Spring-Data-Jpa overview and interface, next we will talk about Spring-Data-Jpa environment configuration and practical application.

Spring-Data method definition specification and usage Configuration

Simple Condition query: queries an object class or set.
According to the Spring Data specification, the query method starts with "find | read | get". When conditional queries are involved, the attributes of the conditions are connected using the condition keyword. Note that the condition attributes are capitalized.


For example, define an Entity object class:

class User{    private String firstName;      private String lastName; }

When using the And condition connection, you should write as follows:

 
findByLastNameAndFirstName(String lastName,String firstName);

The attribute names and numbers of conditions must correspond to the parameter positions and numbers one by one.
The query method is defined directly in the interface. If the query method complies with the specifications, you do not need to write the implementation. Currently, the supported keywords are written as follows:

 

@ Query Annotation
This type of query can be declared in the Repository method to get rid of the constraints such as the name query. The query is directly declared in the corresponding interface method, and the structure is clearer. This is a special implementation of Spring data.

 

 

@ Query to specify a local Query

Custom Repository Method
Define an interface: declare the method to be added and implement it.
Provides the implementation class for this interface: the class name must be added after the Repository to be declared, and the Implementation Method
Declare the Repository interface, and inherit from 1) declared Interface
Use.
Note: by default, Spring Data finds "Interface Name Impl" in base-package as the implementation class. You can also declare the suffix through repository-impl-postfix.

Environment configuration and practical application

Coordinate configuration of pom. xml file

<Properties> <project. build. sourceEncoding> UTF-8 </project. build. sourceEncoding> <slf4j. version> 1.7.0 </slf4j. version> <junit. version> 4.12 </junit. version> <spring. version> 4.3.2.RELEASE </spring. version> 

 

Dao interface instance code

Package com. shsxt. dao; import com. shsxt. entity. user; import org. springframework. data. domain. page; import org. springframework. data. domain. pageable; import org. springframework. data. jpa. repository. jpaRepository; import org. springframework. data. jpa. repository. query; import org. springframework. data. repository. query. param; import java. util. list;/*** Created by lp on. */public interface UserRepos Itory extends UserDao, JpaRepository <User, Integer> {/* naming query constraint definition * // accurately queries public List <User> findByUserName (String userName) based on Attribute fields ); // fuzzy match public List <User> findByUserNameLike (String userName) based on Attribute fields; // multiple criteria match public List <User> findByUserNameAndUserPwd (String userName, String userPwd ); // returns the Page Object Page size. The Page size is fuzzy matched based on the User name. The User record Page size is set to public Page. <User> findByUserNameLike (String userName, Pageable pageable); // Return the public List of paging List data <User> findByUserPwdLike (String userPwd, Pageable pageable);/* query parameter passing 01: placeholder Shanghai shangxuexiang java Jiawei java8733 get more related technical information */@ Query ("select u from User u where u. userName like %? 1% ") public List <User> UserByUserName (String userName); @ Query (" select u from User u where u. userName =? 1 and u. userPwd =? 2 ") public List <User> loadUserByUserNameAndUserPwd (String userName, String userPwd);/*** parameter 02: name parameter * @ param userName * @ param userPwd * @ return */@ Query ("select u from User u where u. userName like: userName and u. userPwd =: userPwd ") public List <User> loadUserByUserNameAndUserPwd02 (@ Param (" userName ") String userName, @ Param (" userPwd ") String userPwd ); /*** Add a fuzzy match flag to the placeholder */@ Query ("select u from Use R u where u. userName like %: userName % ") public List <User> loadUserByUserName02 (@ Param (" userName ") String userName, Pageable pageable ); /*** local SQL Query supports */@ Query (value = "select * from t_user u where u. user_name like %? 1% ", nativeQuery = true) public List <User> loadUserByUserName03 (String userName );}

  

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.