温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用mybatis递归怎么实现一对多

发布时间:2021-05-11 16:31:48 来源:亿速云 阅读:128 作者:Leah 栏目:编程语言

这篇文章给大家介绍使用mybatis递归怎么实现一对多,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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);

实体类

@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层

public interface GoodsMapper {
 List<GoodsCategoryVo> getCategory(Integer pid);
}

mapper.xml

<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>

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

@RequestMapping("/getGoodsList")
 @ResponseBody
 public List<GoodsCategoryVo> getGoodsList(){
  // pid指定为0
  List<GoodsCategoryVo> list = goodsMapper.getCategory(0);
  return list;
 }

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

[
 {
  "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

public class Dept {
 private Integer id;
 private String deptName;
 private String locAdd;
 private List<Emp> emps
@JsonIgnoreProperties("dept")
public class Emp {
 private Integer id;
 private String name;
 private Dept dept;

dao层

public interface DeptMapper {
 public Dept getDeptById(Integer id);
}
public interface EmpMapper {
 public Emp getEmpByDeptId(Integer deptId); 
}

mapper.xml文件

<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>
<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方法

@RequestMapping("/getDeptById")
 @ResponseBody
 public Dept getDeptById() {
  Dept deptById = deptMapper.getDeptById(1);
  return deptById;
 }

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

代码目录

使用mybatis递归怎么实现一对多

关于使用mybatis递归怎么实现一对多就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI