什么是Java Persistence API?
Java Persistence API提供了一个规范,用于将数据通过Java对象持久化、读取和管理到数据库中的关系表。
什么是Hibernate框架?
Hibernate是Java环境的对象关系映射解决方案。对象关系映射或ORM是将应用程序域模型对象映射到关系
数据库表的编程技术。Hibernate是一个基于Java的ORM工具,它提供了一个框架,用于将应用程序域对象映射
到关系数据库表。
Hibernate提供了Java Persistence API的参考实现,使其成为具有松散耦合优势的ORM工具的绝佳选择
注意:JPA是一个规范,Hibernate是一个JPA提供者或实现。
什么是Spring Data JPA?
Spring Data是Spring Framework的一部分,Spring Data存储库抽象的目标是显著减少为各种持久性存储实现
数据访问层所需的代码量。
Spring Data JPA不是JPA提供者。它是一个库/框架,它在我们的JPA提供程序(如Hibernate)的顶部添加了一
个额外的抽象层。
Hibernate和Spring Data JPA有什么区别?
Hibernate是一个JPA实现,而Spring Data JPA是一个JPA数据访问抽象。Spring Data提供了GenericDao自定义
实现的解决方案,它还可以通过方法名称约定代表您生成JPA查询。
Hibernate提供了Java Persistence API的参考实现,使其成为具有松散耦合优势的ORM工具的绝佳选择。
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> </dependencies>
application.yml
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: jdbc:mysql://127.0.0.1:3306/sell?characterEncoding=utf-8&serverTimezone=CTT&useSSL=false jpa: show-sql: true
建表sql:
create table `seller_info` ( `seller_id` varchar(32) not null, `username` varchar(32) not null, `password` varchar(32) not null, `openid` varchar(64) not null comment '微信openid', `create_time` timestamp not null default current_timestamp comment '创建时间', `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间', primary key (`seller_id`) ) comment '卖家信息表';
daoobject(实体类):
@Data @Entity @DynamicUpdate public class SellerInfo { @Id private String sellerId; private String username; private String password; private String openid; }
repository:
public interface SellerInfoRepository extends JpaRepository<SellerInfo,String> { SellerInfo findBySellerId(String sellerId); }
service:
public interface SellerInfoService { /** * * @param sellerId * @return */ SellerInfo findBySellerId(String sellerId); }
serviceImpl:
@Service @Slf4j public class SellerInfoServiceImpl implements SellerInfoService { @Autowired private SellerInfoRepository repository; @Override public SellerInfo findBySellerId(String sellerId){ SellerInfo sellerInfo = repository.findBySellerId(sellerId); return sellerInfo; } }
测试类:
@SpringBootTest @RunWith(SpringRunner.class) @Slf4j public class SellerInfoServiceImplTest { public final String SELLERID="1001"; @Autowired private SellerInfoServiceImpl sellerInfoService; @Test public void testFindBySellerId() throws Exception { SellerInfo result = sellerInfoService.findBySellerId(SELLERID); if(result == null){ log.error("【"+SELLERID+"的卖家信息未查出!!】"); throw new SellerException(ResultEnum.SELLINFO_FIND_FAIL); }else{ log.info("【"+SELLERID+"的卖家姓名为:{},手机号:{},地址:{}】",result.getUsername()); } } }