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);