spring Data Jpa:
Spring Data JPA:Spring Data JPA 是 spring data 项目下的一个模块。提供了一套基于 JPA标准操作数据库的简化方案。底层默认的是依赖 Hibernate JPA 来实现的。
Spring Data JPA 的技术特点:我们只需要定义接口并集成 Spring Data JPA 中所提供的接
口就可以了。不需要编写接口实现类。
spring data jpa的接口继承结构:
由图可以知道接口PagingAndSortingRepository的接口中提供分页和排序
自定义接口继承PagingAndSortingRepository
import com.bjsxt.pojo.Users;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface UsersDao extends PagingAndSortingRepository<Users,Integer> {//自定义接口
}
spring data jpa的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--1.配置读取properties文件的工具类-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!--2.配置c3p0连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="driverClass" value="${jdbc.driver.class}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--3.配置hibernate的sessionfactory-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" >
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL"/>
<property name="generateDdl" value="true"/><!--正向工程-->
<property name="showSql" value="true"/>
</bean>
</property>
<!--扫描实体包-->
<property name="packagesToScan">
<list>
<value>com.bjsxt.pojo</value>
</list>
</property>
</bean>
<!--配置hibernate的事务管理-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!--开启注解事务处理-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--开启springioc的注解扫描-->
<context:component-scan base-package="com.bjsxt"></context:component-scan>
<!--springdata的配置-->
<!--扫描dao所在的接口-->
<jpa:repositories base-package="com.bjsxt.dao"/>
</beans>
连接数据库的配置信息:
jdbc.url=jdbc:mysql://localhost:3306/test jdbc.driver.class=com.mysql.jdbc.Driver jdbc.user=root jdbc.password=root
实体类:
package com.bjsxt.pojo;
import javax.persistence.*;
@Entity
@Table(name = "t_users")
public class Users {
@Id//主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//主键自增
@Column(name = "userId")
private Integer userId;
@Column(name = "name")
private String name;
@Column(name = "gender")
private String gender;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Users{" +
"userId=" + userId +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
'}';
}
}
实现类:
package com.bjsxt.test;
import com.bjsxt.dao.UsersDao;
import com.bjsxt.pojo.Users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class UserDaoImplTest {
@Autowired
private UsersDao usersDao;
/**
* 分页
*/
@Test
public void testPage(){
int page=1;//当前页的索引,默认是0开始的,不是1
int size=3;//每页显示的数据
Pageable pageable=new PageRequest(page,size);
Page<Users> pa = this.usersDao.findAll(pageable);
System.out.println("总数目:"+ pa.getTotalElements());
System.out.println("共"+pa.getTotalPages()+"页");
List<Users> content = pa.getContent();
for (Users user:
content) {
System.out.println(user);
}
}
/**
* 排序
*/
@Test
public void sortUser(){
//多列排序
//sort类封装排序规则以及指定的排序字段
//direction 排序规则
//properties:指定的做排序的属性
Sort.Order order=new Sort.Order(Sort.Direction.DESC,"gender");
Sort.Order order1=new Sort.Order(Sort.Direction.DESC,"userId");
Sort sort=new Sort(order,order1);
List<Users> list= (List<Users>) this.usersDao.findAll(sort);
for (Users user:
list) {
System.out.println(user);
}
}
}