- 定义一个实体类
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();
}
- 实现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>