Spring data jpa Configuration

Source: Internet
Author: User

Programming at ordinary times, in addition to writing pages, it is also annoying to repeatedly write some SQL statements in dao. Recently, it was found that spring data jpa is very useful, it can minimize the number of times we write SQL statements. Some simple SQL statements can be automatically parsed into SQL statements based on the Interface Name, which greatly saves programming time, so that we can spend more time on the most important business logic. The key point is that it also helps us implement the paging query method, which is very easy to use;

For example, if we write a public void findByName (string name) in the dao excuse, it will automatically resolve it to select * from tableName where name = name; so that we do not need to close SQL;

In addition to the springMVC + hibernate + spring framework, you also need to add the jar package:

 
  
   org.hibernate.javax.persistence
  hibernate-jpa-2.0-api
  
   1.0.1.Final
  
 
 
  
   org.springframework.data
  spring-data-jpa
  
   1.0.3.RELEASE
  
 

Remember that it is best to use the 3.6.10.Final version for hibernate and the 3.1.1.RELEASE version for spring. An error has been reported since the version is earlier and it takes several days to locate the problem.

Then modify sessionFactory in applicationContext. xml:

 
  
  
  
   
    
    
    
    
   
  
 

You can use entityManagerFactory as sessionFactory during transaction configuration.

Then write the dao Interface

@Repositorypublic interface UserDao extends PagingAndSortingRepository
 
   {public Test findByName(String name);}
 

Then call the dao method:

@RequestMapping("chen")public void chen(int pageNo, int pageSize) {Page
 
   page = userDao.findAll(new PageRequest(pageNo - 1, pageSize));}
 

It encapsulates some common methods;


You need to write the SQL statement yourself when modifying and deleting the SQL statement, but it is not much written (note the annotation)

@Modifying@Query("update AccountInfo a set a.salary = ?1 where a.salary < ?2")public int increaseSalary(int after, int before);




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.