Spring Data JPA (1)--repository and Crudrepository interfaces

Source: Internet
Author: User
Tags findone iterable
The recent knowledge of spring Data JPA has been very good, providing a number of methods, including crud and paging, to meet the functional requirements of reality.

Altogether it provides four interfaces:

Repository: Just an identity, indicating that any inherited it is a warehouse interface class, convenient spring automatic scanning recognition
Crudrepository: Inheriting repository, implementing a set of CRUD-related methods
Pagingandsortingrepository: Inherit crudrepository, implement a component page sort related method
Jparepository: Inheriting pagingandsortingrepository, implementing a set of JPA specification-related methods
Jpaspecificationexecutor: A relatively special, non-repository system to implement a set of JPA criteria query-related methods

I wrote the example:

/* * @RepositoryDefinition (DomainClass = User.class,idclass = integer.class)//Use this note to replace extends Repository */public inte Rface Userrepository extends Crudrepository<user, integer>{/* Repository interface, he is an empty interface, just an identity *///HQL operation is an object, and The SQL operation is a table, so here user refers to the class//can directly use some of the methods provided by repository, but the method must be named to follow certain rules, such as Findbyxx,findbyxxlike,findbyxxstartingwith
	The. Public User findbyuname (String uname);
	Use @Query annotations to solve the hassle of defining rules and implement complex queries, with more @Query in work ("Select O from User o") public list<user> showusers ();
	@Query ("Select O from user o where O.uno =? 1 and o.uname =? 2") Public User finduser (int uno,string uname); With @modifying annotations and transactions, you can complete the update and delete operations, JPQL does not support the increased Operation @Modifying @Query ("Delete from User where Uno=:uno") the public void Deluser (
	@Param ("uno") int uno); @Modifying @Query ("Update User o set o.uname =: uname where Uno =: Uno") public void Upduser (@Param ("uno") int Uno, @Param ("uname")	
	String uname); /* * Crudrepository interface Inherits Repository, provides a set of crud-related methods to test the FindOne method directly in the test class */}
The Crudrepository Interface source code provides the method:
<s extends t> s save (s entity);
<s extends t> iterable<s> Save (iterable<s> entities);
T FindOne (ID ID);
Boolean exists (ID ID);
Iterable<t> findAll ();
Iterable<t> FindAll (iterable<id> IDs);
Long count ();
VOID Delete (ID ID);
void Delete (T entity);
void Delete (iterable<? extends t> entities);
void DeleteAll ();
Take the FindOne () method as an example to test in a unit test
public class Userrepositorytest {
	private applicationcontext CTX = null;
	Private userrepository userrepository = null;
	@Before public
	void SetUp () {
		CTX = new Classpathxmlapplicationcontext ("Beans.xml");
		Userrepository = Ctx.getbean (userrepository.class);
		SYSTEM.OUT.PRINTLN ("Setup");
	}
	@After public
	void TearDown () {
		ctx = null;
		System.out.println ("teardown");
	}
	@Test public
	void TestQuery3 () {
		System.out.println ("start");
		User user = Userrepository.findone (1);
		System.out.println (User.tostring ());
		System.out.println ("Started");
	}
}





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.