SpringData 第二章:继承PagingAndSortingRepository进行分页开发
来源:互联网 发布:linux目录树结构图 编辑:程序博客网 时间:2024/05/21 08:30
一、pom.xml配置
<!--Mysql 驱动包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.38</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId><version>1.11.6.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>5.2.9.Final</version></dependency><!-- 添加阿里巴巴数据源支持 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.12</version></dependency>
二、SpringData.xml配置
<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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 扫描包 --><context:component-scan base-package="com.mingde" /> <!-- 定义数据源(阿里巴巴得鲁伊) com.alibaba.druid.pool.DruidDataSource" --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" ><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql:///SpringData" /><property name="username" value="root" /><property name="password" value="123" /></bean><!-- 配置实体管理工厂 定义entityManagerFactory --><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><!-- 定义实体扫描器 --><property name="packagesToScan" value="com.mingde" /><!-- 定义一系列的JPA属性 --><property name="jpaProperties"><props><prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop><prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property></bean><!-- 定义是我管理器 --><bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"><property name="entityManagerFactory" ref="entityManagerFactory" ></property></bean><!-- 定义注解事务驱动 --><tx:annotation-driven transaction-manager="transactionManager" /><!-- 定义jpa:repositories --><jpa:repositories base-package="com.mingde" entity-manager-factory-ref="entityManagerFactory"></jpa:repositories></beans>
三、实体类
package com.mingde.po;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entitypublic 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<Student, Integer> {//这里可以写自定义的方法,因为该接口没有修改的方法,所以需要自己手动写
@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接口中的方法
所以继承接口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;@Servicepublic class StudentServier {@Resource(name="studentDao")StudentDao sd;@Autowiredprivate StudentDao studentDao;//分页查询public Page<Student> findAll(Pageable pageable) throws Exception{return studentDao.findAll(pageable);}//查询所有学生信息public List<Student> findAll()throws Exception{return (List<Student>) 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;@Beforepublic void tearDown() throws Exception {ac=new ClassPathXmlApplicationContext("SpringData.xml");ss=ac.getBean(StudentServier.class);}@Testpublic void test() throws Exception {/*//查询所有学生信息List<Student> 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);*/}//分页效果@Testpublic void tes2t() throws Exception {Pageable pageable = new PageRequest(1,3);Page<Student> 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<Student> list = stud.getContent();System.out.println("当前页的数据:" + list);}}
阅读全文
0 0
- SpringData 第二章:继承PagingAndSortingRepository进行分页开发
- SpringData中的PagingAndSortingRepository接口
- pagingandsortingrepository接口实现分页查询
- PagingAndSortingRepository
- PagingAndSortingRepository
- SpringData JPA 分页使用
- SpringData JPA分页查询
- SpringData MongoDB repository 分页
- jpa之PagingAndSortingRepository带分页查询
- jpa之PagingAndSortingRepository带分页查询
- SpringData 学习(7)—— 学习 Repository 接口的第 2 个子接口 PagingAndSortingRepository
- SpringData JPA查询分页demo
- SpringData JPA查询分页demo
- SpringData开发步骤
- SpringBoot+SpringData整合开发
- 第二章_继承
- 第二章继承
- 第二章 继承
- 认识syslog
- 木乃伊迷宫
- 【目标检测】RCNN 算法详解(上)
- 第十二周 项目(1)
- slam笔记
- SpringData 第二章:继承PagingAndSortingRepository进行分页开发
- 高中三年奋斗史
- java系统高并发解决方案
- 利用RMAN备份压缩技术对数据库做全备并备份控制文件和归档日志后删除已备份的归档日志
- POJ2253 -最短路-
- MFC编程文档与视图--学习笔记
- CodeForces
- es6新语法
- JIT编译