好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

mybatis递归 一对多的实现方法示例

前言

今天需要做一个功能,根据专业,有不同的章节,章节下面有对应的习题,

由于只有这么两级,可以不用使用 递归 ,直接查询父集,之后foreach查询子集放入对应的list集合。

虽然实现了,感觉毕竟,太low。

有同事跟我说可以使用 mybatis 的递归实现,就学习了下。

对应的bean里面需要有对应的list<bean> lists的引用。

直接上代码

对应的sql语句

?

1

2

3

4

5

6

7

8

9

10

11

12

13

create table `goods_category` (

  `goodscateid` int ( 11 ) not null auto_increment,

  `name` varchar( 255 ) default null ,

  `parentid` int ( 11 ) default null ,

  `description` varchar( 255 ) default null ,

  `displayorder` int ( 11 ) default null ,

  `commissionrate` double default null ,

  `enabled` int ( 11 ) default null ,

  primary key (`goodscateid`)

) engine=innodb auto_increment= 11 default charset=utf8;

 

/*data for the table `goods_category` */

insert into `goods_category`(`goodscateid`,`name`,`parentid`,`description`,`displayorder`,`commissionrate`,`enabled`) values ( 1 , 'java' , 0 , '111' , null , null , null ),( 2 , 'spring' , 1 , '222' , null , null , null ),( 3 , 'springmvc' , 1 , '333' , null , null , null ),( 4 , 'struts' , 1 , '444' , null , null , null ),( 5 , 'jdbc' , 0 , '555' , null , null , null ),( 6 , 'hibernate' , 5 , '666' , null , null , null ),( 7 , 'mybatis' , 5 , '777' , null , null , null ),( 8 , 'jdbctemplate' , 5 , '888' , null , null , null ),( 9 , 'beanfactory' , 3 , '999' , null , null , null ),( 10 , 'factorybean' , 3 , '000' , null , null , null );

实体类

?

1

2

3

4

5

6

7

8

9

10

11

@jsonignoreproperties ({ "displayorder" , "commissionrate" , "enabled" })

public class goodscategoryvo {

  private integer goodscateid;

  private string name;

  private integer parentid;

  private string description;

  private integer displayorder;

  private double commissionrate;

  private integer enabled;

  private list<goodscategoryvo> catelist;

get 。。。 set。。。 tostring。。。

dao层

?

1

2

3

public interface goodsmapper {

  list<goodscategoryvo> getcategory(integer pid);

}

mapper.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

<resultmap id= "getself" type= "com.bscc.beans.goodscategoryvo" >

   <id column= "goodscateid" property= "goodscateid" ></id>

   <result column= "name" property= "name" ></result>

   <collection property= "catelist" select= "getcategory"

    column= "goodscateid" ></collection>

   <!--查到的cid作为下次的pid -->

  </resultmap>

 

  <select id= "getcategory" resultmap= "getself" >

   select * from goods_category where parentid=#{pid}

   order by displayorder,goodscateid

  </select>

之后直接访问对应的方法,即可查询出来

?

1

2

3

4

5

6

7

@requestmapping ( "/getgoodslist" )

  @responsebody

  public list<goodscategoryvo> getgoodslist(){

   // pid指定为0

   list<goodscategoryvo> list = goodsmapper.getcategory( 0 );

   return list;

  }

结果,可以使用 json在线工具  ,也可以使用 这个工具

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

[

  {

   "goodscateid" : 1 ,

   "name" : "java" ,

   "parentid" : 0 ,

   "description" : "111" ,

   "catelist" : [

    {

     "goodscateid" : 2 ,

     "name" : "spring" ,

     "parentid" : 1 ,

     "description" : "222" ,

     "catelist" : []

    },

    {

     "goodscateid" : 3 ,

     "name" : "springmvc" ,

     "parentid" : 1 ,

     "description" : "333" ,

     "catelist" : [

      {

       "goodscateid" : 9 ,

       "name" : "beanfactory" ,

       "parentid" : 3 ,

       "description" : "999" ,

       "catelist" : []

      },

      {

       "goodscateid" : 10 ,

       "name" : "factorybean" ,

       "parentid" : 3 ,

       "description" : "000" ,

       "catelist" : []

      }

     ]

    },

    {

     "goodscateid" : 4 ,

     "name" : "struts" ,

     "parentid" : 1 ,

     "description" : "444" ,

     "catelist" : []

    }

   ]

  },

  {

   "goodscateid" : 5 ,

   "name" : "jdbc" ,

   "parentid" : 0 ,

   "description" : "555" ,

   "catelist" : [

    {

     "goodscateid" : 6 ,

     "name" : "hibernate" ,

     "parentid" : 5 ,

     "description" : "666" ,

     "catelist" : []

    },

    {

     "goodscateid" : 7 ,

     "name" : "mybatis" ,

     "parentid" : 5 ,

     "description" : "777" ,

     "catelist" : []

    },

    {

     "goodscateid" : 8 ,

     "name" : "jdbctemplate" ,

     "parentid" : 5 ,

     "description" : "888" ,

     "catelist" : []

    }

   ]

  }

]

mybatis递归就是这么的简单。

说下mybatis 一对多 实现

对应的bean

?

1

2

3

4

5

public class dept {

  private integer id;

  private string deptname;

  private string locadd;

  private list<emp> emps

?

1

2

3

4

5

@jsonignoreproperties ( "dept" )

public class emp {

  private integer id;

  private string name;

  private dept dept;

dao层

?

1

2

3

public interface deptmapper {

  public dept getdeptbyid(integer id);

}

?

1

2

3

public interface empmapper {

  public emp getempbydeptid(integer deptid);

}

mapper.xml文件

?

1

2

3

4

5

6

7

8

9

10

11

12

<mapper namespace= "com.bscc.mapper.deptmapper" >

  <resultmap id= "deptresultmap" type= "com.bscc.beans.dept" >

  <id property= "id" column= "id" />

  <result property= "deptname" column= "deptname" />

  <result property= "locadd" column= "locadd" />

  <!-- private list<emp> emps; column= "id" 写被集合对象主键,select按照外键键查询,通过deptid查出emp给dept-->

  <collection property= "emps" column= "id" oftype= "emp" select= "com.bscc.mapper.empmapper.getempbydeptid" />

  </resultmap>

  <select id= "getdeptbyid" parametertype= "integer" resultmap= "deptresultmap" >

   select * from tbl_dept where id=#{id}

  </select>

</mapper>

?

1

2

3

4

5

6

7

8

9

<mapper namespace= "com.bscc.mapper.empmapper" >

  <resultmap id= "empresultmap" type= "com.bscc.beans.emp" >

  <id property= "id" column= "id" />

  <result property= "name" column= "name" />

  </resultmap>

  <select id= "getempbydeptid" parametertype= "integer" resultmap= "empresultmap" >

  select * from tbl_emp where deptid=#{deptid}

  </select>

</mapper>

对应的controller方法

?

1

2

3

4

5

6

@requestmapping ( "/getdeptbyid" )

  @responsebody

  public dept getdeptbyid() {

   dept deptbyid = deptmapper.getdeptbyid( 1 );

   return deptbyid;

  }

无非就是比简单查询复杂一些罢了。

代码目录

ok!!!

对应的github地址

https://github.com/chywx/mavenproject6onetomany  

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:https://www.cnblogs.com/c-h-y/p/9434126.html

查看更多关于mybatis递归 一对多的实现方法示例的详细内容...

  阅读:44次