First, use
1, the opening is defined in a dao method, a method is provided JPQL parameters, return value and using the result to accept the query, add annotations on @query method, written in a comment sentence for JPQL CRUD test
2, using the native sql statement: dao define a method for adding annotation @query process, adding native sql statement in a comment, and add an attribute: nativeQuery = true, the test
3. The method of naming the query:
By certainly rules define a method, a frame itself can generate a sql query statement according to the method name, rules:
1, must begin with findBy
2, query a field attribute name is followed by the entity class findBy
3, if there are a plurality of conditions is the method of adding the attribute name followed by the entity class And
4, corresponding to the parameters of the method defined query
5, the return value data type defined return value; if pagination query, add a parameter in the method to Pageable
Two ,, write an implementation class
package cn.zrf.jpa.entity; import javax.persistence.*; @Entity @Table(name = "cust_customer") public class Customer { // 配置主键自增的策略 @GeneratedValue(strategy = GenerationType.IDENTITY) @Id @Column(name="cust_id") private long custId; @Column(name="cust_name") private String custName; @Column(name="cust_source") private String custSource; @Column(name="cust_indutry") private String custIndutry; @Column(name="cust_level") private String custLevel; @Column(name="cust_address") private String custAddress; @Column(name="cust_phone") private String custPhone; @Override public String toString() { return "Customer{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custIndutry='" + custIndutry + '\'' + ", custLevel='" + custLevel + '\'' + ", custAddress='" + custAddress + '\'' + ", custPhone='" + custPhone + '\'' + '}'; } public long getCustId() { return custId; } public void setCustId(long custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustIndutry() { return custIndutry; } public void setCustIndutry(String custIndutry) { this.custIndutry = custIndutry; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } }
Third, write dao
package cn.zrf.jpa.dao; import cn.zrf.jpa.entity.Customer; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import java.util.List; public interface CustomerDao extends JpaRepository<Customer,Long>,JpaSpecificationExecutor<Customer> { //查询全部 @Query("from Customer") List<Customer> getAllCustomer(); //查询全部并分页 @Query("from Customer ") List<Customer>getAllCustomerByPage (Page Portable page loveable); // conditional query (query by ID) @Query ( "from the Customer the WHERE cust_id =?") The Customer getCustomerById (Long the above mentioned id); // fuzzy queries based on address, name @Query ( "from Customer where custAddress like and custName like?? ") List <the Customer> getCustList (String address, String custName); // update operation in accordance with local ID @Query ("?? = CUST_NAME update the Customer WHERE custId = SET ") @Modifying void getUpdateById (String name, ID Long) ; // native sql statement fuzzy query @Query (value =, = nativeQuery to true "from the SELECT * cust_customer the WHERE CUST_NAME like?") List <the Customer> getCustomerListByNative (String name);// method naming query / ** 1 should be used at the beginning of findBy * 2 query attribute name followed by a field findBy entity class * 3 If multiple conditions are incremented And + attribute named entity class in the method defined * Parameter 4 corresponding method queries * 5 returns the value for the data type definition returned * 6 if desired paging query to add a parameter to the method Pageable to * / // ID query based on the Customer findByCustId (Long the above mentioned id); // fuzzy query based on name, address List <the Customer> findByCustNameLikeAndCustAddressLike (String name, String address); // paging query List <Customer> findByCustAddressLike (String address , the Pageable Pageable); }
Fourth, the test class
package cn.jpa.test; import cn.zrf.jpa.dao.CustomerDao; import cn.zrf.jpa.entity.Customer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.test.annotation.Commit; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import java.awt.print.Pageable; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class TestJpql { @Autowired CustomerDao customerDao; //查询全部 @Test public void getAllCustomerTest(){ List<Customer> list = customerDao.getAllCustomer(); for (Customer customer:list){ System.out.println(customer); } } //查询全部并分页 @Test public void getAllCustomerByPageTest(){ List<Customer> list = customerDao.getAllCustomerByPage(new PageRequest(0, 3)); for (Customer customer:list){ System.out.println(customer); } } //根据ID查询 @Test public void getCustomerByIdTest(){ Customer = customerDao.getCustomerById the Customer (1L); System.out.println (Customer); } // fuzzy query the address, name @Test public void getCuseListTest () { List <the Customer> CustList = customerDao.getCustList ( "% Jing % ","% long% "); for (Customer the Customer: CustList) { System.out.println (Customer); } } // update operation in accordance with local ID @Test @Transactional @Commit public void getUpdateByIdTest () { customerDao .getUpdateById ( "Zhang Wuji", 3L); } // native sql statement Fuzzy queries @Test public void getCustomerByNativeTest () { List <Customer> list = customerDao.getCustomerListByNative ( "% Long%"); for (Customer the Customer:list){ System.out.println (Customer); } } // test method naming rule query // The ID query @Test public void findByIdTest () { the Customer Customer customerDao.findByCustId = (1L); System.out.println (Customer); } // fuzzy query the address, name @Test public void findByCustNameLikeAndAddressLike () { List <the Customer> = customerDao.findByCustNameLikeAndCustAddressLike List ( "Zhang%%" "Beijing%%"); for (Customer the Customer: List) { System.out.println (Customer); } } // query based paging address @Test public void findByCustAddressLike () { List<Customer> list = customerDao.findByCustAddressLike("%京%", new PageRequest(0, 3)); for (Customer customer:list){ System.out.println(customer); } } }