Mybastis Plus 使用mapper.xml

记录一下mybatis plus 中mapper的使用

mybatis plus官网

分页器配置

1
2
3
4
5
6
7
8
9
@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

mapper.xml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?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="mapper.HiProcinstMapper">

    <select id="getTodoList" resultType="model.HiProcinstModel">
        select PROC.*
        from ACT_HI_PROCINST PROC
                 left join ACT_RU_TASK TASK on PROC.PROC_INST_ID_ = TASK.PROC_INST_ID_ ${ew.customSqlSegment}

    </select>

    <!--   -->
    <select id="getDoneList" resultType="model.HiProcinstModel">
        select PROC.*
        from ACT_HI_PROCINST PROC
                 left join ACT_HI_TASKINST TASK on PROC.PROC_INST_ID_ = TASK.PROC_INST_ID_ ${ew.customSqlSegment}
    </select>

</mapper>

mapper.kt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

package mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper
import model.HiProcinstModel;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage
import com.baomidou.mybatisplus.core.toolkit.Constants
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select

interface HiProcinstMapper : BaseMapper<HiProcinstModel> {

    /**
     * 获取代办列表
     */
    fun getTodoList( page:IPage<HiProcinstModel>,@Param(Constants.WRAPPER) wrapper: Wrapper<HiProcinstModel>) : IPage<HiProcinstModel>

    /**
     * 获取已处理列表
     */
    fun getDoneList( page:IPage<HiProcinstModel>,@Param(Constants.WRAPPER) wrapper: Wrapper<HiProcinstModel>) : IPage<HiProcinstModel>

}

我们只在mapper.xml中写了关联查询的语句,其他查询条件和分页我们还是使用了mybatisPlus的分页插件 和QueryWrapper

在Service中调用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@Service
open class HiProcinstServiceImpl : ServiceImpl<HiProcinstMapper, HiProcinstModel>(), IHiProcinstService {

    @Resource
    lateinit var processEngine: ProcessEngine

    /**
     * 待审批列表
     */
    override fun getTodoList(userId: String, pageParams: PageParams): Pagination<ProcInstVo> {
        val queryWrapper = QueryWrapper<HiProcinstModel>()
                // 任务分配给当前用户
            .eq("TASK.${HiTaskinstModel.ASSIGNEE_}", userId)
                // 并且是活动中的任务
            .eq("PROC.${HiProcinstModel.STATE_}", HistoricProcessInstance.STATE_ACTIVE)
            .orderByAsc("PROC.${HiProcinstModel.START_TIME_}")

        val todoList = this.baseMapper.getTodoList(Page(pageParams.page, pageParams.pageSize), queryWrapper)


        return Pagination(this.convert2Vo(todoList))
    }

    /**
     * 已处理流程实例
     */
    override fun getDoneList(userId: String, pageParams: PageParams): Pagination<ProcInstVo> {
        val queryWrapper = QueryWrapper<HiProcinstModel>()
            // 任务分配给当前用户
            .eq("TASK.${HiTaskinstModel.ASSIGNEE_}", userId)
            // 并且任务已完成
            .isNotNull("TASK.${HiTaskinstModel.END_TIME_}")
            .orderByDesc("PROC.${HiProcinstModel.START_TIME_}")

        val doneList = this.baseMapper.getDoneList(Page(pageParams.page, pageParams.pageSize), queryWrapper)


        return Pagination(this.convert2Vo(doneList))
    }

}
Built with Hugo
主题 StackJimmy 设计