spring data jpa
day1: Orm thought, hibernate and jpa overview and jpa basic operation
day2: springdatajpa operation principle
day2: basic operation of springdatajpa
day3: multi-table operation, complex query
day2: The operating principle and basic operation of springdatajpa (below)
environment The setup and the previous code are in: day2: the operating principle of springdatajpa
The fourth complex query
i. Complete the query with the help of the defined method in the interface
findOne(id): query by id
ii.jpql query method
jpql: jpa query language (jpq query language)
Features: grammar or keywords are similar to sql statement
The query is the class and the attributes in the class
* Need to configure the JPQL statement to the interface method
1. Unique query: need to configure method on dao interface
2. On the newly added method, configure the jpql query statement in the form of annotations
3. Comment: @Query
iii.sql statement query
1. Unique query: need to configure method on dao interface
2. On the newly added method, configure the sql query statement in the form of annotations
3. Comment: @Query
value: jpql statement | sql statement
nativeQuery:false (using jpql query) | true (Use local query: sql query)
Whether to use local query
iiii. Method name rule query
@RunWith(SpringJUnit4ClassRunner.class) //declare the unit test environment provided by spring
@ContextConfiguration(locations = "classpath:applicationContext.xml")//specify spring container Configuration information
public class CustomerDaoTest {
@Autowired
private CustomerDao customerDao;
19-spring Data JPA query: call interface method query (count, exists)
/**
* Test statistics query: query the total number of customers
* count: count the total number of items
*/
@Test
public void testCount() {
long count = customerDao.count();//Query the total number of customers
System.out.println(count);
}
/**
* Test: Determine whether the customer with id 4 exists
* 1. You can query the following customers whose id is 4
* If the value is empty, it means it does not exist, if it is not empty, it means it exists
* 2. Determine the number of customers with id 4 in the database
* If the number is 0, it means it does not exist, if it is greater than 0, it means it exists
*/
@Test
public void testExists() {
boolean exists = customerDao.exists(4l);
System.out.println("Does the customer with id 4 exist:"+exists);
}
20-spring Data JPA query: call interface method query (the difference between findOne and getOne)
< div class="code">
/**
* Query from database according to id
* @Transactional: Ensure the normal operation of getOne
*
* findOne:
* em.find(): Load now
* getOne:
* em.getReference: Lazy loading
* * What is returned is a dynamic proxy object of a client
* * When to use and when to inquire
*/
@Test
@Transactional
public void testGetOne() {
Customer customer = customerDao.getOne(2l);
System.out.println(customer);
}
Chapter 4 Spring Data JPA query method
4.1 Query using the methods defined in the interface in Spring Data JPA
After inheriting the JpaRepository and JpaRepository interfaces, we can use the methods defined in the interface to query
< /strong>
4.2 Query using JPQL
21 -Spring Data JPA query: JPQL query introduction
Using the query method provided by Spring Data JPA can solve most of the application scenarios, but for some businesses, we also need flexible construction Query conditions, then you can use @Query annotations, combined with JPQL statements to complete the query.
ii.jpql query method
jpql: jpa query language (jpq query language)
Features: grammar or keywords are similar to sql statement
The query is the class and the attributes in the class
* Need to configure the JPQL statement to the interface method
1. Unique query: need to configure method on dao interface
2. On the newly added method, configure the jpql query statement in the form of annotations
3. Comment: @Query
22-jpql: Use jpql to complete basic query
package cn.bjut.dao;
import cn.bjut.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
/**
* Conforms to the dao layer interface specification of SpringDataJpa
* JpaRepository
* * Basic CRUD operations are encapsulated
* JpaSpecificationExecutor
* * Encapsulates complex queries (pagination)
*/
public interface CustomerDao extends JpaRepository, JpaSpecificationExecutor {
/**
* Case: Query customers based on their names
* Use jpql to query
* jpql: from Customer where custName =?
*
* Configure jpql statement, use @Query annotation
*/ @Query(value="from Customer where custName = ?")
public Customer findJpql(String custName);
}
Create a new test class
JpqlTest
@RunWith(SpringJUnit4ClassRunner.class) //Declare the unit test environment provided by spring
@ContextConfiguration(locations = "classpath:applicationContext.xml")//specify spring container Configuration information
public class JpqlTest {
@Autowired
private CustomerDao customerDao;
23-jpql: Assignment of multiple placeholders
/**
* Case: Query customers based on customer name and customer id
* jpql: from Customer where custName =? and custId =?
*
* For multiple placeholder parameters
* When assigning values, by default, the position of the placeholder needs to be consistent with the position in the method parameter
*
* You can specify the position of the placeholder parameter
*? Index method, specify the value source of this placeholder
*/
@Query(value = "from Customer where custName = ?2 and custId = ?1")
public Customer findCustNameAndId(Long id,String name);
24-jpql: Use jpql to complete the update operation
In addition, you can also use @Query to perform an update operation. For this, we need to use @Query while using < span style="color: #ff0000;"> @Modifying to identify the operation as a modification query, so that the framework will eventually generate an update operation instead of a query.
/**
* Use jpql to complete the update operation
* Case: Update according to id, customer's name
* Update the name of customer No. 3 and change the name to "Junior Programmer"
*
* sql: update cst_customer set cust_name =? where cust_id =?
* jpql: update Customer set custName =? where custId =?
*
* @Query: means to query
* * Declare that this method is used for update operations
* @Modifying
* * Currently performing an update operation
*
*/
@Query(value = "update Customer set custName = ?2 where custId = ?1 ")
@Modifying
public void updateCustomer(long custId,String custName);
springDataJpa uses jpql to complete the update/delete operation
Need to manually add transaction support
By default, the transaction will be rolled back after the execution ends.
/**
* Test the update operation of jpql
* * Use jpql to complete the update/delete operation in springDataJpa
* * Need to manually add transaction support
* * By default, the transaction will be rolled back after the execution is over
* @Rollback: Set whether to automatically roll back
* false | true
*/
@Test
@Transactional //Add transaction support
@Rollback(value = false)
public void testUpdateCustomer() {
customerDao.updateCustomer(3l,"Junior Programmer");
}
========================
end
The fourth complex query
i. Complete the query with the help of the defined method in the interface
findOne(id): query by id
ii.jpql query method
jpql: jpa query language (jpq query language)
Features: grammar or keywords are similar to sql statement
The query is the class and the attributes in the class
* Need to configure the JPQL statement to the interface method
1. Unique query: need to configure method on dao interface
2. On the newly added method, configure the jpql query statement in the form of annotations
3. Comment: @Query
iii.sql statement query
1. Unique query: need to configure method on dao interface
2. On the newly added method, configure the sql query statement in the form of annotations
3. Comment: @Query
value: jpql statement | sql statement
nativeQuery:false (using jpql query) | true (Use local query: sql query)
Whether to use local query
iiii. Method name rule query
@RunWith(SpringJUnit4ClassRunner.class ) //Declare the unit test environment provided by spring
@ContextConfiguration(locations = "classpath:applicationContext.xml")//specify spring container Configuration information
public class CustomerDaoTest {
@Autowired
private CustomerDao customerDao;
/**
* Test statistics query: query the total number of customers
* count: count the total number of items
*/
@Test
public void testCount() {
long count = customerDao.count();//Query the total number of customers
System.out.println(count);
}
/**
* Test: Determine whether the customer with id 4 exists
* 1. You can query the following customers whose id is 4
* If the value is empty, it means it does not exist, if it is not empty, it means it exists
* 2. Determine the number of customers with id 4 in the database
* If the number is 0, it means it does not exist, if it is greater than 0, it means it exists
*/
@Test
public void testExists() {
boolean exists = customerDao.exists(4l);
System.out.println("Does the customer with id 4 exist:"+exists);
}
/**
* Query from database according to id
* @Transactional: Ensure the normal operation of getOne
*
* findOne:
* em.find(): Load now
* getOne:
* em.getReference: Lazy loading
* * What is returned is a dynamic proxy object of a client
* * When to use and when to inquire
*/
@Test
@Transactional
public void testGetOne() {
Customer customer = customerDao.getOne(2l);
System.out.println(customer);
}
ii.jpql query method
jpql: jpa query language (jpq query language)
Features: grammar or keywords are similar to sql statement
The query is the class and the attributes in the class
* Need to configure the JPQL statement to the interface method
1. Unique query: need to configure method on dao interface
2. On the newly added method, configure the jpql query statement in the form of annotations
3. Comments: @Query
package cn.bjut.dao;
import cn.bjut.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
/**
* Conforms to the dao layer interface specification of SpringDataJpa
* JpaRepository
* * Basic CRUD operations are encapsulated
* JpaSpecificationExecutor
* * Encapsulates complex queries (pagination)
*/
public interface CustomerDao extends JpaRepository, JpaSpecificationExecutor {
/**
* Case: Query customers based on their names
* Use jpql to query
* jpql: from Customer where custName =?
*
* Configure jpql statement, use @Query annotation
*/ @Query(value="from Customer where custName = ?")
public Customer findJpql(String custName);
}
@RunWith(SpringJUnit4ClassRunner.class) //Declare the unit test environment provided by spring
@ContextConfiguration(locations = "classpath:applicationContext.xml")//specify spring container Configuration information
public class JpqlTest {
@Autowired
private CustomerDao customerDao;
/**
* Case: Query customers based on customer name and customer id
* jpql: from Customer where custName =? and custId =?
*
* For multiple placeholder parameters
* When assigning values, by default, the position of the placeholder needs to be consistent with the position in the method parameter
*
* You can specify the position of the placeholder parameter
*? Index method, specify the value source of this placeholder
*/
@Query(value = "from Customer where custName = ?2 and custId = ?1")
public Customer findCustNameAndId(Long id,String name);
< span style="color: #008000;">/**
* Use jpql to complete the update operation
* Case: Update according to id, customer's name
* Update the name of customer No. 3 and change the name to "Junior Programmer"
*
* sql: update cst_customer set cust_name =? where cust_id =?
* jpql: update Customer set custName =? where custId =?
*
* @Query: means to query
* * Declare that this method is used for update operations
* @Modifying
* * Currently performing an update operation
*
*/
@Query(value = "update Customer set custName = ?2 where custId = ?1 ")
@Modifying
public void updateCustomer(long custId,String custName);
/**
* Test the update operation of jpql
* * Use jpql to complete the update/delete operation in springDataJpa
* * Need to manually add transaction support
* * By default, the transaction will be rolled back after the execution is over
* @Rollback: Set whether to automatically roll back
* false | true
*/
@Test
@Transactional //Add transaction support
@Rollback(value = false)
public void testUpdateCustomer() {
customerDao.updateCustomer(3l,"Junior Programmer");
}