3-----Mybatis----结果映射集

Posted 梦想成为DALAO

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3-----Mybatis----结果映射集相关的知识,希望对你有一定的参考价值。

resultMap

一对一

当数据库中的字段与实体类之间的字段不对应的情况;

例如:实力类中:id,name,password

   数据库中:id,name,pwd

解决方案

方案一:

select id,name,pwd as password from user

方案二:

在mapper文件中配置

  <resultMap id="UserMap" type="com.sicheng.entity.User">
<!--        id为主键,如果是一样的可以不配置-->
<!--        <id column="id" property="id"/>-->
<!--        <result column="name" property="name"/>-->
    <result column="pwd" property="password"/>
    </resultMap>

    <select id="selectUser" resultMap="UserMap">
        select * from user;
    </select>

一对多的关系

每个学生都会有一名老师,查询所有的学生,并将老师显示出来

 Student

package com.sicheng.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {

    private int sid;
    private String name;
    private Teacher teacher;
}

 Teacher

package com.sicheng.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {

    private int tid;
    private String name;
}

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.sicheng.dao.StudentMapper">
    <resultMap id="StudentTeacher" type="Student">
        <association property="teacher" column="tid" javaType="Teacher" select="selectTeacher"/>
    </resultMap>
    <select id="selectStudent" resultMap="StudentTeacher">
        select * from student;
    </select>

    <select id="selectTeacher" resultType="Teacher">
        select * from teacher where tid = #{id};
    </select>
</mapper> 

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 这里写配置内容 -->
    <typeAliases>
        <package name="com.sicheng.entity"/>
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <package name="com.sicheng.dao"/>
    </mappers>
</configuration>

student表

teacher表

最终测试

import com.sicheng.dao.StudentMapper;

import com.sicheng.entity.Student;

import com.sicheng.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;


public class MybatisTest {

    @Test
    public void selectUser(){
        SqlSession session = MybatisUtils.getSession();
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        List<Student> students = studentMapper.selectStudent();
        for (Student student :
                students) {
            System.out.println(student);
        }
    }


}

 重点

多对多的关系

一个老师有多个学生,查询老师,结果将老师对应的学生全部查询出来

student

package com.sicheng.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {

    private int sid;
    private String name;
    private int tid;
}

 

teacher

package com.sicheng.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {

    private int tid;
    private String name;
    private List<Student> students;
} 

TeacherMapper

package com.sicheng.dao;

import com.sicheng.entity.Teacher;

import java.util.List;

public interface TeacherMapper {

    public List<Teacher> selectTeacher();
}

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 这里写配置内容 -->
    <typeAliases>
        <package name="com.sicheng.entity"/>
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <package name="com.sicheng.dao"/>
    </mappers>
</configuration>

 

TeacherMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.sicheng.dao.TeacherMapper">

    <resultMap id="TeacherStudent" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" column="tid" select="getStudentByTeacherId"/>
    </resultMap>

    <select id="getStudentByTeacherId" resultType="Student">
        select * from student where tid = #{id}
    </select>
    <select id="selectTeacher" resultMap="TeacherStudent">
        select * from teacher;
    </select>


</mapper> 

 MybatisUtils

package com.sicheng.utils;

import com.mysql.cj.Session;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory = null;
    static {
        InputStream in = null;
        try {
            String resources = "mybatis-config.xml";
            in = Resources.getResourceAsStream(resources);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }
}

测试代码

import com.sicheng.dao.TeacherMapper;
import com.sicheng.entity.Student;

import com.sicheng.entity.Teacher;
import com.sicheng.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;


public class MybatisTest {

    @Test
    public void selectUser(){
        SqlSession session = MybatisUtils.getSession();
        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);
        List<Teacher> teachers = teacherMapper.selectTeacher();
        for (Teacher t : teachers) {
            System.out.println(t);
        }
    }


}

 重点代码

 

以上是关于3-----Mybatis----结果映射集的主要内容,如果未能解决你的问题,请参考以下文章

基于yolov7的闸片厚度检测方法

Mybatis缓存

JPA 本机查询结果集映射到具有子类的实体类

精通Mybatis之结果集处理流程与映射体系(联合查询与嵌套映射)

EntityManager执行原生sql结果集与bean的映射

如何将结果集映射到对象的嵌套结构?