1. 程式人生 > >mybatis:註解一對多

mybatis:註解一對多

上期一對一

一對多:作者有一本或多本書

書必然有一個作者,可能多本書共同擁有一個作者

 

package com.hc.model;

import java.util.List;

public class Author {
    private int autId;
    private String autName;
    private List<Book> books;

    public Author() {
    }

    public Author(String autName) {
        this.autName = autName;
    }

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

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

    public int getAutId() {
        return autId;
    }

    public void setAutId(int autId) {
        this.autId = autId;
    }

    public String getAutName() {
        return autName;
    }

    public void setAutName(String autName) {
        this.autName = autName;
    }
}

author中有一個書的list

package com.hc.model;

public class Book {
    private int bookId;
    private String bookName;
    private Author author;

    public Book() {
    }

    public Book(String bookName) {
        this.bookName = bookName;
    }

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }
}

book有一個作者屬性

 

package com.hc.mapper;

import com.hc.model.Author;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface AuthorMapper {
    @Select("select * from Author")
    @Results({
            @Result(id=true,property = "autId",column = "autId"),//id=true
            @Result(property = "books",column = "autId",many = @Many(select = "com.hc.mapper.BookMapper.selectBookByAutId"))
    })
    public List<Author> allAuthor();

    @Select("select * from Author where autId=#{autId}")
    public Author selectAuthorByAutId(int autId);

}

作者的Mapper

通過autid拿book,單獨拿author的autid

否則,最終顯示為0

Many:因為需要查出來的可能是多個,呼叫相應的物件Mapper中的方法

 

package com.hc.mapper;

import com.hc.model.Book;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface BookMapper {
    @Select("select * from Book where autId=#{autId}")
    public Book selectBookByAutId(int autId);

    @Select("select * from Book")
    @Results({
//            @Result(id = true,property = "bookId",column = "bookId"),不需
            @Result(property = "author",column = "autId",[email protected](select = "com.hc.mapper.AuthorMapper.selectAuthorByAutId"))
    })

    public List<Book> allBook();
}

book的Mapper

book中有一列autid,通過這個autid去作者表匹配到相應的作者物件

 

package com.hc.test;

import com.hc.mapper.AuthorMapper;
import com.hc.mapper.BookMapper;
import com.hc.model.Author;
import com.hc.model.Book;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class AuthorTest {

    private SqlSession session;


    @Before
    public void before() {
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(getClass().getClassLoader().getResourceAsStream("myBatis-config.xml"));
        session = factory.openSession();


    }

    @Test
    public void testBook() {
        BookMapper bookMapper=session.getMapper(BookMapper.class);
        for (Book book : bookMapper.allBook()) {
            System.out.println(book.getBookId() + "\t" + book.getBookName());
            Author author=book.getAuthor();
            System.out.println(author.getAutId() + "\t" + author.getAutName());
            System.out.println();
        }
    }

    @Test
    public void testAuthor() {
        AuthorMapper authorMapper=session.getMapper(AuthorMapper.class);
        for (Author author : authorMapper.allAuthor()) {
            System.out.println(author.getAutId() + "\t" + author.getAutName());
            for (Book book : author.getBooks()) {
                System.out.println(book.getBookId() + "\t" + book.getBookName());
            }
            System.out.println();
        }
    }



    @After
    public void after() {
        session.commit();
        session.close();
    }

}

一對多與上次的一對一類似,對比起來,mybatis註解式的關係比hibernate的真的是簡單的很多。