189 - 基于XML的映射语句

Posted 分享牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了189 - 基于XML的映射语句相关的知识,希望对你有一定的参考价值。

当使用基于XML的映射语句时,语句在XML文件中定义。对于不需要整个任务数据的用例,只是其中的一小部分。XML文件可以如下所示:

<mapper namespace="org.flowable.standalone.cfg.TaskMapper">
<resultMap id="customTaskResultMap" type="org.flowable.standalone.cfg.CustomTask">
<id property="id" column="ID_" jdbcType="VARCHAR"/>
<result property="name" column="NAME_" jdbcType="VARCHAR"/>
<result property="createTime" column="CREATE_TIME_" jdbcType="TIMESTAMP" />
</resultMap>
<select id="selectCustomTaskList" resultMap="customTaskResultMap">
select RES.ID_, RES.NAME_, RES.CREATE_TIME_ from ACT_RU_TASK RES
</select>
</mapper>

结果被映射到org.flowable.standalone.cfg.CustomTask类的实例,其可以如下所示:

public class CustomTask 
protected String id;
protected String name;
protected Date createTime;
public String getId() 
return id;
 p
ublic String getName() 
return name;
 p
ublic Date getCreateTime() 
return createTime;


必须将Mapper XML文件提供给Process Engine配置,如下所示:

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

该声明可以执行如下:

List<CustomTask> tasks = managementService.executeCommand(new Command<List<CustomTask>>() 
@SuppressWarnings("unchecked")
@Override
public List<CustomTask> execute(CommandContext commandContext) 
return (List<CustomTask>) commandContext.getDbSqlSession().selectList("selectCustomTaskList");

);

对于需要更复杂语句的用例,XML映射语句可能会有所帮助。由于Flowable在内部使用XML映射语句,因此可以使用基础功能。

假设对于某些用例,根据id,name,type,userId等来查询附件数据的能力是必需的!为了完成这个用例,可以创建一个查询类AttachmentQuery来扩展 org.flowable.engine.impl.AbstractQuery,如下所示:

public class AttachmentQuery extends AbstractQuery<AttachmentQuery, Attachment> 
protected String attachmentId;
protected String attachmentName;
protected String attachmentType;
protected String userId;
public AttachmentQuery(ManagementService managementService) 
super(managementService);
 p
ublic AttachmentQuery attachmentId(String attachmentId)
this.attachmentId = attachmentId;
return this;

public AttachmentQuery attachmentName(String attachmentName)
this.attachmentName = attachmentName;
return this;
 p
ublic AttachmentQuery attachmentType(String attachmentType)
this.attachmentType = attachmentType;
return this;
 p
ublic AttachmentQuery userId(String userId)
this.userId = userId;
return this;

@Override
public long executeCount(CommandContext commandContext) 
return (Long) commandContext.getDbSqlSession()
.selectOne("selectAttachmentCountByQueryCriteria", this);

@Override
public List<Attachment> executeList(CommandContext commandContext, Page page) 
return commandContext.getDbSqlSession()
.selectList("selectAttachmentByQueryCriteria", this);

请注意,扩展AbstractQuery时,扩展类应将ManagementService实例传递给超级构造函数,并且方法executeCount和executeList需要实现以调用映射语句。

包含映射语句的XML文件可能如下所示:

<mapper namespace="org.flowable.standalone.cfg.AttachmentMapper">
<select id="selectAttachmentCountByQueryCriteria" parameterType="org.flowable.standalone.cfg.AttachmentQuery"
resultType="long">
select count(distinct RES.ID_)
<include refid="selectAttachmentByQueryCriteriaSql"/>
</select>
<select id="selectAttachmentByQueryCriteria" parameterType="org.flowable.standalone.cfg.AttachmentQuery"
resultMap="org.flowable.engine.impl.persistence.entity.AttachmentEntity.attachmentResultMap">
$limitBefore
select distinct RES.* $limitBetween
<include refid="selectAttachmentByQueryCriteriaSql"/>
$orderBy
$limitAfter
</select>
<sql id="selectAttachmentByQueryCriteriaSql">
from $prefixACT_HI_ATTACHMENT RES
<where>
<if test="attachmentId != null">
RES.ID_ = #attachmentId
</if>
<if test="attachmentName != null">
and RES.NAME_ = #attachmentName
</if>
<if test="attachmentType != null">
and RES.TYPE_ = #attachmentType
</if>
<if test="userId != null">
and RES.USER_ID_ = #userId
</if>
</where>
</sql>
</mapper>

诸如分页,排序,表名前缀等功能可用于语句中(因为parameterType是AbstractQuery的子类)。请注意,要映射结果,可以使用预定义的org.flowable.engine.impl.persistence.entity.AttachmentEntity.attachmentResultMap resultMap。

最后,AttachmentQuery可以使用如下:

....
// Get the total number of attachments
long count = new AttachmentQuery(managementService).count();
// Get attachment with id 10025
Attachment attachment = new AttachmentQuery(managementService).attachmentId("10025").singleResult();
// Get first 10 attachments
List<Attachment> attachments = new AttachmentQuery(managementService).listPage(0, 10);
// Get all attachments uploaded by user kermit
attachments = new AttachmentQuery(managementService).userId("kermit").list();
....

有关使用XML映射语句的工作示例,请检查文件夹src / test / java / org / flowable / standalone / cfg /和src / test / resources / org中的单元测试org.flowable.standalone.cfg.CustomMybatisXMLMapperTest以及其他类和资源/可流动/独立/ cfg中/

上面文章来自盘古BPM研究院:http://vue.pangubpm.com/
文章翻译提交:https://github.com/qiudaoke/flowable-userguide
了解更多文章可以关注微信公众号:

以上是关于189 - 基于XML的映射语句的主要内容,如果未能解决你的问题,请参考以下文章

189 - 基于XML的映射语句

MyBatis官方文档-XML 映射文件

188 - 基于注解的映射语句

188 - 基于注解的映射语句

MyBatis - 3.XML映射文件

MyBatis结果映射中Association与Collection的用法