SpringData 第二章:继承PagingAndSortingRepository进行分页开发

一、pom.xml配置


		
			mysql
			mysql-connector-java
			5.1.38
		
		
		
			org.springframework.data
			spring-data-jpa
			1.11.6.RELEASE
		
		
		
			org.hibernate
			hibernate-entitymanager
			5.2.9.Final
		
		
		
			com.alibaba
			druid
			1.0.12
		

二、SpringData.xml配置


        
        
		
		 
		
			
			
			
			
		
		
		
			
			
			
			
				
			
			
			
			
			
				
					org.hibernate.cfg.ImprovedNamingStrategy
					org.hibernate.dialect.MySQL5InnoDBDialect
					true
					true
					update
				
			
		
		
		
			
		
		
		
		
		
		

三、实体类

package com.mingde.po;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Student {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int sid;
	private String sname;
	private String sex;
	private int age;
	private String addr;


四、Dao层定义

package com.mingde.dao;

import org.springframework.data.repository.PagingAndSortingRepository;

import com.mingde.po.Student;

//继承该接口即可,若有自定义的方法就写上去
//继承该接口,可以使用该接口里面内定的方法,可及继承该接口的父接口CrudRepository,一般用这个子接口,因为子接口拥有父类的所有非私有的方法
public interface StudentDao extends PagingAndSortingRepository {
	//这里可以写自定义的方法,因为该接口没有修改的方法,所以需要自己手动写
	@Modifying
	@Query("update Student st set st.sname=:sname,st.saddr=:saddr where st.sid=:sid")
	public void update(@Param("sname")String sname,
					   @Param("saddr")String saddr,
					   @Param("sid")int sid) throws Exception;
	
}

PagingAndSortingRepository接口的2个方法



PagingAndSortingRepository接口继承CrudRepository接口




CrudRepository接口中的方法

SpringData 第二章:继承PagingAndSortingRepository进行分页开发_第1张图片

所以继承接口PagingAndSortingRepository就可以用以上全部的方法


五、Service层定义

package com.mingde.servier;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.mingde.dao.StudentDao;
import com.mingde.po.Student;

@Service
public class StudentServier  {

	@Resource(name="studentDao")
	StudentDao sd;
	
	@Autowired
	private StudentDao studentDao;
	
	//分页查询
	public Page findAll(Pageable pageable) throws Exception{
		return studentDao.findAll(pageable);
	}
	
	//查询所有学生信息
	public List findAll()throws Exception{
		return (List) sd.findAll();
	}
	//根据id编号查询学生信息
	public Student findOne(int id)throws Exception{
		return sd.findOne(id);
	}
	//添加学生信息
	public void save(Student st)throws Exception{
		sd.save(st);
	}
	//根据id删除学生信息
	public void delete(int id)throws Exception{
		sd.delete(id);
	}
	//根据id判断该学生是否存在
	public boolean exists(int id)throws Exception{
		return sd.exists(id);
	}
	
}


六、测试类

package com.mingde.test;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import com.mingde.po.Student;
import com.mingde.servier.StudentServier;

public class StudentTest {

	private ApplicationContext ac;
	private StudentServier ss;
	@Before
	public void tearDown() throws Exception {
		ac=new ClassPathXmlApplicationContext("SpringData.xml");
		ss=ac.getBean(StudentServier.class);
	}

	@Test
	public void test() throws Exception {
		/*
		//查询所有学生信息
		List findAll = ss.findAll();
		System.out.println(findAll);
		
		//根据学生id查询学生信息
		Student findOne = ss.findOne(1002);
		System.out.println(findOne);
		
		//添加学生信息
		Student st=new Student("史迪奇", "男", 10, "迪尼斯");
		ss.save(st);
		System.out.println("添加成功");
		
		//删除指定id学生信息
		ss.delete(1004);
		System.out.println("删除成功");
		
		//根据id判断该学生是否存在
		boolean exists = ss.exists(1003);
		System.out.println("该学生存在?:"+exists);*/
		
	}
	//分页效果
	@Test
	public void tes2t() throws Exception {
		Pageable pageable = new PageRequest(1,3);
		Page stud = ss.findAll(pageable);
		//下面查看分页的一系列数据
		int page = stud.getNumber();
		System.out.println("当前页码:" + (page + 1));
		int records = stud.getNumberOfElements();
		System.out.println("当前页记录数:" + records);
		int size = stud.getSize();
		System.out.println("每一页的记录数:" + size);
		long totalRecords = stud.getTotalElements();
		System.out.println("总记录数:" + totalRecords);
		int totalPages = stud.getTotalPages();
		System.out.println("总页数:" + totalPages);
		List list = stud.getContent();
		System.out.println("当前页的数据:" + list);
	}
	
}

你可能感兴趣的:(SpringData)