• 前置条件

    在开发之前,请确保

    介绍

    demo 需涉及到 infra 层的 mapper 类及 repository 实现类。

    Mapper

    .1 mapper 接口类

    .2 mapper.xml

    .3 UserMapper.java 代码

    package org.hzero.todo.infra.mapper;
    
    import io.choerodon.mybatis.common.BaseMapper;
    import org.hzero.todo.domain.entity.User;
    
    /**
     * UserMapper
     */
    public interface UserMapper extends BaseMapper<User> {
    
    }
    

    .4 TaskMapper.java 代码

    package org.hzero.todo.infra.mapper;
    
    import java.util.List;
    import io.choerodon.mybatis.common.BaseMapper;
    import org.hzero.todo.domain.entity.Task;
    
    /**
     * TaskMapper
     */
    public interface TaskMapper extends BaseMapper<Task> {
    
        /**
         * 查询任务
         * 
         * @param params 任务查询参数
         * @return Task
         */
        List<Task> selectTask(Task params);
    }
    

    .5 UserMapper.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="org.hzero.todo.infra.mapper.UserMapper">
    
    </mapper>
    

    .6 TaskMapper.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="org.hzero.todo.infra.mapper.TaskMapper">
    
        <select id="selectTask" resultType="org.hzero.todo.domain.entity.Task">
            select
                tt.id,
                tt.employee_id,
                tt.state,
                tt.task_number,
                tt.task_description,
                tt.tenant_id,
                tt.object_version_number,
                tu.employee_name,
                tu.employee_number
            from todo_task tt join todo_user tu on tt.employee_id = tu.id
            <where>
                <if test="taskNumber != null and taskNumber != ''">
                    and tt.task_number = #{taskNumber}
                </if>
                <if test="taskDescription != null and taskDescription != ''">
                    <bind name="taskDescriptionLike" value="'%' + taskDescription + '%'" />
                    and tt.task_description like #{taskDescriptionLike}
                </if>
            </where>
        </select>
    
    </mapper>
    

    Repository

    .1 UserRepositoryImpl.java 代码

    package org.hzero.todo.infra.repository.impl;
    
    import org.springframework.stereotype.Component;
    import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
    import org.hzero.todo.domain.entity.User;
    import org.hzero.todo.domain.repository.UserRepository;
    
    /**
     * 用户资源库实现
     */
    @Component
    public class UserRepositoryImpl extends BaseRepositoryImpl<User> implements UserRepository {
    
    }
    

    .2 TaskRepositoryImpl.java 代码

    package org.hzero.todo.infra.repository.impl;
    
    import java.util.List;
    
    import org.apache.commons.collections4.CollectionUtils;
    import org.springframework.stereotype.Component;
    import io.choerodon.core.domain.Page;
    import io.choerodon.mybatis.pagehelper.PageHelper;
    import io.choerodon.mybatis.pagehelper.domain.PageRequest;
    import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
    import org.hzero.mybatis.common.Criteria;
    import org.hzero.todo.domain.entity.Task;
    import org.hzero.todo.domain.repository.TaskRepository;
    import org.hzero.todo.infra.mapper.TaskMapper;
    
    /**
     * 任务资源库实现
     */
    @Component
    public class TaskRepositoryImpl extends BaseRepositoryImpl<Task> implements TaskRepository {
    
        private final TaskMapper taskMapper;
    
        public TaskRepositoryImpl(TaskMapper taskMapper) {
            this.taskMapper = taskMapper;
        }
    
        @Override
        public Page<Task> pageTask(Task task, PageRequest pageRequest) {
            return PageHelper.doPage(pageRequest, () -> taskMapper.selectTask(task));
        }
    
        @Override
        public List<Task> selectByEmployeeId(Long employeeId) {
            Task task = new Task();
            task.setEmployeeId(employeeId);
            return this.selectOptional(task, new Criteria()
                    .select(Task.FIELD_ID, Task.FIELD_EMPLOYEE_ID, Task.FIELD_STATE, Task.FIELD_TASK_DESCRIPTION)
                    .where(Task.FIELD_EMPLOYEE_ID)
            );
        }
    
        @Override
        public Task selectDetailByTaskNumber(String taskNumber) {
            Task params = new Task();
            params.setTaskNumber(taskNumber);
            List<Task> tasks = taskMapper.selectTask(params);
            return CollectionUtils.isNotEmpty(tasks) ? tasks.get(0) : null;
        }
    }