标签:
<?xml version="1.0" encoding="utf-8"?> <litepal> <dbname value="litepal_demo"></dbname> <version value="1"></version> <list> <mapping class="com.cbooy.litepal.domain.News"></mapping> <mapping class="com.cbooy.litepal.domain.Comment"></mapping> <mapping class="com.cbooy.litepal.domain.Category"></mapping> <mapping class="com.cbooy.litepal.domain.Introduction"></mapping> </list> </litepal>
DataSupport.update(Class<?> modelClass, ContentValues values, long id)
DataSupport.updateAll(modelClass, values, conditions)
News updateNews = new News(); updateNews.setTitle("new title"); updateNews.update(2);
News updateNews = new News(); updateNews.setToDefault("commentCount"); updateNews.updateAll();
News news = DataSupport.find(News.class, 1); News firstNews = DataSupport.findFirst(News.class); News lastNews = DataSupport.findLast(News.class); List<News> newsList = DataSupport.findAll(News.class, 1, 3, 5, 7); List<News> newsList = DataSupport.findAll(News.class, new long[] { 1, 3, 5, 7 }); List<News> allNews = DataSupport.findAll(News.class);
其他查询条件
List<News> news = DataSupport.where("id>?","0").find(News.class); List<News> newsList = DataSupport.select("title", "content").where("commentcount > ?", "0").find(News.class); // 指定某些列 List<News> newsList = DataSupport.select("title", "content").where("commentcount > ?", "0").order("publishdate desc").find(News.class); //排序 List<News> newsList = DataSupport.select("title", "content").where("commentcount > ?", "0").order("publishdate desc").limit(10).find(News.class); // limit List<News> newsList = DataSupport.select("title", "content").where("commentcount > ?", "0").order("publishdate desc").limit(10).offset(10).find(News.class); // 分页
如何查询关联表中的数据,每一个类型的find()方法,都对应了一个带有isEager参数的方法重载,设置成true就表示激进查询,这样就会把关联表中的数据一起查询出来了。 (不推荐)
最好的做法是在实体类中封装关联对象的查询操作。
public class News extends DataSupport{ public List<Comment> getComments() { return DataSupport.where("news_id = ?", String.valueOf(id)).find(Comment.class); } }
原生SQL查询支持:
Cursor cursor = DataSupport.findBySQL("select * from news where commentcount>?", "0");
聚合函数的支持,LitePal中一共提供了count()、sum()、average()、max()和min()这五种聚合函数
int result = DataSupport.count(News.class); int result = DataSupport.where("commentcount = ?", "0").count(News.class); int result = DataSupport.sum(News.class, "commentcount", int.class); double result = DataSupport.average(News.class, "commentcount"); int result = DataSupport.max(News.class, "commentcount", int.class); int result = DataSupport.min(News.class, "commentcount", int.class);
标签:
原文地址:http://www.cnblogs.com/cbooy/p/4604287.html