Jpa实现数据库的CRUD
Posted wangshichang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jpa实现数据库的CRUD相关的知识,希望对你有一定的参考价值。
1....创建步骤:练习使用
1... 创建 maven工程
2...添加依赖 mysql c3p0 hibernate springDataJpa spring相关的包
3...创建 配置文件 员(数据源)工(工程类对象)是(事物)爹(dao 扫描器)
4... 创建实体类
5... 创建dao 接口 继承 JapRespository
2....五种查询方式
(1) crud
添加/修改 数据 save 方法
? 删除数据 delete 方法
? 查询 :findOne. getOne 方法
? 查询全部 findAll() 方法
? findAll(Pageable pageaable) 返回Page对象 进行分页 排序
(2) 在springDataJpa框架使用Jpql 查询
a: 在dao 接口中定义一个方法 使用方法的参数设置jpql的参数 并且使用方法的返回值接受查询的结果
? b: 在方法上添加一个注解Qquery
? c 在注解中编写jpql
? d 测试
(3)使用原生的Sql语句
1 dao中 定义一个方法
? 2 在方法中添加@Query注解
? 3 在注解中 添加 原生的sql 语句 添加一个属性 nativeQuery=true
? 4 测试
(4)方法命名规则查询
通过一定的规则 定义一个方法 框架本省就可以根据方法名生成一个SQL语句进行查询
? 规则:
? 1 使用findBy开头
? 2 查询某个字段 findBy后跟实体类的属性的名称
? 3 如果有多个条件 就在方法后加And+实体类的属性名
? 4 方法的参数 对应查询的定义
? 5 返回值根据返回的数据类型定义
如果需要分页查询 在方法中添加一个参数Pageable 即可
(5)使用Specification方式进行查询
最强大的查询方式 除了原生的SQL 语句以外 最复杂的查询方式
使用方式:
? 1 要在dao 继承JpaSeciFicationExection 接口
? 2 使用JpaSeciFicationExection 接口 中提供的方法进行查询
? 每个方法都需要使用Specification对象作为参数
具体实现:::
1...pom.xml 配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.wsc</groupId> 8 <artifactId>springdatalpa01</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 <maven.compiler.source>1.8</maven.compiler.source> 15 <maven.compiler.target>1.8</maven.compiler.target> 16 <spring.version>4.2.4.RELEASE</spring.version> 17 <hibernate.version>5.0.7.Final</hibernate.version> 18 <slf4j.version>1.6.6</slf4j.version> 19 <log4j.version>1.2.12</log4j.version> 20 <c3p0.version>0.9.1.2</c3p0.version> 21 <mysql.version>5.1.6</mysql.version> 22 </properties> 23 <dependencies> 24 <dependency> 25 <groupId>junit</groupId> 26 <artifactId>junit</artifactId> 27 <version>4.12</version> 28 <scope>test</scope> 29 </dependency> 30 <!--spring start--> 31 <dependency> 32 <groupId>org.aspectj</groupId> 33 <artifactId>aspectjweaver</artifactId> 34 <version>1.6.8</version> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework</groupId> 38 <artifactId>spring-aop</artifactId> 39 <version>$spring.version</version> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework</groupId> 43 <artifactId>spring-context</artifactId> 44 <version>$spring.version</version> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-context-support</artifactId> 49 <version>$spring.version</version> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-orm</artifactId> 54 <version>$spring.version</version> 55 </dependency> 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-beans</artifactId> 59 <version>$spring.version</version> 60 </dependency> 61 <dependency> 62 <groupId>org.springframework</groupId> 63 <artifactId>spring-core</artifactId> 64 <version>$spring.version</version> 65 </dependency> 66 <!--spring end--> 67 <!--hibernate start--> 68 <dependency> 69 <groupId>org.hibernate</groupId> 70 <artifactId>hibernate-core</artifactId> 71 <version>$hibernate.version</version> 72 </dependency> 73 <dependency> 74 <groupId>org.hibernate</groupId> 75 <artifactId>hibernate-entitymanager</artifactId> 76 <version>$hibernate.version</version> 77 </dependency> 78 <dependency> 79 <groupId>org.hibernate</groupId> 80 <artifactId>hibernate-validator</artifactId> 81 <version>5.2.1.Final</version> 82 </dependency> 83 <!--hibernate end--> 84 <dependency> 85 <groupId>c3p0</groupId> 86 <artifactId>c3p0</artifactId> 87 <version>$c3p0.version</version> 88 </dependency> 89 <dependency> 90 <groupId>log4j</groupId> 91 <artifactId>log4j</artifactId> 92 <version>$log4j.version</version> 93 </dependency> 94 <dependency> 95 <groupId>org.slf4j</groupId> 96 <artifactId>slf4j-api</artifactId> 97 <version>$slf4j.version</version> 98 </dependency> 99 <dependency> 100 <groupId>org.slf4j</groupId> 101 <artifactId>slf4j-log4j12</artifactId> 102 <version>$slf4j.version</version> 103 </dependency> 104 <dependency> 105 <groupId>mysql</groupId> 106 <artifactId>mysql-connector-java</artifactId> 107 <version>$mysql.version</version> 108 </dependency> 109 <dependency> 110 <groupId>org.springframework.data</groupId> 111 <artifactId>spring-data-jpa</artifactId> 112 <version>1.9.0.RELEASE</version> 113 </dependency> 114 <dependency> 115 <groupId>org.springframework</groupId> 116 <artifactId>spring-test</artifactId> 117 <version>4.2.4.RELEASE</version> 118 </dependency> 119 <dependency> 120 <groupId>javax.el</groupId> 121 <artifactId>javax.el-api</artifactId> 122 <version>2.2.4</version> 123 </dependency> 124 <dependency> 125 <groupId>org.glassfish.web</groupId> 126 <artifactId>javax.el</artifactId> 127 <version>2.2.4</version> 128 </dependency> 129 </dependencies> 130 </project>
2...applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 13 http://www.springframework.org/schema/data/jpa 14 http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> 15 <!--1 dataSource--> 16 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 17 <property name="password" value="wsc"></property> 18 <property name="user" value="root"></property> 19 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/shop?characterEncoding=utf-8"></property> 20 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 21 </bean> 22 <!--2 工厂类对象--> 23 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 24 <!--数据源--> 25 <property name="dataSource" ref="dataSource"></property> 26 <property name="packagesToScan" value="com.wsc.core.entity"></property> 27 <!--配置供应商适配器--> 28 <property name="jpaVendorAdapter"> 29 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 30 <property name="showSql" value="true"></property> 31 <property name="generateDdl" value="true"></property> 32 <!--是否向控制台输出语句 update hibernate.hbm2ddl.auto--> 33 <property name="database" value="MYSQL"></property> 34 </bean> 35 </property> 36 </bean> 37 <!--配置事物--> 38 <bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 39 <property name="entityManagerFactory" ref="entityManagerFactory"></property> 40 </bean> 41 <tx:advice id="txAdvice" transaction-manager="jpaTransactionManager"> 42 <tx:attributes> 43 <tx:method name="save*" propagation="REQUIRED"/> 44 <tx:method name="insert*" propagation="REQUIRED"/> 45 <tx:method name="update*" propagation="REQUIRED"/> 46 <tx:method name="delete*" propagation="REQUIRED"/> 47 <tx:method name="get*" read-only="true"/> 48 <tx:method name="find*" read-only="true"/> 49 <tx:method name="*" propagation="REQUIRED"/> 50 </tx:attributes> 51 </tx:advice> 52 <aop:config> 53 <aop:pointcut id="pointcut" expression="execution(* com.wsc.core.service.*.*(..)) "/> 54 <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"></aop:advisor> 55 </aop:config> 56 <!--dao--> 57 <jpa:repositories base-package="com.wsc.core.dao" 58 transaction-manager-ref="jpaTransactionManager" 59 entity-manager-factory-ref="entityManagerFactory"> 60 </jpa:repositories> 61 </beans>
3.....实体类
1 package com.wsc.core.entity; 2 3 import javax.persistence.*; 4 5 /** 6 * @version 1.0 7 * @ClassName Account 8 * @Description TODO 9 * @Author WSC 10 * @Date 2019/8/16 11:31 11 **/ 12 @Entity 13 @Table(name="ac_account") 14 public class Account 15 @GeneratedValue(strategy= GenerationType.IDENTITY) 16 @Id 17 @Column(name="ac_id") 18 private int acId; 19 @Column(name="ac_address") 20 private String acAddress; 21 @Column(name="ac_name") 22 private String acName; 23 @Column(name="ac_age") 24 private int acAge; 25 26 public int getAcId() 27 return acId; 28 29 30 public void setAcId(int acId) 31 this.acId = acId; 32 33 34 public String getAcAddress() 35 return acAddress; 36 37 38 public void setAcAddress(String acAddress) 39 this.acAddress = acAddress; 40 41 42 public String getAcName() 43 return acName; 44 45 46 public void setAcName(String acName) 47 this.acName = acName; 48 49 50 public int getAcAge() 51 return acAge; 52 53 54 public void setAcAge(int acAge) 55 this.acAge = acAge; 56 57 58 @Override 59 public String toString() 60 return "Account" + 61 "acId=" + acId + 62 ", acAddress=‘" + acAddress + ‘\‘‘ + 63 ", acName=‘" + acName + ‘\‘‘ + 64 ", acAge=" + acAge + 65 ‘‘; 66 67
4....dao
1 package com.wsc.core.dao; 2 3 import com.wsc.core.entity.Account; 4 import org.springframework.data.domain.Page; 5 import org.springframework.data.domain.Pageable; 6 import org.springframework.data.jpa.repository.JpaRepository; 7 import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 8 import org.springframework.data.jpa.repository.Modifying; 9 import org.springframework.data.jpa.repository.Query; 10 import org.springframework.transaction.annotation.Transactional; 11 12 import java.util.List; 13 14 //实现JpaRepository接口 15 public interface AccountDao extends JpaRepository<Account,Integer>, JpaSpecificationExecutor<Account> 16 /** 17 * 查询全部 18 * @return 19 */ 20 @Query("from Account ") 21 public List<Account> getFindAll(); 22 /** 23 * 查询全部 分页 24 * @return 25 * @param pageable 26 */ 27 @Query("from Account ") 28 public List<Account> getFindAllByPage(Pageable pageable); 29 /** 30 * 根据id查询 31 */ 32 @Query("from Account where ac_id=?1") 33 public Account getFindById(Long ac_id); 34 35 /** 36 * 37 * 模糊查询 38 */ 39 @Query("from Account where acName like ?1 and acAddress like ?2") 40 public List<Account> findByName(String acName,String acAddress); 41 42 /** 43 * 修改数据 44 */ 45 @Modifying 46 @Transactional 47 @Query("update Account set acName=?1 where acId=?2") 48 public void change(String acName,Integer acId); 49 50 // 原生SQL 语句查询 51 /** 52 * 原生SQL 语句查询 53 */ 54 @Query(value = "select * from ac_account WHERE ac_name LIKE ?" ,nativeQuery = true) 55 public List<Account> getAccount(String name); 56 57 //方法命名规则查询 58 /** 59 * 根据id查询 60 */ 61 public Account findAccountByAcId(Integer id); 62 63 /** 64 * 模糊查询 65 * @param address 66 * @param name 67 * @return 68 */ 69 public List<Account> findByAcAddressLikeAndAcNameLike(String address,String name); 70 71 /** 72 * 分页查询 73 * @param name 74 * @param pageable 75 * @return 76 */ 77 public Page<Account> findByAcNameLike(String name,Pageable pageable); 78 79 80
5.....test
test 01
(delete
save
findOne
count
getOne
分页
排序
判断是否存在)
1 package com.wsc.core.test; 2 3 import com.wsc.core.dao.AccountDao; 4 import com.wsc.core.entity.Account; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.data.domain.Page; 9 import org.springframework.data.domain.PageRequest; 10 import org.springframework.data.domain.Sort; 11 import org.springframework.test.context.ContextConfiguration; 12 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 import org.springframework.transaction.annotation.Transactional; 14 15 import java.util.List; 16 17 /** 18 * @version 1.0 19 * @ClassName TestJPA 20 * @Description TODO 21 * @Author WSC 22 * @Date 2019/8/16 13:12 23 **/ 24 @RunWith(SpringJUnit4ClassRunner.class) 25 @ContextConfiguration("classpath:applicationContext.xml") 26 public class TestJPA 27 @Autowired 28 private AccountDao accountDao; 29 /** 30 * 添加数据 31 */ 32 @Test 33 public void test01() 34 // ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); 35 // AccountDao accountDao = applicationContext.getBean(AccountDao.class); 36 Account account = new Account(); 37 account.setAcAddress("提阿女号"); 38 account.setAcAge(133); 39 account.setAcName("zhang"); 40 this.accountDao.save(account); 41 42 /** 43 * 删除数据 44 */ 45 @Test 46 public void test02() 47 accountDao.delete(22); 48 49 /** 50 * 修改数据 51 */ 52 @Test 53 public void test03() 54 Account account = accountDao.findOne(23); 55 account.setAcName("阿凡提123"); 56 account.setAcAddress("美丽的郑州"); 57 accountDao.save(account); 58 59 /** 60 * 查询单个数据 findOne 方法 61 */ 62 @Test 63 public void test04() 64 Account one = accountDao.findOne(23); 65 System.out.println(one); 66 67 /** 68 * 查询数据的总个数 count 方法 69 */ 70 @Test 71 public void getCount() 72 long count = accountDao.count(); 73 System.out.println(count); 74 75 /** 76 * 获得数据单个 77 * 必须加注解 @Transactional 事务注解 78 * 不加报错:could not initialize proxy - no Session 79 */ 80 @Test 81 @Transactional 82 public void getFindOne() 83 Account one = accountDao.getOne(23); 84 System.out.println(one); 85 86 /** 87 * 分页功能 88 */ 89 @Test 90 public void testPage() 91 PageRequest pageRequest = new PageRequest(0, 5); 92 Page<Account> accountPage = accountDao.findAll(pageRequest); 93 //总的数据条数 94 long totalElements = accountPage.getTotalElements(); 95 System.out.println(totalElements); 96 //总的页数 97 int totalPages = accountPage.getTotalPages(); 98 System.out.println(totalPages); 99 //遍历数据 100 for(Account account:accountPage) 101 System.out.println(account); 102 103 List<Account> accountList = accountPage.getContent(); 104 System.out.println(accountList); 105 106 /** 107 * 排序 Sort.Direction.DESC 降序 108 */ 109 @Test 110 public void testSort() 111 // 创建一个sort 对象 112 // 参数1 排序方式 113 // 参数2 排序的字段 应该是实体类的属性名 114 Sort sort = new Sort(Sort.Direction.DESC,"acId"); 115 List<Account> accountList = accountDao.findAll(sort); 116 for(Account account:accountList) 117 System.out.println(account); 118 119 /** 120 * 判断 是否 存在 exists 121 */ 122 @Test 123 public void testExists() 124 boolean exists = accountDao.exists(1); 125 System.out.println(exists); 126 127
test02
( 全部信息
根据id查
分页
模糊查询 原生SQL
方 法命名规则查询
模糊查询 方 法命名规则查询
根据 名字 模糊查询 并 分页)
1 package com.wsc.core.test; 2 3 import com.wsc.core.dao.AccountDao; 4 import com.wsc.core.entity.Account; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.data.domain.Page; 9 import org.springframework.data.domain.PageRequest; 10 import org.springframework.test.context.ContextConfiguration; 11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 13 import java.util.List; 14 15 /** 16 * @version 1.0 17 * @ClassName Test02 18 * @Description TODO 19 * @Author WSC 20 * @Date 2019/8/19 14:32 21 **/ 22 @RunWith(SpringJUnit4ClassRunner.class) 23 @ContextConfiguration("classpath:applicationContext.xml") 24 public class Test02 25 @Autowired 26 private AccountDao accountDao; 27 28 /** 29 * 全部信息 30 */ 31 @Test 32 public void testFindAll() 33 List<Account> findAll = accountDao.getFindAll(); 34 for (Account account : findAll) 35 System.out.println(account); 36 37 38 39 /** 40 * id 信息 41 */ 42 @Test 43 public void getById() 44 Account findById = accountDao.getFindById(3L); 45 System.out.println(findById); 46 47 48 /** 49 * 查询全部 分页 50 */ 51 @Test 52 public void getByPage() 53 // 参数 1:页面开始 2: 页面数据条数 54 PageRequest pageRequest = new PageRequest(0, 5); 55 List<Account> findAllByPage = accountDao.getFindAllByPage(pageRequest); 56 for (Account account:findAllByPage) 57 System.out.println(account); 58 59 60 61 /** 62 * 模糊查询 63 */ 64 @Test 65 public void get02() 66 List<Account> byName = accountDao.findByName("%2%", "%中%"); 67 for(Account account:byName) 68 System.out.println(account); 69 70 71 @Test 72 public void change() 73 accountDao.change("滚",3); 74 75 76 /** 77 * 原生SQL 语句查询 78 */ 79 @Test 80 public void get01() 81 List<Account> account = accountDao.getAccount("%2%"); 82 for(Account acc: account) 83 System.out.println(acc); 84 85 86 87 /** 88 * 方 法命名规则查询 89 */ 90 @Test 91 public void get03() 92 Account accountByAcId = accountDao.findAccountByAcId(3); 93 System.out.println(accountByAcId); 94 95 96 /** 97 * 模糊查询 方 法命名规则查询 98 */ 99 @Test 100 public void get04() 101 List<Account> byAcAddressAndAcName = accountDao.findByAcAddressLikeAndAcNameLike("%中%", "%2%"); 102 for(Account account:byAcAddressAndAcName) 103 System.out.println(account); 104 105 106 107 /** 108 * 根据 名字 模糊查询 并 分页 109 */ 110 @Test 111 public void get05() 112 Page<Account> byAcNameLike = accountDao.findByAcNameLike("%2%", new PageRequest(0, 2)); 113 long totalElements = byAcNameLike.getTotalElements(); 114 System.out.println(totalElements+"总的条数"); 115 int totalPages = byAcNameLike.getTotalPages(); 116 System.out.println(totalPages+"总的页数"); 117 List<Account> content = byAcNameLike.getContent(); 118 for(Account account:content) 119 System.out.println(account); 120 121 122
test03
(使用Specification方式进行查询)
1 package com.wsc.core.test; 2 3 import com.wsc.core.dao.AccountDao; 4 import com.wsc.core.entity.Account; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.data.domain.Page; 9 import org.springframework.data.domain.PageRequest; 10 import org.springframework.data.jpa.domain.Specification; 11 import org.springframework.test.context.ContextConfiguration; 12 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 14 import javax.persistence.criteria.CriteriaBuilder; 15 import javax.persistence.criteria.CriteriaQuery; 16 import javax.persistence.criteria.Predicate; 17 import javax.persistence.criteria.Root; 18 import java.util.List; 19 20 /** 21 * @version 1.0 22 * @ClassName test03 23 * @Description TODO 24 * @Author WSC 25 * @Date 2019/8/19 16:55 26 **/ 27 28 // 测试 使用 Specification 方式进行查询 29 30 @RunWith(SpringJUnit4ClassRunner.class) 31 @ContextConfiguration("classpath:applicationContext.xml") 32 public class test03 33 @Autowired 34 private AccountDao accountDao; 35 36 /** 37 * 使用 Specification 方式 38 * 根据 id 查询 39 */ 40 @Test 41 public void test01() 42 Account account=accountDao.findOne(new Specification<Account>() 43 @Override 44 public Predicate toPredicate(Root<Account> root, CriteriaQuery<?> query, CriteriaBuilder cb) 45 // 参数1 字段名称 46 // 参数2 字段的值 47 Predicate acId = cb.equal(root.get("acId"), 3); 48 return acId; 49 50 ); 51 System.out.println(account); 52 53 54 /** 55 * 多字段 模糊查询 并 整合 56 */ 57 @Test 58 public void get02() 59 accountDao.findAll(new Specification<Account>() 60 @Override 61 public Predicate toPredicate(Root<Account> root, CriteriaQuery<?> query, CriteriaBuilder cb) 62 // 创建 根据模糊查询的条件 63 Predicate acName = cb.like(root.get("acName"), "%2%"); 64 Predicate acAddress = cb.like(root.get("acAddress"), "%中%"); 65 // 组合两个条件 66 Predicate predicate = cb.and(acName, acAddress); 67 return predicate; 68 69 ).forEach(c->System.out.println(c));//打印功能 70 71 72 /** 73 * 多字段 模糊查询 并 整合 并 分页 74 */ 75 @Test 76 public void get03() 77 Page<Account> byAcNameLike= accountDao.findAll(new Specification<Account>() 78 @Override 79 public Predicate toPredicate(Root<Account> root, CriteriaQuery<?> query, CriteriaBuilder cb) 80 // 创建 根据模糊查询的条件 81 Predicate acName = cb.like(root.get("acName"), "%2%"); 82 Predicate acAddress = cb.like(root.get("acAddress"), "%中%"); 83 // 组合两个条件 84 Predicate predicate = cb.and(acName, acAddress); 85 return predicate; 86 87 ,new PageRequest(0,3)); 88 long totalElements = byAcNameLike.getTotalElements(); 89 System.out.println(totalElements+"总的条数"); 90 int totalPages = byAcNameLike.getTotalPages(); 91 System.out.println(totalPages+"总的页数"); 92 List<Account> content = byAcNameLike.getContent(); 93 for(Account account:content) 94 System.out.println(account); 95 96 97
以上是关于Jpa实现数据库的CRUD的主要内容,如果未能解决你的问题,请参考以下文章
用Springboot+Jpa实现学生CRUD操作(含前端页面,含分页,自定义SQL)