Mybatis-Study05-ResultMap结果集映射

ResultMap结果集映射

简单使用ResultMap结果集映射

当我们数据库中的字段名与实体类中的属性名不一致时,我们的程序可能会出现一些我们意想不到的错误,比如。

数据库中的字段名如下:(id,name,pwd)
在这里插入图片描述
我们的实体类中的属性名如下(id,name,password)
在这里插入图片描述
当我们运行程序查询user表的结果为:
在这里插入图片描述
我们发现password的值为空
这就是字段名与属性名不同导致结果未查询出来

解决方案一:
可以在sql语句中为我们的pwd字段起一个别名

select id,name,pwd as password from mybatis.user

第二种方案就是使用ResultMap结果集映射

  • 在我们的mapper.xml文件中增加一个ResultMap标签
<!--id:随便起一个名字  type指的是我们的实体类 -->
    <resultMap id="resultMap" type="com.wang.pojo.User">
        <!--column指的是我们数据库中的字段名  property指的是我们实体类中的属性名-->
        <result column="id" property="id"></result>
        <result column="name" property="name"></result>
        <result column="pwd" property="password"></result>
    </resultMap>
  • 注意我们mapper.xml中的select标签(resultType改为resultMap)
  <select id="getUserList" resultMap="resultNap">
       select * from mybatis.user
   </select>
  • 再次运行程序我们就会发现我们的password已经有数值了:
    在这里插入图片描述
    注意:
    当我们属性名与字段名一样时,我们仅仅可以注册不一样的,如下。
<!--id:随便起一个名字  type指的是我们的实体类 -->
    <resultMap id="resultMap" type="com.wang.pojo.User">
        <!--column指的是我们数据库中的字段名  property指的是我们实体类中的属性名-->
        <result column="pwd" property="password"></result>
    </resultMap>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis-Plus 是一个 MyBatis 的增强工具,它提供了一系列增强功能来简化 MyBatis 的使用。其中之一就是提供了一个比 MyBatis 更加方便的结果映射方式,就是通过 `ResultMap`。 `ResultMap` 是 MyBatis 提供的结果映射器,它可以将数据库查询返回的结果映射Java 对象。MyBatis-Plus 的 `ResultMap` 功能就是在 MyBatis 的 `ResultMap` 基础上进行的增强。 在 MyBatis-Plus 中,我们可以通过 `@TableName` 注解来指定实体类和数据库表的映射关系,然后使用 `@TableField` 注解来指定实体类中的属性和数据库表中的字段的映射关系。 使用 MyBatis-Plus 的 `ResultMap` 功能时,我们只需要在实体类中定义一个 `ResultMap`,然后在查询语句中使用该 `ResultMap` 即可将查询结果映射Java 对象。 以下是一个使用 MyBatis-Plus `ResultMap` 的示例: ```java @TableName("user") public class User { @TableId(value = "id", type = IdType.AUTO) private Long id; @TableField("username") private String username; @TableField("password") private String password; // getter 和 setter 省略 } // 定义 ResultMap private static final ResultMap USER_RESULT_MAP = new ResultMap.Builder(configuration, "userResultMap", User.class, new ArrayList<ResultMapping>()) .id(new ResultMapping.Builder(configuration, "id", "id", Long.class).build()) .result(new ResultMapping.Builder(configuration, "username", "username", String.class).build()) .result(new ResultMapping.Builder(configuration, "password", "password", String.class).build()) .build(); // 使用 ResultMap 查询 List<User> userList = sqlSession.selectList("com.example.mapper.UserMapper.selectUserList", null, new RowBounds(0, 10), USER_RESULT_MAP); ``` 在上面的示例中,我们首先使用 `@TableName` 和 `@TableField` 注解指定了实体类和数据库表之间的映射关系。然后我们定义了一个 `ResultMap`,并在其中指定了实体类中的属性和数据库表中的字段的映射关系。最后我们在查询语句中使用该 `ResultMap` 将查询结果映射Java 对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

QQ星小天才

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

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

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

打赏作者

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

抵扣说明:

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

余额充值