Mybatis之resultMap结果集映射

解决属性名和字段名不一致的问题

Mybatis之resultMap结果集映射_第1张图片
Mybatis之resultMap结果集映射_第2张图片
当实体类中的属性与数据库中的字段名不匹配时,结果不会将数据存入实体类中
在这里插入图片描述
解决方法一:

select * from t_user

将原来的SQL语句改成(起别名):

select id,username,password as pwd,gender,regist_time from t_user

解决方式二:
通过resultMap结果集映射:
Mybatis之resultMap结果集映射_第3张图片
Mybatis之resultMap结果集映射_第4张图片


    <resultMap id="userMap" type="User">

        <result column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="pwd"/>
        <result column="gender" property="gender"/>
        <result column="regist_time" property="regist_time"/>
    resultMap>
    <select id="selectUsers"  resultMap="userMap">
        select * from t_user
    select>

运行结果:
Mybatis之resultMap结果集映射_第5张图片
注意:对于数据库其他字段和实体类中的属性相一致时,我们可以不必进行一一映射,只需要将对应的字段和属性映射即可


    <resultMap id="userMap" type="User">

        <result column="password" property="pwd"/>
    resultMap>
    <select id="selectUsers"  resultMap="userMap">
        select * from t_user
    select>

Mybatis多对一查询

使用java实现sql语句

select s.id,s.`name`,t.`name` as 授课老师 from `student` s,`teacher` t where s.tid=t.id

Mybatis之resultMap结果集映射_第6张图片

  1. 数据库设计
CREATE TABLE `teacher` (
  `id` INT(10) NOT NULL,
  `name` VARCHAR(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT INTO teacher(`id`, `name`) VALUES (1, '陈老师'); 

CREATE TABLE `student` (
  `id` INT(10) NOT NULL,
  `name` VARCHAR(30) DEFAULT NULL,
  `tid` INT(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1', '小明', '1'); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('2', '小红', '1'); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('3', '小张', '1'); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('4', '小李', '1'); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('5', '小王', '1');
  1. 建立实体类
    Student类:
@Data
public class Student {
     
	//学生的id
    private int id;
    //学生的名字
    private String name;
    //多个学生可以是同一个老师,即多对一
    private Teacher teacher;
}

Teacher类:

@Data
public class Teacher {
     
	//老师id
    private int id;
    //老师的名字
    private String name;
}
  1. 编写接口类:
public interface StudentMapper {
     
	//查询学生对应的所有老师
    List<Student> queryStudents();

}

按查询嵌套处理

  1. Mapper.xml配置文件


<mapper namespace="dao.StudentMapper">

    

    <resultMap id="students" type="Student">
        
        <association property="teacher" javaType="Teacher" column="tid" select="getTeacher"/>
    resultMap>

    <select id="queryStudents" resultMap="students" >
        select * from student
    select>
    
    <select id="getTeacher" resultType="Teacher">
        select * from teacher where id=#{tid}
    select>
mapper>
  1. 在Mybatis核心配置文件注册mapper

    <mappers>

        <mapper class="dao.StudentMapper"/>
    mappers>
  1. 测试类:
public class Test {
     
    @org.junit.Test
    public void test(){
     
        //通过封装好的工具类获取SqlSession会话
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //通过接口类型class获取接口对象实例(动态代理)
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //执行接口中的方法
        List<Student> students = mapper.queryStudents();
        for (Student student : students) {
     
            System.out.println(student);
        }
        sqlSession.commit();
        //关闭SqlSession
        sqlSession.close();
    }
}
  1. 结果:
    Mybatis之resultMap结果集映射_第7张图片
    按照结果嵌套处理

Mapper.xml配置文件中sql语句:

<resultMap id="students" type="Student">
        
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>

        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"/>
        association>
    resultMap>

    <select id="queryStudents" resultMap="students" >
        select s.`id` sid,s.`name` sname,t.`name` tname from `student` s,`teacher` t where s.tid=t.id
    select>

结果:

Mybatis之resultMap结果集映射_第8张图片

Mybatis一对多查询

  1. 创建实体类
    Student类
@Data
public class Student {
     
    private int id;
    private String name;
    private int tid;
}

Teacher类

@Data
public class Teacher {
     
    private int id;
    private String name;
    //一个老师对应多个学生
    private List<Student> students;
}
  1. 编写接口类
public interface TeacherMapper {
     

    //通过老师的id查询所有的学生
    Teacher getTeacherById(@Param("id")int id);
}

按结果嵌套处理
3. Mapper.xml配置文件配置



<mapper namespace="dao.TeacherMapper">
    

    <select id="getTeacherById" resultMap="TeacherToStudents" parameterType="int">
        select s.id sid,s.`name` sname,s.tid tid,t.`name` tname
        from student s,teacher t where s.tid=t.id and t.id=#{id}
    select>
    <resultMap id="TeacherToStudents" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" javaType="ArrayList" ofType="Student" >
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        collection>
    resultMap>


mapper>
  1. 在mybatis.xml核心配置文件中注册mapper

    <mappers>

        <mapper class="dao.TeacherMapper"/>
    mappers>
  1. 测试类:
public class Test {
     
    @org.junit.Test
    public void test(){
     
        //通过封装好的工具类获取SqlSession会话
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //通过接口类型class获取接口对象实例(动态代理)
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        //执行接口中的方法
        Teacher teacher = mapper.getTeacherById(1);
        System.out.println(teacher);
        sqlSession.commit();
        //关闭SqlSession
        sqlSession.close();
    }
}
  1. 结果:
    Mybatis之resultMap结果集映射_第9张图片
    按照查询嵌套处理

Mapper.xml配置文件配置:



<mapper namespace="dao.TeacherMapper">
    

    <select id="getTeacherById" resultMap="TeacherToStudents" parameterType="_int">
        select id,name
        from teacher   where id=#{id}
    select>
    <resultMap id="TeacherToStudents" type="Teacher">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <collection property="students" javaType="ArrayList" ofType="Student" select="queryStudentsByUid" column="id" >
        collection>
    resultMap>
    <select id="queryStudentsByUid" resultType="Student">
        select id,name,tid from student where tid=#{id}
    select>

mapper>

结果:
Mybatis之resultMap结果集映射_第10张图片

注意collection 中的

Mybatis之resultMap结果集映射_第11张图片

在这里插入图片描述

你可能感兴趣的:(Mybatis框架)