Flowable源碼分析:Spring Data JPA實現從自己創建的數據庫中讀取指定信息


在Flowable-ui-task模塊查詢數據庫相關內容記錄

目錄:

  1. 讀取信息為Flowable內部對象
  2. 讀取信息為自定義對象屬性
  1. 讀取信息為ProcessDefinition(Flowable內部對象)

ProcessDefinition為Flowable中定義的對象,因此有提供專門的數據庫查詢接口,如:

  • 以id的方式
repositoryService.getProcessDefinition(processDefinitionId)
  • 以id列表的方式
ProcessDefinitionQuery definitionQuery = repositoryService.createProcessDefinitionQuery();
List<ProcessDefinition> definitions = definitionQuery.processDefinitionIds(processDefinitionIds).list();
  • 自定義sql方式
NativeProcessDefinitionQuery nativeProcessDefinitionQuery = repositoryService.createNativeProcessDefinitionQuery();
List<ProcessDefinition> definitions = nativeProcessDefinitionQuery.sql("SELECT act_re_procdef.* " +
    "FROM act_re_procdef,act_proc " +
    "WHERE act_a.A_ID_ = #{aId} AND act_re_procdef.ID_ = act_a.PROC_DEF_ID_ " +
    "ORDER BY a" +
    "DESC")
    .parameter("a", a).list();

項目中暫時只了解、用到以上幾種查詢接口,這些查詢僅限Flowable內部定義的對象,對自定義對象查詢沒有提供相應接口。
本文在以下部分記錄實驗室小伙伴介紹的使用JPA實現自定義對象的查詢。

  1. Spring Data JPA實現Flowable自定義對象查詢
  • 添加依賴
    本項目將依賴添加在了logic模塊下的pom文件中
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</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>
  • 相關配置
    在app模塊的flowable-default.properties中添加以下maven配置
# DATABASE
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/flowable?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

# JPA
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  • 創建實體
@Entity
@Table(name = "act_a")
public class MyObject{

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID_", unique = true, nullable = false)
    private Integer id;

    @Column(name = "NAME_", unique = true)
    private String name;

    @Column(name = "ADDRESS_", unique = true)
    private String address;


    public MyObject(String name, String address) {
        this.name= name;
        this.address= address;
    }

    public MyObject() {

    }

    // getters and setters
}

實體類名與數據庫表名不一樣時可在Table的name屬性中指定數據庫表名
同樣,屬性名與數據庫中字段名不一樣時,可在column的name屬性中指定對應數據庫表中的字段名

  • dao層:Repository構建
    該接口JPA會自動注入實現,不需要自己另外編寫代碼
public interface MyObjectRepository extends JpaRepository<MyObject, Integer> {
    @Query(value = "select ID_, NAME_, ADDRESS_ from act_a where ACT_AId_= ?1", nativeQuery = true)
    List<MyObject> findByAId(String aId);
}

查詢時select后面的字段需要寫上實體類定義的所有屬性對應的字段(此處為id、name、address對應的字段),否則會報錯“某列找不到”

另外,以下引用接口方法名實現數據庫操作的語法

Spring Data JPA 可以根據接口方法名來實現數據庫操作,主要的語法是 findXXBy、readAXXBy、queryXXBy、countXXBy、getXXBy 后面跟屬性名稱,利用這個功能僅需要在定義的 Repository 中添加對應的方法名即可,使用時 Spring Boot 會自動幫我們實現.

  • service部分
@Resource
private MyObjectRepository myObjectRepository;

public ResultListDataRepresentation getMyObjects(String aId){

   List<MyObject> myObjects = myObjectRepository.findByAId(aId);
   ResultListDataRepresentation result = new ResultListDataRepresentation(myObjects );
   return result;
   }
  • APPLYCATION
    程序入口如果沒有在以上文件的根目錄處(如果以上文件為org.flowable.root.service,則根目錄可為org.flowalbe.root),則程序入口處需添加掃描注解
    本項目中如下注解掃描如下:
@SpringBootApplication(proxyBeanMethods = false)
@ComponentScan(basePackages = {"org.flowable.ui.task.service", "org.flowable.ui.task.rest"})  // 掃描service、controller層
@EnableJpaRepositories(basePackages = "org.flowable.ui.task.repository") // 掃描dao層(repository)
@EntityScan(basePackages = "org.flowable.ui.task.model")  // 掃描實體層
public class FlowableTaskApplication extends SpringBootServletInitializer {
     ......
}

參考鏈接:
Spring Data JPA: https://www.cnblogs.com/wadmwz/p/10313495.html
實體類常用注解: https://blog.csdn.net/Mr_DouDo/article/details/79380642
SpringBoot JPA 中無法注入 JpaRepository 接口的問題: https://blog.csdn.net/weixin_30549175/article/details/99445024
cannot resolve column或cannot resolve table(據說這個問題可以不用解決?不影響?):
https://blog.csdn.net/qq78442761/article/details/104287164
https://blog.csdn.net/weixin_42941486/article/details/100660560


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM