Android ORMapping庫

來源:互聯網
上載者:User

Android ORMapping庫
自己用Java的註解實現了Android SQLite的ORM庫,之前寫過XML的,不過感覺不是很穩定,效率、魯棒性各方面都不太好,今天花了一下午的時間,補全了所有的註解、注釋,產生了javadoc,寫了範例程式,並發布到了Github上,地址是:https://github.com/Steven-Luo/AnnotationDao,感興趣的可以試著用用,比之前純手動實現SQLite資料庫的操作簡單了許多,可擴充性也較好,用法如下: 複製代碼 1 //實體類 2 @Table(name = "dog") 3 public class Dog { 4     private boolean alive; 5     private int id; 6     private String name; 7  8     public Dog() { 9 10     }11 12     public Dog(boolean alive, String name) {13         this.alive = alive;14         this.name = name;15     }16 17     @Id(name = "id")18     @Column(name = "id", type = DataType.Integer)19     public int getId() {20         return id;21     }22 23     @Column(name = "name", type = DataType.Varchar, length = 20)24     public String getName() {25         return name;26     }27 28     @Column(name = "alive", type = DataType.Boolean)29     public boolean isAlive() {30         return alive;31     }32 33     public void setAlive(boolean alive) {34         this.alive = alive;35     }36 37     public void setId(int id) {38         this.id = id;39     }40 41     public void setName(String name) {42         this.name = name;43     }44 }45 46 //添加47 AnnotationDao dao = new AnnotationDao(this, Dog.class);48 49 String name = "dog1";50 boolean alive = true;51 52 Dog dog = new Dog(alive, name);53 dao.insert(dog);54 55 //尋找56 //列出所有對象57 List list = dao.list();58 //根據ID查詢59 dao.query(3);60 61 //刪除62 dao.delete(dog);63 64 //更新,更新時需要保證id欄位跟資料庫中的值是對應的,也就是之前調用list或query取出來的65 dog.setName("dog2");66 dao.update(dog);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.