LitePal的简单使用

1.关于Litepal

采用ORM(对象关系映射)的模式,体积小;Litepal相比较Android原生操作Sqlite使用起来非常方便,没有比较多步骤,主要工作量在于第一次使用往项目中引入Litepal,后期的使用是比较简单的。下面我们一起学习吧!

地址:https://github.com/LitePalFramework/LitePal

2.集成Litepal环境

2.1 引入依赖

implementation 'org.litepal.guolindev:core:3.2.3'

2.2 配置Application

让程序application 继承 org.litepal.LitePalApplication

  1. 第一种方法:直接将程序的application定义为LitePalApplication

    <application
            android:name="org.litepal.LitePalApplication"
            ...>
    </application>
    
  2. 第二种方法:手写一个继承LitePalApplication的类作为程序的appliction

    import org.litepal.LitePalApplication;
    
    public class BaseApplication extends LitePalApplication {
        
    }
    

    设置BaseApplication为程序Application

      <application
            android:name="com.jw.firstapp.application.BaseApplication"
            ...>
    </application>
    

2.3 新建assets目录,在assets目录中新建litepal.xml

  1. 新建assets目录

    在main目录下右击鼠标选New,然后选Folder,然后选Assets Folder
    在这里插入图片描述

    不用选中下面的框,直接Finish
    在这里插入图片描述

  2. 新建litepal.xml

    右击assets目录,新建一个File,名称命名为litepal.xml
    在这里插入图片描述

​ litepal.xml文件内容:

<?xml version="1.0" encoding="utf-8" ?>
<litepal> 
  <!--数据库的名称    -->
  <dbname value="first_db"></dbname>
  <!-- 数据的版本,后续升级需要改动版本号   -->
  <version value="1"></version>
  <!-- 管理数据库中的表,一个类对应的一个表,暂时为空,后面增加表需要添加对应的类进来   -->
  <list>
  </list>
</litepal>	

到这里,配置工作就完成了!

3.使用Litepal创建表

3.1 新建实体类Student

public class Student {

    private int id;

    private String name;

    private int age;

    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 int getAge() {
        return age;
    }

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

3.2 在litepal.xml中增加映射关系

<?xml version="1.0" encoding="utf-8" ?>
<litepal>
    <!--数据库的名称    -->
    <dbname value="first_db"></dbname>
    <!-- 数据的版本,后续升级需要改动版本号   -->
    <version value="1"></version>
    <!-- 管理数据库中的表,一个类对应的一个表   -->
    <list>
        <mapping class="com.jw.firstapp.db.mode.Student"></mapping>
    </list>
</litepal>

现在只要你对数据库有任何的操作,Student表就会被自动创建出来;如下,获取SQLiteDataBase实例方法:

//执行该方法后,表就会被新建出来
SQLiteDatabase db = Connector.getDatabase();

4.Litepal的增删改

4.1存储数据

  1. 预备工作,让需要存储的类继承LitepalSupport,这样就可以调用LitepalSupport的用来存储数据的save方法了;

     public class Student extends LitePalSupport {   
    		private int id;
    
        private String name;
    
        private int age;
    
        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 int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
    
  2. 操作

    • 保存一个对象 bolean save()

      Student xiaoming = new Student("小明", 12);
      //result代表save方法的结果
      boolean result = xiaoming.save();
      
    • 保存一组对象 boolean saveAll(Collection c)

      ArrayList<Student> students = new ArrayList<>();
      for (int i = 0; i < 5; i++) {
        Student student = new Student("小学生" + i, new Random().nextInt(15) + 1);
        students.add(student);
      }
      //result代表saveAll方法的结果
      boolean result = LitePal.saveAll(students);
      return result;
      

4.2修改数据

修改数据和添加数据的使用基本上一样,也是使用API,只是吧save和saveAll变成了update和updateAll。

4.2.1 条件的写法

指定条件:下面会使用到指定条件,操作符合条件的数据;这里的条件需要主要一下使用方法;在Litepal指定条件使用字符串和占位符(?)完成的;比如指定id为3的数据,写作(“id = ?”,“3”),指定id为5并且age=10的数据,写作(“id = ? and age = ?”,“5”,“20”)。

总结一下,这里的?表示占用一个位置,然后在后面跟着的字符串给它赋值;特点就是一个?对应一个值;

4.2.2 操作
  • 通过ID修改一条记录

    //API: int update(int id);
    //设置id为1的记录的名称为“小明改名字了”
    Student student = new Student("小明改名字了", 15);
    //受影响的行数
    int effectRows = student.update(1);
    
  • 修改所有表里所有数据

    //API:int updateAll();
    //将所有的记录的age都设置成0
    Student student = new Student();
    student.setAge(0);
    //受影响的行数
    int effectRows = student.updateAll();
    
  • 指定一个条件,修改符合条件的数据

    //API:int updateAll(String conditions...)
    //将表中age=12的记录的age设置成0
     Student student = new Student();
    student.setAge(0);
    //受影响的行数
    int effectRows = student.updateAll("age = ?","12");
    
  • 指定多个条件,修改符合条件的数据

    //API:int updateAll(String conditions...)
    //将表中age=100并且id=3的记录的age设置0
      Student student = new Student();
    student.setAge(20);
    //受影响的行数
    int effectRows = student.updateAll("age = ? and id = ? ","0","3");
    
  • 指定一个列,让该类恢复默认值

    //将表中所有记录的age这一列置成默认值0  
    Student student = new Student();
    student.setToDefault("age");
    //受影响的行数
    int effectRows =student.updateAll();
    

4.3删除数据

删除数据和添加数据的使用基本上一样,也是使用API,只是吧save和saveAll变成了delete和deleteAll。

  • 通过ID删除一条记录

    //删除表中id为2的记录
    //API: int delete(Class<?> modelClass,int id);
    // effectRows:受影响的行数
    int effectRows =LitePal.delete(Student.class,2);
    
  • 指定一个条件,删除满足条件的所有数据

    //删除表中id大于10的记录
    //API: int deleteAll(Class<?> modelClass,String conditions);
    // effectRows:受影响的行数
    int effectRows =LitePal.deleteAll(Student.class,"id > ?","10");
    
  • 删除所有数据

    //删除表中所有记录
    //API: int deleteAll(Class<?> modelClass);
    // effectRows:受影响的行数
    int effectRows =LitePal.deleteAll(Student.class);
    

5.使用Litepal查询数据

  • 通过Id查询数据

    //查询Id为1的记录
    Student student = LitePal.find(Student.class, 1);
    
  • 通过一组id查询一组数据

    //查询表中id为1,2,3,5的所有记录
    long[] ids = new long[]{1L,2L,3L,5L};
    List<Student> students = LitePal.findAll(Student.class, ids);
    
  • 查找表中第一个和最后一个数据

    //查询表中id为1,2,3,5的所有记录
    long[] ids = new long[]{1L,2L,3L,5L};
    List<Student> students = LitePal.findAll(Student.class, ids);
    
  • 根据条件查询表中数据

    //select:指定查询的列
    List<Student> students = LitePal.select("id", "age").find(Student.class);
    //where:指定条件,查询的结果都是符合该条件的
    List<Student> students1 = LitePal.where("age > ?", "10").find(Student.class);
    //order: 指定排序,desc:降序;asc:升序
    //按年龄降序
    List<Student> students2 = LitePal.order("age desc").find(Student.class);
    //limit:指定查询的最大条数
    List<Student> students3 = LitePal.limit(10).find(Student.class);
    //使用SQL原生语句查询
    Cursor cursor = LitePal.findBySQL("select * from student where age > 10");
    while (cursor != null && cursor.moveToNext()){
      int age = cursor.getInt(cursor.getColumnIndex("age"));
      String name = cursor.getString(cursor.getColumnIndex("name"));
      Student student = new Student(name, age);
    }
    
  • 查询所有数据

    List<Student> students = LitePal.findAll(Student.class);
    
  • 10
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值