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<Book> 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<Book> getBooksssss() {
        return DataSupport.where("author_id=?", String.valueOf(id)).find(Book.class);
    }

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

    public void setBooks(List<Book> 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<Book> bookList = DataSupport.findAll(Book.class);//查询所有书籍
                List<Author> 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<Book> bookss = author1.getBooks();
                    //List<Book> 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_Category> 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<Book_Category> getBook_categoryList() {
        return book_categoryList;
    }

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

    /**
     * 获取一本书在Book_Category中的集合
     * @return
     */
    public List<Book_Category> 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_Category> 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<Book_Category> getBook_categoryList() {
        return book_categoryList;
    }

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

    /**
     * 获取一个书类型在Book_Category中的集合
     * @return
     */
    public List<Book_Category> 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<NewBook> 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<Category> 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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,142评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,298评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,068评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,081评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,099评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,071评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,990评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,832评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,274评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,488评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,649评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,378评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,979评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,625评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,643评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,545评论 2 352

推荐阅读更多精彩内容