mybatis 多对一 & 一对多

对一:多个tid对应一个id,可以简单理解多个学生对应一个老师这种类型。

一对多:  一个id对应多个tid,可以简单理解一个老师对应多个学生这种类型。

多对一和一对多都可以通过查询嵌套处理和结果嵌套处理。

多对一:

1.创建简单的环境。(坐标导包,类,接口,主配置文件,映射配置文件)

        类中的setter and getter, toString 方法可以通过@Data 简化daiti,前提是导入lombok坐标

        主配置文件中 可以通过<propertis resource="配置文件名">引入外部配置文件。

        <typeAliases> 可以通过两种方式(<typeAlias type="全限定类名" alias="别名"/>; <package name="包名"/>)配置别名

        <mapper> 可以通过两种方式(resource=xx/xx.xml 映射配置文件中写sql;    class=xx.xx.接口,接口上写注释sql)

2.接口中编写代码

public interface IStudent {

ListfindAll();

    ListgetStudent();

}

3.映射配置文件中写sql

<mapper namespace="com.dao.IStudent">

    <!--================按照查询嵌套处理================-->

    <select id="findAll" resultMap="StudentTeacher">

        select* from student;

    <resultMap id="StudentTeacher" type="Student">

        <result property="id" column="id"/>

        <result property="name" column="name"/>

        <!--复杂的对象我们需要单独处理 对象:association  集合:collection-->

        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>

    <select id="getTeacher" resultType="Teacher">

        select* from teacher where id=#{id}

 <!--**************按照结果嵌套处理*******************-->

    <select id="getStudent" resultMap="StudentTeacher2">

        select s.id sid,s.name sname,t.name tname from student s, teacher t where s.tid = t.id

    <resultMap id="StudentTeacher2" type="Student">

        <result property="id" column="sid"/>

        <result property="name" column="sname"/>

        <association property="teacher" column="tname" javaType="Teacher">

            <result property="name" column="tname"/>

</mapper>

4.编写测试类

@Test

public void testStudent(){

IStudent mapper =sqlSession.getMapper(IStudent.class);

    List studentList = mapper.findAll();

    for(Student student : studentList){

System.out.println(student);

    }

}

@Test

public void testStudent2(){

IStudent mapper =sqlSession.getMapper(IStudent.class);

    List studentList = mapper.getStudent();

    for(Student student : studentList){

System.out.println(student);

    }

}

一对多:

1.同上。

2.编写接口

public interface TeacherMapper {

Teacher1getTeacher(@Param("tid")int id);

    Teacher1getTeacher2(@Param("tid")int id);

}

3.编写映射配置文件

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

    <!--===================结果嵌套处理(引用别名)===============-->

    <select id="getTeacher" resultMap="TeacherStudent">

        select s.id sid, s.name sname, t.id tid, t.name tname from student s, teacher t where s.tid=t.id and t.id= #{tid};

    <resultMap id="TeacherStudent" type="Teacher1">

        <result property="id" column="tid"/>

        <result property="name" column="tname"/>

        <collection property="students" ofType="Student1">

            <result property="id" column="sid"/>

            <result property="name" column="sname"/>

            <result property="tid" column="tid"/>

    <!--====================查询嵌套处理=================-->

    <select id="getTeacher2" resultMap="TeacherStudent1">

        select * from teacher where id=#{tid};

    <resultMap id="TeacherStudent1" type="Teacher1">

        <collection property="students" javaType="ArrayList" ofType="Student1" column="id" select="getStudentsByID"/>

    <select id="getStudentsByID" resultType="Student1">

        select * from student where tid=#{tid}

</mapper>

4.编写测试类

public class TestAll {

private SqlSessionsqlSession;

private InputStreamin;

@Before

public void init()throws IOException {

//1.读取配置文件,生成字节输入流

    in = Resources.getResourceAsStream("MybatisConfig.xml");

    //2.获取sqlSessionFactory

    SqlSessionFactory factory =new SqlSessionFactoryBuilder().build(in);

    //3.获取sqlSession对象

    sqlSession = factory.openSession();

}

@After

public void destroy()throws IOException {

//6.释放资源

    sqlSession.close();

    in.close();

}

@Test

public void testGetTeacherById(){

TeacherMapper mapper =sqlSession.getMapper(TeacherMapper.class);

    Teacher1 teacher = mapper.getTeacher(1);

    System.out.println(teacher);

}

@Test

public void testGetTeacherById2(){

TeacherMapper mapper =sqlSession.getMapper(TeacherMapper.class);

    Teacher1 teacher = mapper.getTeacher2(1);

    System.out.println(teacher);

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容