MyBatis一对多映射简单查询案例

Posted 得不到的陪伴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis一对多映射简单查询案例相关的知识,希望对你有一定的参考价值。

一、案例描述

书本类别表和书本信息表,查询书本类别表中的某一记录,连带查询出所有该类别书本的信息。

 

二、数据库表格

书本类别表(booktypeid,booktypename)

书本信息表(bookid,booktypeid,bookname...)

 

三、java代码

1.书本类别

BookType.java

package com.yh.entity;

import java.util.List;

public class BookType {
    private int bookTypeId;
    private String bookTypeName;
    private List<Book> books;

    public int getBookTypeId() {
        return bookTypeId;
    }

    public void setBookTypeId(int bookTypeId) {
        this.bookTypeId = bookTypeId;
    }

    public String getBookTypeName() {
        return bookTypeName;
    }

    public void setBookTypeName(String bookTypeName) {
        this.bookTypeName = bookTypeName;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

}

 

BookTypeMapper.xml

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yh.mybatis.mapper.BookTypeMapper">

    <select id="findTypeById" parameterType="int"
        resultMap="bookTypeMap">
        select *from booktype where booktypeid = #{id}
    </select>

    <resultMap type="BookType" id="bookTypeMap">
        <id property="bookTypeId" column="booktypeid"></id>
        <result property="bookTypeName" column="booktypename"></result>
        <collection property="books" column="booktypeid"
            select="com.yh.mybatis.mapper.BookMapper.findBookInfosByType" ofType="Book"></collection>
    </resultMap>
</mapper>

 

BookTypeMapper.java

package com.yh.mybatis.mapper;

import com.yh.entity.BookType;

public interface BookTypeMapper {
    BookType findTypeById(int id);
}

 

2.书本信息

Book.java(省略getter()、setter()方法)

package com.yh.entity;

import java.sql.Timestamp;
import java.sql.Date;

import com.yh.dao.DBAddRecord;

public class Book {

    // 自动赋值
    private int bookId;
    private int bookTypeId;

    private int sellerId;
    private String bookName;
    private String press;
    private Date publishDate;
    private short bookSize;
    private short bookVersion;
    private String author;
    private String translator;
    private String isbn;
    private float price;
    private int pageNumber;
    private String introduction;
    private float marketPrice;
    private float memberPrice;
}

 

BookMapper.xml

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yh.mybatis.mapper.BookMapper">
    
    <select id="findBookInfosByType" parameterType="int"
        resultType="Book">
        select bookname,dealamount,price,press,imgurl,bookid
        from book where booktypeid = #{id}
    </select>

</mapper>

 

BookMapper.java

package com.yh.mybatis.mapper;

import java.util.List;

import com.yh.entity.Book;

public interface BookMapper {
    List<Book> findBookInfosByType(String type);
}

 

3.测试代码

@ResponseBody
@RequestMapping(value="/byType",produces = "application/json; charset=utf-8")
public String byType(
        @RequestParam(value="type") String type){
    this.init();
    BookTypeMapper btm = sqlSession.getMapper(BookTypeMapper.class);
    BookType books = btm.findTypeById(Integer.valueOf(type));
    this.destroy();
    return JSON.toJSONString(books);
}

 

以上是关于MyBatis一对多映射简单查询案例的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis--02

mybatis映射 一对一,一对多,多对多高级映射

Mybatis的多对多映射

Mybatis

Mybatis复杂映射开发:一对一一对多多对多查询

JavaEE简单实例——MyBatis的一对一映射的嵌套查询的简单介绍和基础配置