如何自定義sql


場景: 在實際項目中,有可能你會自己寫一些sql,但是你又不想寫過多的dao,service xml的時候,我們可以利用flowable自身的自定義sql實現

實現這一場景,我們一般有兩種方式。

1、配置xml的形式

1.1、編寫xml文件

1.2、配置config

 
<property name="customMybatisXMLMappers">
        <set>
            <value>org/flowable/standalone/cfg/custom-mappers/CustomTaskMapper.xml</value>
        </set>
    </property>

 

1.3、執行查詢操作

 
CustomTask customTask = managementService.executeCommand(new Command<CustomTask>() {
            @Override
            public CustomTask execute(CommandContext commandContext) {
                return (CustomTask) CommandContextUtil.getDbSqlSession(commandContext).selectOne("selectOneCustomTask", taskId);
            }
        });

 

2、注解的方式

2.1、配置查詢接口注解

 
public interface MyTestMapper {

    @Select("SELECT ID_ as id, NAME_ as name, CREATE_TIME_ as createTime FROM ACT_RU_TASK")
    List<Map<String, Object>> selectTasks();

    @Select({ "SELECT task.ID_ as taskId, variable.LONG_ as variableValue FROM ACT_RU_VARIABLE variable", "inner join ACT_RU_TASK task on variable.TASK_ID_ = task.ID_",
            "where variable.NAME_ = #{variableName}" })
    List<Map<String, Object>> selectTaskWithSpecificVariable(String variableName);

}

 

2.2、配置config

 
<property name="customMybatisMappers">
        <set>
            <value>org.flowable.standalone.cfg.MyTestMapper</value>
        </set>
    </property>

 

2.3、執行sql

 

 

 // Fetch the columns we're interested in
        CustomSqlExecution<MyTestMapper, List<Map<String, Object>>> customSqlExecution = new AbstractCustomSqlExecution<MyTestMapper, List<Map<String, Object>>>(MyTestMapper.class) {

            @Override
            public List<Map<String, Object>> execute(MyTestMapper customMapper) {
                return customMapper.selectTasks();
            }
        };

        // Verify
        List<Map<String, Object>> tasks = managementService.executeCustomSql(customSqlExecution);

 

 

  

  

  

  


免責聲明!

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



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