【记录解决问题】activiti--sql 转义符设置

发布于:2025-06-26 ⋅ 阅读:(20) ⋅ 点赞:(0)

一、背景

%、!、_在sql查询时需要转义,转义的语法

like %?2% escape ?#{escapeCharacter()}

二、activiti转义配置

String wildcardEscapeClause = "";
if (this.databaseWildcardEscapeCharacter != null && this.databaseWildcardEscapeCharacter.length() != 0) {
    wildcardEscapeClause = " escape '" + this.databaseWildcardEscapeCharacter + "'";
}

properties.put("wildcardEscapeClause", wildcardEscapeClause);

版本8.2.0测试demo配置activiti.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="processEngineConfiguration"
    class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">

    <property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
    <property name="jdbcDriver" value="org.h2.Driver" />
    <property name="jdbcUsername" value="sa" />
    <property name="jdbcPassword" value="" />

    <!-- Database configurations -->
    <property name="databaseSchemaUpdate" value="drop-create" />

    <!-- job executor configurations -->
    <property name="asyncExecutor" ref="asyncExecutor" />
    <property name="asyncExecutorActivate" value="false" />

    <property name="defaultFailedJobWaitTime" value="1" />
    <property name="asyncFailedJobWaitTime" value="1" />

    <!-- mail server configurations -->
    <property name="mailServerPort" value="5025" />

    <property name="mailServers">
      <map>
        <entry key="myEmailTenant">
          <bean class="org.activiti.engine.cfg.MailServerInfo">
            <property name="mailServerHost" value="localhost" />
            <property name="mailServerPort" value="5025" />
            <property name="mailServerUseSSL" value="false" />
            <property name="mailServerUseTLS" value="false" />
            <property name="mailServerDefaultFrom" value="activiti@myTenant.com" />
            <property name="mailServerUsername" value="activiti@myTenant.com" />
            <property name="mailServerPassword" value="password" />
          </bean>
        </entry>
      </map>
    </property>

    <property name="history" value="full" />

    <property name="enableProcessDefinitionInfoCache" value="true" />

    <property name="databaseWildcardEscapeCharacter" value="\" />

  </bean>

  <bean id="asyncExecutor" class="org.activiti.engine.impl.asyncexecutor.DefaultAsyncJobExecutor">
    <property name="defaultAsyncJobAcquireWaitTimeInMillis" value="1000" />
    <property name="defaultTimerJobAcquireWaitTimeInMillis" value="1000" />
  </bean>

</beans>

其中配置的转义符为反斜杠。这一句

<property name="databaseWildcardEscapeCharacter" value="\" />

三、项目配置

在注入ProcessEngineConfiguration时,设置。前后代码省略。

	@Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(){
    //省略前面代码...
		SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
		configuration.setDatabaseWildcardEscapeCharacter(String.valueOf(JPQLTemplates.DEFAULT_ESCAPE));
	//省略后面代码...
	}

配合TemplateFactory使用。

private static final TemplateFactory templateFactory = new TemplateFactory(JPQLTemplates.DEFAULT_ESCAPE);

转义方法

templateFactory.escapeForLike(str);

四、结论

注入ProcessEngineConfiguration时,设置setDatabaseWildcardEscapeCharacter,配合TemplateFactory(com.querydsl.core.types)使用。


网站公告

今日签到

点亮在社区的每一天
去签到