当前位置:Gxlcms > 数据库问题 > 搭建SpringCloud微服务框架:六、数据库持久层-SpringDataJPA

搭建SpringCloud微服务框架:六、数据库持久层-SpringDataJPA

时间:2021-07-01 10:21:17 帮助过:50人阅读

搭建微服务框架(数据库持久层-SpringDataJPA)

技术图片

用惯了Mybatis,这次来换换口味,在SQuid中集成SpringDataJPA。

本文源地址:搭建微服务框架(数据库持久层-SpringDataJPA)

Github地址:SQuid


介绍

以前都是听说过的是 HibernateJPA ,却从来没有使用过,一直在项目中使用的是 Mybatis

SpringDataJPA是基于Hibernate的底层封装的一套ORM框架,使用起来的第一感觉是代码量真的很少,相较传统的Mybatis来说,感觉最起码少了60%,当然大部分都是体现在xml文件上。

介绍真的没有太多词汇可以展示出来,下面来进行使用。??


使用

在squid项目中,我们新建一个 squid-example-jpa的项目(由于之前的example目录被删除,可以根据下面的层级目录来进行新建)

技术图片

引入依赖:

  1. <code class="language-xml"><dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-jpa</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.alibaba</groupId>
  7. <artifactId>druid-spring-boot-starter</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>mysql</groupId>
  11. <artifactId>mysql-connector-java</artifactId>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.projectlombok</groupId>
  15. <artifactId>lombok</artifactId>
  16. <optional>true</optional>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-test</artifactId>
  25. </dependency>
  26. </code>

生成Java实体

如果使用的是IDEA,完全可以参考这篇博客 IDEA下生成SpringDataJPA的Java实体
来生成实体信息。

因为使用了lombok,所以在生成的实体中并没有 getter setter 方法呈现,关于lombok可以了解一下 Lombok

DAO

生成了实体信息后,DAO文件就需要我们自己来手工生成了:

  1. <code class="language-java">public interface EdocInvoiceRepository extends JpaRepository<EdocInvoice, Long> {
  2. }
  3. </code>

一般我们直接继承的是 JpaRepository ,这个是包含所有JPA处理的类,基本上拥有了所有持久层的交互方法。

JPARespository:

  1. <code class="language-java">public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
  2. List<T> findAll();
  3. List<T> findAll(Sort var1);
  4. List<T> findAllById(Iterable<ID> var1);
  5. <S extends T> List<S> saveAll(Iterable<S> var1);
  6. void flush();
  7. <S extends T> S saveAndFlush(S var1);
  8. void deleteInBatch(Iterable<T> var1);
  9. void deleteAllInBatch();
  10. T getOne(ID var1);
  11. <S extends T> List<S> findAll(Example<S> var1);
  12. <S extends T> List<S> findAll(Example<S> var1, Sort var2);
  13. }
  14. </code>

如果你还有其他需求,比如需要根据某两个字段进行查询等等,SpringDataJPA完全支持:

Keyword Sample PQL snippet
And findByLastnameAndFirstname where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstname,findByFirstnameIs,findByFirstnameEquals … where x.firstname = ?1
Between findByStartDateBetween … where x.startDate between ?1 and ?2
LessThan findByAgeLessThan … where x.age < ?1
LessThanEqual findByAgeLessThanEqual … where x.age <= ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull findByAgeIsNull … where x.age is null
IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like ?1
NotLike findByFirstnameNotLike ... findByFirstnameNotLike
StartingWith findByFirstnameStartingWith … where x.firstname like ?1 (parameter bound with appended %)
EndingWith findByFirstnameEndingWith … where x.firstname like ?1 (parameter bound with prepended %)
Containing findByFirstnameContaining … where x.firstname like ?1 (parameter bound wrapped in %)
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection ages) … where x.age not in ?1
True findByActiveTrue() … where x.active = true
False findByActiveFalse() … where x.active = false
IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1)

也可以访问SpringDataJPA的官方文档查看:SpringDataJPA

Service & Impl & Controller

service、service.impl,controller,按照常规项目中来编写就行,其中需要注意的是,service如果是要作为对外接口,可以注明 @FeignClient("squid-example-jpa"),可以参考 sc-server

贴上项目中的例子:

  1. <code class="language-java">
  2. public interface EdocInvoiceService {
  3. List<EdocInvoice> findAll();
  4. EdocInvoice save(EdocInvoice edocInvoice);
  5. }
  6. @Service
  7. public class EdocInvoiceServiceImpl implements EdocInvoiceService {
  8. @Autowired
  9. private EdocInvoiceRepository edocInvoiceRepository;
  10. @Override
  11. public List<EdocInvoice> findAll() {
  12. return edocInvoiceRepository.findAll();
  13. }
  14. @Override
  15. public EdocInvoice save(EdocInvoice edocInvoice) {
  16. return edocInvoiceRepository.save(edocInvoice);
  17. }
  18. }
  19. @RestController
  20. @RequestMapping(value = "/invoice")
  21. public class EdocInvoiceController {
  22. @Autowired
  23. private EdocInvoiceService edocInvoiceService;
  24. @PostMapping(value = "/findAll")
  25. public List<EdocInvoice> findAll() {
  26. return edocInvoiceService.findAll();
  27. }
  28. @PostMapping(value = "/save")
  29. public EdocInvoice save(@RequestBody EdocInvoice edocInvoice) {
  30. return edocInvoiceService.save(edocInvoice);
  31. }
  32. }
  33. </code>

application.yaml

  1. <code class="language-yaml">spring:
  2. application:
  3. name: squid-miniprogram
  4. datasource:
  5. driver-class-name: com.mysql.jdbc.Driver
  6. url: jdbc:mysql://yanzhenyidai.com:3306/fapiaochi?useUnicode=true&characterEncoding=utf-8
  7. username: root
  8. password: ***
  9. type: com.alibaba.druid.pool.DruidDataSource
  10. jpa:
  11. hibernate:
  12. ddl-auto: update
  13. show-sql: true
  14. server:
  15. port: 9090
  16. </code>

jpa.hibernate.ddl-auto

key value
create 启动时删数据库中的表,然后创建,退出时不删除数据表
create-drop 启动时删数据库中的表,然后创建,退出时删除数据表 如果表不存在报错
update 如果启动时表格式不一致则更新表,原有数据保留
validate 项目启动表结构进行校验 如果不一致则报错

一般的 ddl-auto 属性使用 update 就行。

多表查询

复杂业务出现时,可能会使用到多表查询,但是JPA不像Mybatis那么灵活,需要创建中间实体来进行接收,我一般的做法两张表分开查询,然后合并。


总结

SpringDataJPA使用起来的感受就是一个字 “” ,相较于比较小型的项目,真的推荐使用SpringDataJPA,它能快速的搭建起一个持久层,也不用像Mybatis一样太多的关心底层的xml文件。

参考资料:

SpringDataJPA-Github

SpringDataJPA-doc

搭建SpringCloud微服务框架:六、数据库持久层-SpringDataJPA

标签:type   cat   结构   业务   格式   idea   orm   project   var   

人气教程排行