Bootstrap

Mybatis执行原理以及resultmap结果集映射(一对多,多对一)

Mybatis的执行流程
1Resources获取全局配置文件
2实例化sqlsessionfactorybuilder构造器
3解析配置 文件流xmlconfigbuilder
4Configuration所有的配置信息
5实例化sqlsessionfactory
6transactional事务管理器
7创建executor执行器
8创建sqlsession
9实现crud
10查看是否执行成功
11提交事务
12关闭
多对一的处理
多对一:多个学生对应一个老师
对于学生而言,关联 多个学生关联一个老师【多对一】


package com.nan.pojo;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private  int id;
    private String name;
    //学生需要关联老师
    private  int tid;
}
package com.nan.pojo;

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

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Teacher {
    private  int id;
    private  String name;
    //一个老师拥有多个学生
    private List<Student> students;
}
package com.nan.dao;

import com.nan.pojo.Student;

import java.util.List;

public interface StudentMapper {
 List<Student> getStudent();

}
package com.nan.dao;

import com.nan.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface TeacherMapper {
//获取老师
    List<Teacher>getTeacher(@Param("tid") int id);
    //获取指定老师下的所有学生及老师的信息
     //Teacher getTeacher(@Param("tid") int id);
}
package com.nan.utils;
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;

    /**
     * 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的。
     * SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。
     * 而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先配置的 Configuration 实例来构建出 SqlSessionFactory 实例。
     */
    static {
        String resource = "mybatis-config.xml";
        try {
            //使用mybatis第一步获取sqlsessionfactory对象
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * SqlSession 完全包含了面向数据库执行sql命令所需的所有方法
     * @return
     */
    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession(true);
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nan.dao.StudentMapper">
<!--    思路查询所有的学生
根据查询出来的学生的t.id查找对于的老师-->
    <select id="getStudent" resultMap="StudentTeacher">
select *from mybatis.student

    </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
<!--       复杂的属性我们需要单独处理-->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select *from mybatis.teacher where id=#{id}
    </select>

</mapper>
import com.nan.dao.StudentMapper;
import com.nan.dao.TeacherMapper;
import com.nan.pojo.Student;
import com.nan.pojo.Teacher;
import com.nan.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class MyText {
    @Test
    public void getStudent(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        List<Teacher> teacher = mapper.getTeacher(1);
        for (Teacher teacher1 : teacher) {
            System.out.println(teacher1);
        }


        sqlSession.close();
    }
}





对于老师而言,集合,一个老师有很多学生【一对多】
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nan.dao.TeacherMapper">
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid,s.name sname,t.name tname,t.id tid
        from mybatis.student s,mybatis.teacher t
        where s.tid=t.id and t.id=#{tid}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <!--        -->
        <collection property="students" ofType="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
    </resultMap>
</mapper>
<?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>
    <properties resource="db.properties"/>

    <settings>

        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <typeAlias type="com.nan.pojo.Student" alias="Student"/>
        <typeAlias type="com.nan.pojo.Teacher" alias="Teacher"/>
    </typeAliases>




    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
<mappers>
    <mapper class="com.nan.dao.StudentMapper"/>
  <mapper class="com.nan.dao.TeacherMapper"/>


</mappers>

</configuration>

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;