一、配置xml的形式
1. 编写xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.flow.dao.mapper.SqlMapper">
<select id="getRole" resultType="java.util.Map">
select * from tbl_role
</select>
</mapper>
2. 配置ProcessEngine加载xml文件
package com.flow.config;
import java.util.HashSet;
import java.util.Set;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {
@Override
public void configure(SpringProcessEngineConfiguration engineConfiguration) {
Set<String> xm = new HashSet<>();
xm.add("mapper/SqlMapper.xml");
engineConfiguration.setCustomMybatisXMLMappers(xm);
}
}
3. 执行查询
List<Map<String, Object>> list2 = managementService.executeCommand(new Command<List<Map<String, Object>>>() {
@Override
public List<Map<String, Object>> execute(CommandContext commandContext) {
return CommandContextUtil.getDbSqlSession(commandContext).selectList("com.qzt.flow.dao.mapper.SqlMapper.getRole");
//也可不加命名空间,直接使用id
//return CommandContextUtil.getDbSqlSession(commandContext).selectList("getRole");
}
});
4. Mapper接口方式查询
SqlMapper接口所在包要和xml命名空间一直
package com.flow.dao.mapper;
import java.util.List;
import java.util.Map;
public interface SqlMapper {
List<Map<String, Object>> getRole();
}
执行查询
List<Map<String, Object>> list2 = managementService.executeCommand(new Command<List<Map<String, Object>>>() {
@Override
public List<Map<String, Object>> execute(CommandContext commandContext) {
SqlMapper sm = CommandContextUtil.getDbSqlSession(commandContext).getCustomMapper(SqlMapper.class);
return sm.getRole();
}
});
二、配置注解的方式
1. 接口Mapper
package com.flow.dao.mapper;
import java.util.List;
import java.util.Map;
public interface SqlMapper {
@Select("select * from tbl_role")
List<Map<String, Object>> getRole();
}
2. 配置ProcessEngine加载mapper
package.qzt.flow.config;
import java.util.HashSet;
import java.util.Set;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.context.annotation.Configuration;
import com.qzt.flow.dao.mapper.SqlMapper;
@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {
@Override
public void configure(SpringProcessEngineConfiguration engineConfiguration) {
Set<Class<?>> m = new HashSet<>();
m.add(SqlMapper.class);
engineConfiguration.setCustomMybatisMappers(m);
}
}
3. 执行查询
List<Map<String, Object>> list1 = managementService.executeCustomSql(new AbstractCustomSqlExecution<SqlMapper, List<Map<String, Object>>>(SqlMapper.class) {
@Override
public List<Map<String, Object>> execute(SqlMapper mapper) {
return mapper.getRole();
}
});
三、传参
xml形式的需要获取mybatis的SqlSession即可正常传参
CommandContextUtil.getDbSqlSession(commandContext).getSqlSession()
使用DbSqlSession 不管是传对象还是map都会抛出There is no getter for property named 'xxx'异常
DbSqlSession 传参,参数需要封装成对象并继承ListQueryParameterObject
public static class Param extends ListQueryParameterObject {
private String id;
//不用加
/*public String getId() {
return id;
}*/
}
也可以整个dao继承AbstractQuery抽象类,参考:https://blog.csdn.net/weixin_52265084/article/details/112200147