mybaits查询树形结构数据(递归)

业务中有一张数据库表中存放树形结构数据,查询时,根据EntityType的数据集合,需要查询出其对应的所有子级数据。

其中数据库表结构如下:
在这里插入图片描述

其中AbstractEntityID为id,AbstractEntityParentID为当前数据的父节点id,顶层节点的id为空。

1. entity

首先我们创建数据库表对应的entity,其内容如下:

@Setter
@Getter
public class AbstractEntityTree {

    /**
     * 设备树类型
     */
    private String entityType;

    /**
     * 树结构
     */
    private String entityStructure;

    /**
     * 树id
     */
    private String id;

    /**
     * 父节点id
     */
    private String parentId;

    /**
     * code码
     */
    private String code;

    /**
     * 名
     */
    private String name;

    /**
     * 子节点
     */
    private List<AbstractEntityTree> children;

}

其中children为当前对象自身。

2. mapper

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xxx.xxx.mapper.common.AbstractEntityTreeMapper">

    <!--AbstractEntityTree实体类与查询出的数据字段的映射-->
    <resultMap id="baseResult" type="com.xxx.xxx.entity.AbstractEntityTree">
        <id property="entityType" column="EntityType"/>
        <id property="entityStructure" column="EntityStructure"/>
        <id property="entityType" column="EntityType"/>
        <id property="name" column="Name"/>
        <id property="code" column="Code"/>
        <id property="id" column="AbstractEntityID"/>
        <id property="parentId" column="AbstractEntityParentID"/>
        <association property="children" select="com.xxx.xxx.mapper.common.AbstractEntityTreeMapper.findTreeByParentId" column="AbstractEntityID" />
    </resultMap>

    <!--根据entityType获取设备树的信息(不传是获取所有的设备树信息)-->
    <select id="findTreeByEntityTypeList" resultMap="baseResult">
        SELECT * FROM AbstractEntityTree
        <if test="entityTypes != null and entityTypes.size > 0">
            WHERE EntityType IN
            <foreach collection="entityTypes" open="(" separator="," close=")" item="entityType">
                #{entityType}
            </foreach>
        </if>
    </select>

    <!--根据父节点id获取子设备树的信息-->
    <select id="findTreeByParentId" resultMap="baseResult">
        SELECT * FROM AbstractEntityTree WHERE AbstractEntityParentID = #{parentId}
    </select>

</mapper>

在这里插入图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis框架是一个优秀的ORM(对象关系映射)框架,它提供了多种方法向数据库插入数据。以下是使用MyBatis数据库插入数据的步骤: 1.在MyBatis中通过配置文件或注解方式定义数据源和SQL语句。配置文件是在mybatis-config.xml中定义,它设置了MyBatis框架中的全局属性。其他的数据源和数据库配置也可与此文件中连同。 2.配置Mapper文件,如mapper1.xml:mapper1.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xxx.mapper.UserMapper"> <insert id="insertUser" parameterType="com.xxx.entity.User"> insert into user(username,password,job,create_time,update_time) values(#{username},#{password},#{job},#{createTime},#{updateTime}) </insert> </mapper> 其中`namespace`是Mapper接口的全路径,`insert`是定义的插入语句,`parameterType`指定了实体类对应的参数类型。需要注意的是,在parameterType中指定的实体类属性名应该与数据库表中的字段名一致。 3.使用MyBatis框架提供的SqlSession类来获取一个与数据库连接的会话。 4.然后通过SqlSession中定义的插入方法insert执行插入操作。指定插入语句和插入参数,如下: ```java SqlSession sqlSession = MyBatisUtil.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setUserName("test"); user.setPassword("123456"); user.setJob("developer"); user.setCreateTime(new Date()); user.setUpdateTime(new Date()); // 执行插入操作 userMapper.insertUser(user); // 一定要提交,不然数据不会写入数据库 sqlSession.commit(); ``` 通过以上几个步骤就可以使用MyBatis框架向数据库插入数据了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值