Java MyBatis查询数据库&结果映射 之ResultMap的使用

 案例大致文件结构:

BrandMapper接口类,存放有sql的查询方法

pojo下的Brand是实体类

BrandMapper.xml与mybatis-config.xml是配置文件

test包下的myBatisTest是测试类

准备环境
1.数据库表tb_Brand
2.实体类 Brand
3.测试用例
4.安装MyBatisX插件

1.数据库表tb_Brand 

2.实体类 Brand

package com.itheima.pojo;
/*
在实体类中,基本数据类型,建议使用其对应的包装类
 */
public class Brand {

    //id主键
    private Integer id;
    //品牌名称
    private String brandName;
    //企业名称
    private String companyName;
    //排序字段
    private Integer ordered;
    //描述信息
    private String discription;
    //状态 0:禁用 1:启用
    private Integer status;


    public Brand(Integer id, String brandName, String companyName, Integer ordered, String discription,
                 Integer status) {
        super();
        this.id = id;
        this.brandName = brandName;
        this.companyName = companyName;
        this.ordered = ordered;
        this.discription = discription;
        this.status = status;
    }


    public Brand() {
    }



    @Override
    public String toString() {
        return "Brand [id=" + id + ", brandName=" + brandName + ", companyName=" + companyName + ", ordered=" + ordered
                + ", discription=" + discription + ", status=" + status + "]";
    }


    public Integer getId() {
        return id;
    }


    public void setId(Integer id) {
        this.id = id;
    }


    public String getBrandName() {
        return brandName;
    }


    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }


    public String getCompanyName() {
        return companyName;
    }


    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }


    public Integer getOrdered() {
        return ordered;
    }


    public void setOrdered(Integer ordered) {
        this.ordered = ordered;
    }


    public String getDiscription() {
        return discription;
    }


    public void setDiscription(String discription) {
        this.discription = discription;
    }


    public Integer getStatus() {
        return status;
    }


    public void setStatus(Integer status) {
        this.status = status;
    }





}

4.安装MyBatisX插件(更好用)

1.查询所有数据


    (1).编写接口方法Mapper接口   List<Brand> selectAll();
        参数:无
        返回值结果为:List<Brand>
    (2).编写sql语句:sql映射文件
         <select id="selectAll" resultType="com.itheima.pojo.Brand">
               select * from tb_brand;
         </select>
    (3).执行方法,测试

(1).编写接口方法Mapper接口 List<Brand> selectAll(); 参数:无 返回值结果为:List<Brand>

package com.itheima.mapper;
/*
使用mapper代理方式完成入门案例
    1.定义sql映射文件同名的mapper接口,并且将mapper接口和sql映射文件防止在同一目录下
    2.设置sql映射文件的namespace属性为mapper接口全限定名
    3.在mapper接口中定义方法,方法名就是sql映射文件中的sql语句中的id,并保持参数类型和返回值类型一致
    4.编码
       1.通过SqlSession的getMapper方法获取,Mapper接口的代理对象
       2.调用对应方法完成sql的执行
细节:如果Mapper接口名称和sql映射文件名称相同,并在同意目录下,则就可以使用包扫描的方式简化sql映射文件的加载
把<mapper resource="com/itheima/mapper/UserMapper.xml"/>更换为<package name="com.itheima.mapper"/>
 */
import com.itheima.pojo.Brand;

import java.util.List;

public interface BrandMapper {
    //1.查询所有
    public List<Brand> selectAll();
    //2.查看详情

}

 (2).编写sql语句:sql映射文件
         <select id="selectAll" resultType="com.itheima.pojo.Brand">
               select * from tb_brand;
         </select>

 <select id="selectAll" resultType="com.itheima.pojo.Brand">
               select * from tb_brand;
         </select>

  (3).执行方法,测试

(结果是成功的!)但是也可以发现公司名称和商品名称的值为null,这是由于

数据库表的字段名称  和  实体类的属性名  不一样,则不能自动封装数据

接下来我们来准备解决这个问题

有三种方式

1.起别名

2.起别名并且定义sql片段

3.使用resultMap

 1.起别名(缺点:太笨重)

<select id="selectAll" resultType="com.itheima.pojo.Brand">
     select
     id,brand_name as brandName,company_name as companyName,ordered,discription,status
      from  tb_brand;
     </select>

2.起别名并且定义sql片段(缺点:笨重,不灵活)

<sql id="brand_column">
   id,brand_name as brandName,company_name as companyName,ordered,discription,status
</sql>

<select id="selectAll" resultType="com.itheima.pojo.Brand">
   select
      <include refid="brand_column"></include>
   from  tb_brand;
</select>

3.使用resultMap(缺点:看似不方便,其实方便的一批)

 <resultMap id="brandResultMap" type="com.itheima.pojo.Brand">
        <result column="brand_name" property="brandName"></result>
        <result column="company_name" property="companyName"></result>
    </resultMap>

    <select id="selectAll" resultMap="brandResultMap">
        select
        *
        from  tb_brand;
    </select>

对于这个问题做出总结:

数据库表的字段名称  和  实体类的属性名  不一样,则不能自动封装数据
     起别名 缺点:每一次都需要重写
     定义sql片段(不灵活)
     ResultMap(以后最常用)
         id:完成主键字段的映射的
         column:数据库表的列名
         property:实体类的属性名
         result:完成一般字段的映射
         resultMap步骤:
             1.定义resultMap标签
             2.在select标签中把resultType换成resultMap属性
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis 中,可以使用递归查询实现对数据库中树形结构数据的查询。具体实现步骤如下: 1. 在 mapper.xml 文件中定义递归查询语句,例如: ```xml <select id="selectChildren" resultMap="treeResultMap"> WITH RECURSIVE temp(id, name, parent_id) AS ( SELECT id, name, parent_id FROM tree_table WHERE id = #{id} UNION ALL SELECT c.id, c.name, c.parent_id FROM tree_table c, temp p WHERE p.id = c.parent_id ) SELECT * FROM temp </select> ``` 这里使用 WITH RECURSIVE 关键字来定义递归查询语句,temp 表示递归表的别名,id、name、parent_id 是递归表的字段,tree_table 是表名,#{id} 是查询条件,treeResultMap 是结果集映射。 2. 在 mapper.xml 文件中定义结果集映射,例如: ```xml <resultMap id="treeResultMap" type="Tree"> <id property="id" column="id"/> <result property="name" column="name"/> <association property="parent" column="parent_id" select="selectChildren"/> <collection property="children" column="id" select="selectChildren"/> </resultMap> ``` 这里使用 association 和 collection 标签来定义关联属性和子节点属性。 3. 在 Java 代码中调用递归查询语句,例如: ```java public interface TreeMapper { Tree selectChildren(int id); } public class Tree { private int id; private String name; private Tree parent; private List<Tree> children; // getters and setters } TreeMapper mapper = sqlSession.getMapper(TreeMapper.class); Tree root = mapper.selectChildren(0); ``` 这里定义了 Tree 类来表示树节点,包含 id、name、parent 和 children 四个属性。调用 selectChildren 方法时,传入根节点的 id,返回包含根节点及其所有子节点的树对象。 以上就是使用 MyBatis 实现递归查询数据库的基本步骤。需要注意的是,递归查询可能会导致性能问题,应当谨慎使用

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

很丧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值