mybatis 无限极分类

  1. 定义一个实体类

import com.baomidou.mybatisplus.annotation.TableField;
import com.communal.common.anotations.*;
import com.communal.mybatis.entities.BaseTimeEntity;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import java.util.List;

/***
 * className Department
 * date: 2021-08-16 15:17
 * author: 杨兴
 **/
@Setter
@Getter
@NoArgsConstructor
@Table
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Department extends BaseTimeEntity<Long> {
    private static final long serialVersionUID = -6030797868846577498L;

    @TableElement(title = "部门名称")
    @FormField(title = "部门名称", required = true, minLength = 2, maxLength = 20, isSearch = true)
    @NotBlank(message = "部门名称不能为空")
    @Length(min = 2, max = 30, message = "部门名称的长度在2到30之间")
    private String name;

    @FormField(title = "部门说明", type = FormFieldType.TEXTAREA, order = 5)
    @Length(max = 150, message = "部门说明的长度最多150个字")
    private String summary;


    @FormField(title = "部门主管", order = 1)
    private Long chargeId;

    @TableField(exist = false)
    @TableElement(title = "部门主管", order = 3)
    private String change;


    @FormField(title = "上级部门", order = 6, type = FormFieldType.SELECT)
    private Long parentId;

    @TableField(exist = false)
    private List<Department> children;

    @TableField(exist = false)
    @TableElement(title = "部门员工数", order = 4, type = FieldType.TEMPLATE)
    private Integer employeeNumber;
}

2.定义mapper 的接口


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comptroller.entities.Department;
import com.comptroller.entities.Option;
import org.apache.ibatis.annotations.Param;

import java.util.List;


/***
 * className EmployeeMapper
 * date: 2021-09-15 16:51
 * author: 杨兴
 **/
public interface DepartmentMapper extends BaseMapper<Department> {
    Page<Department> selectDepartmentPage(IPage<Department> page, @Param("name") String name);
    List<Option> selectOptionList();
}

  1. 实现xml
    <resultMap id="departmentMap" type="com.comptroller.entities.Department">
        <id javaType="Long" property="id" column="id"/>
        <result property="employeeNumber" javaType="Integer" column="employee_number"/>
        <collection property="children" ofType="com.comptroller.entities.Department" select="selectByParent" column="id"></collection>
    </resultMap>
    <sql id="selectDepartmentSql">
        select a.*, (select count(b.id) from comptroller_employee as b
        where b.department_id = a.id)  as employee_number, (select real_name from  comptroller_employee where
        department_id = a.id) as `change` from comptroller_department as a
    </sql>
    <select id="selectByParent" resultMap="departmentMap">
        <include refid="selectDepartmentSql"></include>
        where a.parent_id = #{0}
    </select>
    <select id="selectDepartmentPage" resultMap="departmentMap">
        <include refid="selectDepartmentSql"></include>
        <where>
            a.parent_id is null
            <if test="name != null and name != ''">
                <bind name="name_key" value="'%' + name + '%'"/>
                a.name lile #{name_key}
            </if>
        </where>
    </select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值