mybatis-plus使用注解方式实现递归


service层

    /**
     * 获取所有部门
     * @return
     */
    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.getAllDepartments();
    }

mapper层

    /**
     * 查询所有部门
     * @return
     */
    @Select("select * from t_department where parent_id = 0")
    @Results(id = "departmentMap",value = {
            @Result(property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "parentId",column = "parent_id"),
            @Result(property = "depPath",column = "dep_path"),
            @Result(property = "enabled",column = "enabled"),
            @Result(property = "isParent",column = "is_parent"),
            @Result(property = "children",column = "id",many = @Many(select = "getAllDepartmentsId")),//这里表示一对多 一里面包含多
    })
    List<Department> getAllDepartments();

    @Select("select * from t_department where parent_id = #{parent_id}")
    @ResultMap("departmentMap")
    List<Department> getAllDepartmentsId();


children: 它是在实体类定义的字段 private List children;
id = "departmentMap": 通过注解@Results来指定对应关系 指定@ResultMap("departmentMap")
column指定数据库字段的名称,property指定实体类属性的名称,jdbcType数据库字段类型,@Result里的id值为true表明主键,默认false


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM