MyBatis之自查询使用递归实现 N级联动效果(两种实现方式)

在MyBatis中利用递归实现n级联动

sql语句:select * from type where pid = 0;  首次指定pid值为0,然后下次根据pid为0的cid 作为下次查询的pid 
  public List<Category> getCategory(Integer pid); //接口层方法

映射文件配置

<mapper namespace="dao.CateGoryDao">
 <resultMap id="getSelf" type="entity.Category">
   <id column="cid" property="cid"></id>
   <result column="cname" property="cName"></result>
   <collection property="categorySet" select="getCategory" column="cid"></collection> //这里可以不用指定oftype 使用反向查询select从另一个maper文件中取出数据时必须用ofType
   <!--查到的cid作为下次的pid-->
 </resultMap>
 <select id="getCategory" resultMap="getSelf" >
  select * from category where pid=#{pid}
 </select>
</mapper>

mybatis的javaType和ofType 

都是指定对象的类型 不同的是当使用反向查询select从另一个maper文件中取出数据时必须用ofType

都可以为collection和association是指定对象的类型,

都不是必须写的, 只有反向select时需要ofType;

实体类:

package entity;
import java.util.HashSet;
import java.util.Set;
/**
 * Created by zhangyu on 2017/7/12.
 */
public class Category {
 private Integer cid;
 private String cName;
 private Integer pid;
 private Set<Category> categorySet = new HashSet<Category>();
 @Override
 public String toString() {
  return "Category{" +
    "cid=" + cid +
    ", cName='" + cName + '\'' +
    ", pid=" + pid +
    ", categorySet=" + categorySet +
    '}';
 }
 public Integer getCid() {
  return cid;
 }
 public void setCid(Integer cid) {
  this.cid = cid;
 }
 public String getcName() {
  return cName;
 }
 public void setcName(String cName) {
  this.cName = cName;
 }
 public Integer getPid() {
  return pid;
 }
 public void setPid(Integer pid) {
  this.pid = pid;
 }
 public Set<Category> getCategorySet() {
  return categorySet;
 }
 public void setCategorySet(Set<Category> categorySet) {
  this.categorySet = categorySet;
 }
}

测试类:

 //测试自连接
 @Test
 public void TestSelf(){
  CateGoryDao dao = MyBatis.getSessionTwo().getMapper(CateGoryDao.class);
  List<Category> list = dao.getCategory(0);
  for (Category item:list ) {
   System.out.println(item);
  }
 }

打印结果:

Category{cid=1, cName='图书', pid=0, categorySet=[Category{cid=5, cName='期刊报纸', pid=1, categorySet=[]}, Category{cid=3, cName='青年图书', pid=1, categorySet=[Category{cid=6, cName='读者', pid=3, categorySet=[Category{cid=7, cName='12月份', pid=6, categorySet=[]}]}]}, Category{cid=4, cName='少儿图书', pid=1, categorySet=[]}]}
Category{cid=2, cName='服装', pid=0, categorySet=[]}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用MyBatis递归查询实现树形结构的查询。具体步骤如下: 1. 定义实体类 定义一个实体类,表示树的节点,包含节点id、父节点id、节点名称等属性。 2. 定义Mapper接口 定义一个Mapper接口,包含一个方法,用于查询树形结构的节点。方法的返回值为List集合,表示查询到的树形结构的节点。 3. 编写Mapper.xml配置文件 在Mapper.xml配置文件中,编写递归查询语句,通过查询语句实现树形结构的查询。具体实现方式如下: - 定义一个select语句,用于查询指定节点的所有子节点。 - 在select语句中,使用union all关键字连接多个子查询语句,实现递归查询。 - 在子查询语句中,使用with recursive关键字定义递归查询语句。 4. 调用Mapper接口 在Java代码中,调用Mapper接口的方法,获取查询到的树形结构的节点。可以通过递归遍历节点,实现树形结构的展示。 下面是一个示例代码,供参考: ``` // 定义实体类 public class TreeNode { private Integer id; private Integer parentId; private String name; // getter和setter方法 } // 定义Mapper接口 public interface TreeNodeMapper { List<TreeNode> selectTreeNodes(Integer parentId); } // 编写Mapper.xml配置文件 <select id="selectTreeNodes" parameterType="java.lang.Integer" resultType="TreeNode"> with recursive cte(id, parent_id, name) as ( select id, parent_id, name from tree_node where parent_id = #{parentId} union all select tn.id, tn.parent_id, tn.name from tree_node tn inner join cte on tn.parent_id = cte.id ) select * from cte; </select> // 调用Mapper接口 @Autowired private TreeNodeMapper treeNodeMapper; public List<TreeNode> getTreeNodes(Integer parentId) { return treeNodeMapper.selectTreeNodes(parentId); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值