resultMap使用方法

resultMap使用案例:

1.CustomObjec对象的定义:

public class CustomObject {
    private Integer cid;
    private String cname;
    private String pwd;

2.数据库 User表 中的列名

id、name、pwd

可以看出,用来接收数据的CustomObject对象User表 中的列名和对象名不一一对应

如果要用resultType,数据库User表会把 数据传递CustomObject对象与列名相同的的属性名。这样只有对象中的pwd属性能正确接收User表中的pwd列的值

此时就需要用到resultMap,建立 CustomObject 对象User表的各列一一对应关系

3. dao接口方法:

CustomObject selectById2(@Param("stuid") Integer id);

4. mapper文件内的 sql语句 及 resultMap定义语句:

<!--    定义resultMap
        id:给resultMap的映射关系自定义一个名称,是唯一值
        type:填java类型的全限定名称-->
    <resultMap id="customMap" type="com.gys.vo.CustomObject">
<!--        定义列名和属性名的对应关系-->
<!--        主键列使用id标签-->
        <id column="id" property="cid"/>
<!--        非主键列使用result标签-->
        <result column="name" property="cname"/>
<!--        列名和属性名相同则不用再定义-->
    </resultMap>
<!--    使用resultMap属性来制定映射关系-->
    <select id="selectById2" resultMap="customMap">
        select * from User where id=#{stuid}
    </select>

作用:

User表主键列名id 对应 CustomObject对象的属性 cid
User表非主键列名name 对应 CustomObject对象的属性 cname
由于 User表非主键列名pwdCustomObject对象的属性 pwd 已经是对应关系,所以不需要再定义

select标签,使用resultMap属性,id的值为 自定义的 resultMap 的 id 例如上面定义的customMap,表明在此标签中执行 id 为 customMap 的 resultMap 所定义的映射关系

5. 测试:

//    1.使用工具类
    @Test
    public void testSelectById2(){
        SqlSession session = MyBatisUtil.getSqlSession();
        StudentDao dao = session.getMapper(StudentDao.class);
        CustomObject cstudent = dao.selectById2(3); // 传入参数 id=3
        System.out.println("student = " + cstudent);
        session.close();
    }

6. 结果:
student = CustomObject{cid=3,cname=‘李四3’, pwd=‘lisi3’}

7. 日志:
== Preparing: select * from User where id=?
== Parameters: 3(Integer)
== Columns: id, name, pwd
== Row: 3, 李四3, lisi3
== Total: 1

比较:

不使用 resultMap定义映射关系,而是直接使用resultType指定数据接收对象结果:

1. 只需要修改mapper文件内的 sql语句,其他不变:

<!--    直接使用resulttype-->
    <select id="selectById2" resultType="com.gys.vo.CustomObject">
        select * from User where id=#{stuid}
    </select>

2.结果:
student = CustomObject{cid=null, cname=‘null’, pwd=‘lisi3’}

3.日志:
== Preparing: select * from User where id=?
== Parameters: 3(Integer)
== Columns: id, name, pwd
== Row: 3, 李四3, lisi3
== Total: 1

通过两者的日志对比,可以知道,都获取到了完整信息。
使用resultMap的方法,建立映射关系,可以给对象的三个属性都接收数据成功
使用resultType的方法,没有建立映射关系,只有一个原先就存在对应关系的属性pwd接收数据成功

  • 21
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ResultMap 是 MyBatis 中用于映射查询结果的一个标签,它可以将查询结果集中的列映射到 Java 对象的属性上,从而方便我们进行数据操作。 下面是 ResultMap使用方法: 1. 在 MyBatis 的配置文件中定义 ResultMap ```xml <resultMap id="userMap" type="com.example.User"> <id property="id" column="user_id"/> <result property="username" column="user_name"/> <result property="password" column="user_password"/> <result property="email" column="user_email"/> </resultMap> ``` 上面的代码中,我们定义了一个名为 userMapResultMap,它将查询结果集中的列 user_id、user_name、user_password、user_email 分别映射到 User 对象中的 id、username、password、email 属性上。 2. 在 SQL 映射文件中使用 ResultMap ```xml <select id="getUserById" resultMap="userMap"> select user_id, user_name, user_password, user_email from user where user_id = #{id} </select> ``` 上面的代码中,我们定义了一个名为 getUserById 的查询语句,它使用了前面定义的 userMap ResultMap,查询结果将会被映射到 User 对象中。 3. 在 Java 代码中使用查询结果 ```java SqlSession sqlSession = sqlSessionFactory.openSession(); User user = sqlSession.selectOne("getUserById", 1); ``` 上面的代码中,我们使用 SqlSession 的 selectOne 方法执行查询语句 getUserById,并将查询结果映射到 User 对象中。 以上就是 ResultMap使用方法,它可以让我们更加方便地操作数据库

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值