4 - 关联查询~一对一

一本书只属于一个人!
table

create table `student`(
`id` int auto_increment primary key,
`name` varchar(30) not null,
`age` int
)auto_increment=10000;

create table `book`
(`id` int auto_increment primary key,
`bookName` varchar(20),
`author` varchar(10),
`studentID` int references `student`(`id`)
)auto_increment=10000;

model

public class Student {
    private int id;
    private String name;
    private int age;

    public Student(){}
    //getter setter略
}

public class Book {

    private int bookID;
    private String bookName;
    private String author;

    private Student student;

    public Book() {
    }
   //getter setter略
}

dao

public interface StudentMapper {
    Book findBook(int bookID);
}

mapper.xml
方式一:嵌套

    <resultMap id="bookMapper" type="book">
        <id column="bookID" property="bookID"/>
        <result column="bookName" property="bookName"/>
        <result column="author" property="author"/>
        <association property="student" javaType="student">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="age" property="age"/>
        </association>
    </resultMap>

    <select id="findBook" resultMap="bookMapper">
        select `student`.`id` `id`,
       `student`.`name` `name`,
       `student`.`age` `age`,
       `book`.`id` `bookID`,
       `book`.`bookName` `bookName`,
       `book`.`author` `author`
        from `student`,`book`
        where `student`.`id` = `book`.`studentID`
        and `book`.`id` = #{bookID};
    </select>

方式二:复用resultMapper

    <select id="findBook" resultMap="bookMapper">
        select `student`.`id` `id`,
       `student`.`name` `name`,
       `student`.`age` `age`,
       `book`.`id` `bookID`,
       `book`.`bookName` `bookName`,
       `book`.`author` `author`
        from `student`,`book`
        where `student`.`id` = `book`.`studentID`
        and `book`.`id` = #{bookID};
    </select>

   <resultMap id="studentMapper" type="student">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="age" property="age"/>
    </resultMap>

    <resultMap id="bookMapper" type="book">
        <id column="bookID" property="bookID"/>
        <result column="bookName" property="bookName"/>
        <result column="author" property="author"/>
        <association property="student" resultMap="studentMapper"/>
    </resultMap>

综合3、4关联查询两篇文章,总结一下
1.明确实体之间的关系,优化范式,构建表
2.依据业务,构建model
3.编写关联查询语句
首先编写简单的model开始,建议使用resutlMap来映射,因为当SQL复杂时,嵌套很容易出错。然后依据一对一association或一对多collection来创建复杂实体。最后编写关联查询语句,注意返回字段的名称。
需要注意两点:

  1. resultMap中的column属性必须和数据库返回字段相同
  2. resultMap中的property属性必须和Model的成员属性相同
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,700评论 0 4
  • 1、什么是Mybatis? (1)Mybatis是一个半ORM(对象关系映射)框架,它内部封装了JDBC,开发时只...
    裘马轻狂大帅阅读 417评论 0 8
  • Spring 技术笔记Day 1 预热知识一、 基本术语Blob类型,二进制对象Object Graph:对象图...
    OchardBird阅读 1,005评论 0 2
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • MyBatis--关联查询 当查询内容涉及到具有关联关系的多个表时,就需要使用关联查询。根据表与表间的关联关系的不...
    我可能是个假开发阅读 9,649评论 0 16