ResultMap总结

对resultMap的感觉

1. 1. 1. resultMap用于绑定某种属性;
2. 2. 2. resultMap可以实现定制键值对来绑定实体类属性和数据库字段;
3. 3. 3. resultMap可以实现嵌套结果,从而嵌套绑定;通过关联(association )或集合(collection )实现跨表绑定。

当实体类属性名与数据库字段不对应时

在这里插入图片描述
映射xml

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tl.dao.UserMapper">

    <resultMap id="UserMap" type="com.tl.pojo.User">
    <!--column数据库中的字段,property实体类中的属性-->
    <result column="id" property="id" />
    <result column="name" property="name" />
    <result column="pwd" property="password" />
    </resultMap>

    <select id="getUserById" resultMap="UserMap">
    select * from user where id=#{id}
    </select>

</mapper>

实体类

public class User {
    private int id;
    private String name;
    private String password;
}

测试

public class Test {
    @org.junit.Test
    public void Test(){
        //通过工具类获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //通过sqlSession.getMapper获取接口对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //通过接口获取其实现的方法返回的user对象集
        User user = userMapper.getUserById(5);

        System.out.println(user);
    }
}

在这里插入图片描述

多对一(按结果嵌套)

映射xml

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tl.dao.StudentMapper">
    <select id="getStuInfo" resultMap="studentTeacher">
    select s.id sid,s.name sname,t.name tname,s.tid
    from student s,teacher t
    where s.tid=t.id;
    </select>
    <resultMap id="studentTeacher" type="com.tl.pojo.Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <result property="tid" column="tid"/>
        <!--关联结果-->
        <association property="teacher" javaType="com.tl.pojo.Teacher">
            <result property="name" column="tname"/>
        </association>
    </resultMap>
</mapper>

实体类

public class Student {
    private int id;
    private int tid;
    private String name;
    private String pwd;
    //组合另一张表对象
    private Teacher teacher;
   }

测试

@org.junit.Test
    public void Test(){
        //通过工具类获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //通过sqlSession.getMapper获取接口对象
        StudentMapper userMapper = sqlSession.getMapper(StudentMapper.class);
        //通过接口获取其实现的方法返回的user对象集
        List<Student> list = userMapper.getStuInfo();
        for(Student student:list){
            System.out.println(student);
        }
    }

多对一结果
为null是因为SQL语句没有指定字段,mybatis补上了默认值;
在这里插入图片描述

一对多(按结果嵌套)

映射xml

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tl.dao.TeacherMapper">
    <select id="getTeaInfo" resultMap="teacherStudent">
    select t.name tname,t.id tid,s.name sname
    from teacher t,student s
    where s.tid=t.id and t.id=#{tid};
  </select>
    <resultMap id="teacherStudent" type="com.tl.pojo.Teacher">
        <result property="name" column="tname"/>
        <result property="id" column="tid"/>
        <collection property="students" ofType="com.tl.pojo.Student">
            <result property="name" column="sname"/>
        </collection>
    </resultMap>
</mapper>

实体类

public class Teacher {
    private int id;
    private String name;
    private String pwd;
    //集合
    private List<Student> students;
 }

测试

@org.junit.Test
    public void test2(){
        //通过工具类获取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //通过sqlSession.getMapper获取接口对象
        TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
        //通过接口获取其实现的方法返回的user对象集
        List<Teacher> list = teacherMapper.getTeaInfo(1);
        for(Teacher teacher:list){
            System.out.println(teacher);
        }
//        System.out.println(teacher);
    }

一对多结果
为0为null是因为没有指定字段,mybatis补上了默认值;

 /*
            Teacher{id=1, name='唐三藏', pwd='null', 
            students=[Student{id=0, tid=0, name='至尊宝', pwd='null'}, 
            Student{id=0, tid=0, name='紫霞仙子', pwd='null'}, 
            Student{id=0, tid=0, name='猪八戒', pwd='null'}, 
            Student{id=0, tid=0, name='沙僧', pwd='null'}]}
         */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值