LitePal数据操作:一对多,多对多

LitePal已经发布好长时间了,因此它的配置CURD就不在这里描述了。根据郭神的总结:即一对一关联的实现方式是用外键,多对一关联的实现方式也是用外键,多对多关联的实现方式是用中间表。根据这个我们来直接讲解一对多多对多的使用。

一对多

比如:书和作者;一本书对应一名作者(这里说的是大部分情况,及个别的情况除外),一名作者对应好几本书。这里就存在一对多的情况。下面我们来建立书和作者类。
书的实体类:Book.class

public class Book extends DataSupport {
    @Column(unique = true)
    private int id;
    private String bookCode;//书编号
    private String bookName;//书名称
    private int author_id;//作者ID

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAuthor_id() {
        return author_id;
    }

    public void setAuthor_id(int author_id) {
        this.author_id = author_id;
    }

    public String getBookCode() {
        return bookCode;
    }

    public void setBookCode(String bookCode) {
        this.bookCode = bookCode;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
}
注意:类中private int author_id;//作者ID就是作者和书的关联点

作者的实体类:Author.class

public class Author extends DataSupport {
    @Column(unique = true)
    private int id;//作者id
    private String name;//姓名
    private String sex;//性别
    private int age;//年龄
    private int b_num;//书总数
    private List books = new ArrayList<>();//书列表

    public int getB_num() {
        return b_num;
    }

    public void setB_num(int b_num) {
        this.b_num = b_num;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 查找作者所对应的书
     * @return
     */
    public List getBooksssss() {
        return DataSupport.where("author_id=?", String.valueOf(id)).find(Book.class);
    }

    public List getBooks() {
        return books;
    }

    public void setBooks(List books) {
        this.books = books;
    }
}
注意:在这里使用方法getBooksssss()来获取作者所对应的书籍,用方法getBooks()获取的书籍列表为空。

现在这两个类已经建立好了,下面就来进行数据的操作
数据库的创建:

Connector.getDatabase();//数据库的创建

数据的添加:

                Book book1 = new Book();
                book1.setBookCode("100001001");
                book1.setBookName("Pan Long");

                Book book2 = new Book();
                book2.setBookCode("100001002");
                book2.setBookName("Xing ChenBian");

                Author author = new Author();
                author.setAge(33);
                author.setName("WoChiXiHongShi");
                author.setSex("male");

                book1.setAuthor_id(author.getId());
                book2.setAuthor_id(author.getId());

                author.getBooks().add(book1);
                author.getBooks().add(book2);
                author.setB_num(author.getBooks().size());

                if (book1.save()){
                    Log.e("LHC", "book1 添加成功");
                }else{
                    Log.e("LHC", "book1 添加失败");
                }
                if (book2.save()){
                    Log.e("LHC", "book2 添加成功");
                }else{
                    Log.e("LHC", "book2 添加失败");
                }
                if (author.save()){
                    Log.e("LHC", "author 添加成功");
                }else{
                    Log.e("LHC", "author 添加失败");
                }

数据查询:

                List bookList = DataSupport.findAll(Book.class);//查询所有书籍
                List authorList = DataSupport.findAll(Author.class);//查询所有作者
                for (Book book : bookList) {
                    Author author1 = DataSupport.find(Author.class, book.getAuthor_id());//查询书对应的作者
                    Log.e("LHC", "bookAuthor:"+ author1.getName());
                    Log.e("LHC", "bookCode:"+ book.getBookCode()+",bookName:"+book.getBookName()+",author_id:"+ book.getAuthor_id());
                }
                for (Author author1 : authorList) {
                    Log.e("LHC", "id:"+ author1.getId()+",name:"+ author1.getName()+",sex:"+author1.getSex()+",size:"+author1.getB_num());
                    author1.setBooks(author1.getBooksssss());//调用`getBooksssss()`方法获取作者名下所有的书籍并且赋值(此处可以直接使用,没有必要在进行赋值)
                    List bookss = author1.getBooks();
                    //List bookssss = author1.getBooksssss();//可以直接使用
                    for (Book book : bookss) {
                        Log.e("LHC", "bookCode:"+ book.getBookCode()+",bookName:"+book.getBookName());
                    }
                }

这样就完成了一对多数据的建立和操作。

多对多

比如:书和类型;一本书可能既是修仙类型有是玄幻类型,当然一个类型中包含了很多书籍,这样就形成了多对多的关系。

对于多对多的处理,是为书类类型类建立一个书_类型类,用来记录书和类型的关联关系。那么对于书类书_类型类来说,就是一对多的关系;同样的对于类型类书_类型类来说,也是一对多的关系。这样看来我们对多对多的操作就简单了。

书的实体类:NewBook.class

public class NewBook extends DataSupport {
    @Column(unique = true)
    private int id;
    private String bookCode;//书编号
    private String bookName;//书名称
    private String bookAuthor;//书作者
    private List book_categoryList = new ArrayList<>();

    public NewBook(String bookCode, String bookName, String bookAuthor){
        this.bookCode = bookCode;
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List getBook_categoryList() {
        return book_categoryList;
    }

    public void setBook_categoryList(List book_categoryList) {
        this.book_categoryList = book_categoryList;
    }

    /**
     * 获取一本书在Book_Category中的集合
     * @return
     */
    public List getBookCategories() {
        return DataSupport.where("book_id=?", String.valueOf(id)).find(Book_Category.class);
    }

    public String getBookAuthor() {
        return bookAuthor;
    }

    public void setBookAuthor(String bookAuthor) {
        this.bookAuthor = bookAuthor;
    }

    public String getBookCode() {
        return bookCode;
    }

    public void setBookCode(String bookCode) {
        this.bookCode = bookCode;
    }

    public String getBookName() {
        return bookName;
    }

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

类型实体类:Category.class

public class Category extends DataSupport {
    private int id;
    private String typeName;
    private List book_categoryList = new ArrayList<>();

    public Category(String typeName){
        this.typeName = typeName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public List getBook_categoryList() {
        return book_categoryList;
    }

    public void setBook_categoryList(List book_categoryList) {
        this.book_categoryList = book_categoryList;
    }

    /**
     * 获取一个书类型在Book_Category中的集合
     * @return
     */
    public List getBookCategories() {
        return DataSupport.where("category_id=?", String.valueOf(id)).find(Book_Category.class);
    }
}

书_类型实体类:Book_Category.class

public class Book_Category extends DataSupport {
    private int book_id;//书ID
    private int category_id;//书类型ID

    public Book_Category(int book_id, int category_id){
        this.book_id = book_id;
        this.category_id = category_id;
    }

    public int getBook_id() {
        return book_id;
    }

    public void setBook_id(int book_id) {
        this.book_id = book_id;
    }

    public int getCategory_id() {
        return category_id;
    }

    public void setCategory_id(int category_id) {
        this.category_id = category_id;
    }
}

数据的添加:

                Category category1 = new Category("玄幻");
                category1.save();
                Category category2 = new Category("修仙");
                category2.save();
                Category category3 = new Category("穿越");
                category3.save();

                NewBook book1 = new NewBook("1000001001", "盘龙", "我吃西红柿");
                NewBook book2 = new NewBook("1000001002", "星辰变", "我吃西红柿");
                NewBook book3 = new NewBook("1000001003", "琴帝", "唐家三少");
                NewBook book4 = new NewBook("1000001004", "遮天", "辰东");
                NewBook book5 = new NewBook("1000001005", "斗破苍穹", "天蚕土豆");
                book1.save();
                book2.save();
                book3.save();
                book4.save();
                book5.save();

                Book_Category book_category1 = new Book_Category(book1.getId(), category1.getId());
                Book_Category book_category2 = new Book_Category(book1.getId(), category2.getId());
                book1.getBook_categoryList().add(book_category1);
                book1.getBook_categoryList().add(book_category2);
                Book_Category book_category3 = new Book_Category(book2.getId(), category1.getId());
                Book_Category book_category4 = new Book_Category(book2.getId(), category3.getId());
                book2.getBook_categoryList().add(book_category3);
                book2.getBook_categoryList().add(book_category4);
                Book_Category book_category5 = new Book_Category(book3.getId(), category1.getId());
                Book_Category book_category6 = new Book_Category(book3.getId(), category2.getId());
                book3.getBook_categoryList().add(book_category5);
                book3.getBook_categoryList().add(book_category6);
                Book_Category book_category7 = new Book_Category(book4.getId(), category2.getId());
                Book_Category book_category8 = new Book_Category(book4.getId(), category3.getId());
                book4.getBook_categoryList().add(book_category7);
                book4.getBook_categoryList().add(book_category8);
                Book_Category book_category9 = new Book_Category(book5.getId(), category2.getId());
                Book_Category book_category0 = new Book_Category(book5.getId(), category3.getId());
                book5.getBook_categoryList().add(book_category9);
                book5.getBook_categoryList().add(book_category0);
                book_category1.save();
                book_category2.save();
                book_category3.save();
                book_category4.save();
                book_category5.save();
                book_category6.save();
                book_category7.save();
                book_category8.save();
                book_category9.save();
                book_category0.save();

数据查询:

                //一本书对应的所有类型
                List bookList = DataSupport.findAll(NewBook.class);
                for (NewBook book : bookList) {
                    Log.e("LHC", "bookCode:"+ book.getBookCode()+",bookName:"+book.getBookName()+",author:"+ book.getBookAuthor());

                    for (Book_Category book_category : book.getBookCategories()) {
                        Category category = DataSupport.find(Category.class, book_category.getCategory_id());
                        Log.e("LHC", "category_id:"+ book_category.getCategory_id()+",category_name:"+ category.getTypeName());
                    }
                }

                //一种类型对应的所有书籍
                List categoryList = DataSupport.findAll(Category.class);
                for (Category category : categoryList) {
                    Log.e("LHC", "category_name:"+ category.getTypeName());

                    for (Book_Category book_category : category.getBookCategories()) {
                        NewBook book = DataSupport.find(NewBook.class, book_category.getBook_id());
                        Log.e("LHC", "bookCode:"+ book.getBookCode()+",bookName:"+book.getBookName()+",author:"+ book.getBookAuthor());
                    }
                }

结果输出:

 //一本书对应的所有类型
04-10 13:59:39.314 16734-16734/com.lhc.view E/LHC: bookCode:1000001001,bookName:盘龙,author:我吃西红柿
04-10 13:59:39.361 16734-16734/com.lhc.view E/LHC: category_id:1,category_name:玄幻
04-10 13:59:39.371 16734-16734/com.lhc.view E/LHC: category_id:2,category_name:修仙
04-10 13:59:39.371 16734-16734/com.lhc.view E/LHC: bookCode:1000001002,bookName:星辰变,author:我吃西红柿
04-10 13:59:39.389 16734-16734/com.lhc.view E/LHC: category_id:1,category_name:玄幻
04-10 13:59:39.396 16734-16734/com.lhc.view E/LHC: category_id:3,category_name:穿越
04-10 13:59:39.396 16734-16734/com.lhc.view E/LHC: bookCode:1000001003,bookName:琴帝,author:唐家三少
04-10 13:59:39.411 16734-16734/com.lhc.view E/LHC: category_id:1,category_name:玄幻
04-10 13:59:39.416 16734-16734/com.lhc.view E/LHC: category_id:2,category_name:修仙
04-10 13:59:39.416 16734-16734/com.lhc.view E/LHC: bookCode:1000001004,bookName:遮天,author:辰东
04-10 13:59:39.429 16734-16734/com.lhc.view E/LHC: category_id:2,category_name:修仙
04-10 13:59:39.435 16734-16734/com.lhc.view E/LHC: category_id:3,category_name:穿越
04-10 13:59:39.435 16734-16734/com.lhc.view E/LHC: bookCode:1000001005,bookName:斗破苍穹,author:天蚕土豆
04-10 13:59:39.444 16734-16734/com.lhc.view E/LHC: category_id:2,category_name:修仙
04-10 13:59:39.448 16734-16734/com.lhc.view E/LHC: category_id:3,category_name:穿越

//一种类型对应的所有书籍
04-10 13:59:39.452 16734-16734/com.lhc.view E/LHC: category_name:玄幻
04-10 13:59:39.462 16734-16734/com.lhc.view E/LHC: bookCode:1000001001,bookName:盘龙,author:我吃西红柿
04-10 13:59:39.468 16734-16734/com.lhc.view E/LHC: bookCode:1000001002,bookName:星辰变,author:我吃西红柿
04-10 13:59:39.473 16734-16734/com.lhc.view E/LHC: bookCode:1000001003,bookName:琴帝,author:唐家三少
04-10 13:59:39.473 16734-16734/com.lhc.view E/LHC: category_name:修仙
04-10 13:59:39.482 16734-16734/com.lhc.view E/LHC: bookCode:1000001001,bookName:盘龙,author:我吃西红柿
04-10 13:59:39.486 16734-16734/com.lhc.view E/LHC: bookCode:1000001003,bookName:琴帝,author:唐家三少
04-10 13:59:39.490 16734-16734/com.lhc.view E/LHC: bookCode:1000001004,bookName:遮天,author:辰东
04-10 13:59:39.494 16734-16734/com.lhc.view E/LHC: bookCode:1000001005,bookName:斗破苍穹,author:天蚕土豆
04-10 13:59:39.494 16734-16734/com.lhc.view E/LHC: category_name:穿越
04-10 13:59:39.504 16734-16734/com.lhc.view E/LHC: bookCode:1000001002,bookName:星辰变,author:我吃西红柿
04-10 13:59:39.509 16734-16734/com.lhc.view E/LHC: bookCode:1000001004,bookName:遮天,author:辰东
04-10 13:59:39.513 16734-16734/com.lhc.view E/LHC: bookCode:1000001005,bookName:斗破苍穹,author:天蚕土豆

[参考]
1,https://blog.csdn.net/guolin_blog/article/details/39207945

你可能感兴趣的:(LitePal数据操作:一对多,多对多)